|
|
|
@ -7,18 +7,32 @@ import 'package:nearby_connections/nearby_connections.dart';
|
|
|
|
|
|
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
|
|
|
|
|
|
import 'package:loc_chain_app/util/keyfile_manager.dart';
|
|
|
|
|
import 'package:loc_chain_app/util/transaction_manager.dart';
|
|
|
|
|
|
|
|
|
|
class Connect {
|
|
|
|
|
final serviceId = "com.yourdomain.appname";
|
|
|
|
|
final Strategy strategy = Strategy.P2P_STAR;
|
|
|
|
|
late final _userName;
|
|
|
|
|
final BuildContext? context;
|
|
|
|
|
|
|
|
|
|
Map<String, ConnectionInfo> endpointMap = Map();
|
|
|
|
|
Map<String, Transaction> transactionMap = Map();
|
|
|
|
|
|
|
|
|
|
Connect() {
|
|
|
|
|
Connect({this.context}) {
|
|
|
|
|
SharedPreferences.getInstance()
|
|
|
|
|
.then((s) => _userName = s.getString('userName') ?? '0');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void showSnackbar(dynamic a) {
|
|
|
|
|
if (context == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
ScaffoldMessenger.of(context!).showSnackBar(SnackBar(
|
|
|
|
|
content: Text(a.toString()),
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void startConnect() async {
|
|
|
|
|
// final prefs = await SharedPreferences.getInstance();
|
|
|
|
|
// final userName = prefs.getString('userName') ?? " ";
|
|
|
|
@ -27,19 +41,21 @@ class Connect {
|
|
|
|
|
bool advertise = await Nearby().startAdvertising(
|
|
|
|
|
_userName,
|
|
|
|
|
strategy,
|
|
|
|
|
onConnectionInitiated: (String id, ConnectionInfo info) {
|
|
|
|
|
// Called whenever a discoverer requests connection
|
|
|
|
|
//
|
|
|
|
|
// onConnectionInit
|
|
|
|
|
},
|
|
|
|
|
onConnectionInitiated: onConnectionInit,
|
|
|
|
|
onConnectionResult: (String id, Status status) {
|
|
|
|
|
// Called when connection is accepted/rejected
|
|
|
|
|
// if connection is accepted send the transaction
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
endpointMap.forEach((key, value) async {
|
|
|
|
|
String str = await Transaction.generateHash(key);
|
|
|
|
|
Nearby().sendBytesPayload(key, Uint8List.fromList(str.codeUnits));
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
onDisconnected: (String id) {
|
|
|
|
|
// Callled whenever a discoverer disconnects from advertiser
|
|
|
|
|
// delete connection info
|
|
|
|
|
endpointMap.remove(id);
|
|
|
|
|
// delete transaction info
|
|
|
|
|
transactionMap.remove(id);
|
|
|
|
|
},
|
|
|
|
|
serviceId: serviceId, // uniquely identifies your app
|
|
|
|
|
);
|
|
|
|
@ -49,6 +65,19 @@ class Connect {
|
|
|
|
|
strategy,
|
|
|
|
|
onEndpointFound: (String id, String userName, String serviceId) {
|
|
|
|
|
// called when an advertiser is found
|
|
|
|
|
Nearby().requestConnection(
|
|
|
|
|
userName,
|
|
|
|
|
id,
|
|
|
|
|
onConnectionInitiated: onConnectionInit,
|
|
|
|
|
onConnectionResult: (id, status) {
|
|
|
|
|
showSnackbar(status);
|
|
|
|
|
},
|
|
|
|
|
onDisconnected: (id) {
|
|
|
|
|
endpointMap.remove(id);
|
|
|
|
|
showSnackbar(
|
|
|
|
|
"Disconnected from: ${endpointMap[id]!.endpointName}, id $id");
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
onEndpointLost: (String? id) {
|
|
|
|
|
//called when an advertiser is lost (only if we weren't connected to it )
|
|
|
|
@ -59,9 +88,49 @@ class Connect {
|
|
|
|
|
// platform exceptions like unable to start bluetooth or insufficient permissions
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void onConnectionInit(String otherId, ConnectionInfo info) {
|
|
|
|
|
// Called whenever a discoverer requests connection
|
|
|
|
|
//
|
|
|
|
|
// onConnectionInit
|
|
|
|
|
if (endpointMap.containsKey(otherId)) {
|
|
|
|
|
Nearby().rejectConnection(otherId);
|
|
|
|
|
}
|
|
|
|
|
endpointMap[otherId] = info;
|
|
|
|
|
Nearby().acceptConnection(
|
|
|
|
|
otherId,
|
|
|
|
|
onPayLoadRecieved: (_otherId, payload) async {
|
|
|
|
|
if (payload.type != PayloadType.BYTES) return;
|
|
|
|
|
// completed payload from other connection
|
|
|
|
|
String str = String.fromCharCodes(payload.bytes!);
|
|
|
|
|
var parts = str.split(':');
|
|
|
|
|
if (parts.length != 2) {
|
|
|
|
|
showSnackbar("$_otherId invalid payload: $str");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// Store transaction
|
|
|
|
|
var combinedHash = parts[0];
|
|
|
|
|
var publicKey = parts[1];
|
|
|
|
|
transactionMap[_otherId] =
|
|
|
|
|
Transaction(hash: combinedHash, pubKey: publicKey);
|
|
|
|
|
// sign combined hash with our private key
|
|
|
|
|
// upload hash+otherKey to firebase
|
|
|
|
|
transactionMap.remove(_otherId);
|
|
|
|
|
},
|
|
|
|
|
onPayloadTransferUpdate: (endid, payloadTransferUpdate) {
|
|
|
|
|
if (payloadTransferUpdate.status == PayloadStatus.IN_PROGRESS) {
|
|
|
|
|
print(payloadTransferUpdate.bytesTransferred);
|
|
|
|
|
} else if (payloadTransferUpdate.status == PayloadStatus.FAILURE) {
|
|
|
|
|
print("failed");
|
|
|
|
|
showSnackbar(endid + ": FAILED to transfer file");
|
|
|
|
|
} else if (payloadTransferUpdate.status == PayloadStatus.SUCCESS) {
|
|
|
|
|
showSnackbar(
|
|
|
|
|
"$endid success, total bytes = ${payloadTransferUpdate.totalBytes}");
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ElevatedButton(
|
|
|
|
@ -77,10 +146,6 @@ class Connect {
|
|
|
|
|
// },
|
|
|
|
|
// ),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// void onConnectionInit(String id, ConnectionInfo info) {
|
|
|
|
|
// showModalBottomSheet(
|
|
|
|
|
// context: context,
|
|
|
|
|