2021-04-10 19:48:24 +00:00
|
|
|
import 'dart:typed_data';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
2021-04-11 02:52:37 +00:00
|
|
|
import 'package:flutter_udid/flutter_udid.dart';
|
2021-04-10 19:48:24 +00:00
|
|
|
import 'package:nearby_connections/nearby_connections.dart';
|
|
|
|
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
|
2021-04-10 22:06:05 +00:00
|
|
|
import 'package:loc_chain_app/util/keyfile_manager.dart';
|
|
|
|
import 'package:loc_chain_app/util/transaction_manager.dart';
|
|
|
|
|
2021-04-10 20:19:32 +00:00
|
|
|
class Connect {
|
2021-04-11 02:52:37 +00:00
|
|
|
static final serviceId = "com.yourdomain.appname";
|
|
|
|
static final Strategy strategy = Strategy.P2P_STAR;
|
|
|
|
static String _userName = "";
|
|
|
|
static BuildContext? context;
|
2021-04-10 21:01:14 +00:00
|
|
|
|
2021-04-11 02:52:37 +00:00
|
|
|
static Map<String, ConnectionInfo> endpointMap = Map();
|
|
|
|
static Map<String, Transaction> transactionMap = Map();
|
2021-04-10 21:01:14 +00:00
|
|
|
|
2021-04-11 02:52:37 +00:00
|
|
|
static void showSnackbar(dynamic a) {
|
2021-04-10 22:06:05 +00:00
|
|
|
if (context == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ScaffoldMessenger.of(context!).showSnackBar(SnackBar(
|
|
|
|
content: Text(a.toString()),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2021-04-11 02:52:37 +00:00
|
|
|
static void stop() {
|
|
|
|
Nearby().stopAdvertising();
|
|
|
|
Nearby().stopDiscovery();
|
|
|
|
Nearby().stopAllEndpoints();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void start() async {
|
|
|
|
_userName = await FlutterUdid.consistentUdid;
|
2021-04-10 21:01:14 +00:00
|
|
|
// final prefs = await SharedPreferences.getInstance();
|
|
|
|
// final userName = prefs.getString('userName') ?? " ";
|
2021-04-10 20:19:32 +00:00
|
|
|
|
2021-04-11 02:52:37 +00:00
|
|
|
Nearby().startAdvertising(
|
|
|
|
_userName,
|
|
|
|
strategy,
|
|
|
|
onConnectionInitiated: onConnectionInit,
|
|
|
|
onConnectionResult: (String id, Status status) async {
|
|
|
|
// Called when connection is accepted/rejected
|
|
|
|
// if connection is accepted send the transaction
|
|
|
|
if (status != Status.CONNECTED) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Connected to other device, send combined hash and pub key
|
|
|
|
String str = await Transaction.generateHash(id);
|
|
|
|
print(str);
|
|
|
|
Nearby().sendBytesPayload(id, 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
|
|
|
|
).catchError((e) {
|
|
|
|
print(e);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
Nearby().startDiscovery(
|
|
|
|
_userName,
|
|
|
|
strategy,
|
|
|
|
onEndpointFound: (String id, String userName, String serviceId) {
|
|
|
|
// called when an advertiser is found
|
|
|
|
Nearby().requestConnection(
|
|
|
|
userName,
|
|
|
|
id,
|
|
|
|
onConnectionInitiated: onConnectionInit,
|
|
|
|
onConnectionResult: (String id, Status status) async {
|
|
|
|
// Called when connection is accepted/rejected
|
|
|
|
// if connection is accepted send the transaction
|
|
|
|
if (status != Status.CONNECTED) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Connected to other device, send combined hash and pub key
|
|
|
|
String str = await Transaction.generateHash(id);
|
|
|
|
Nearby()
|
|
|
|
.sendBytesPayload(id, Uint8List.fromList(str.codeUnits))
|
|
|
|
.catchError((e) {
|
|
|
|
print(e);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
onDisconnected: (id) {
|
|
|
|
endpointMap.remove(id);
|
|
|
|
print(
|
|
|
|
"Disconnected from: ${endpointMap[id]!.endpointName}, id $id");
|
|
|
|
},
|
|
|
|
).catchError((e) {
|
|
|
|
print(e);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
onEndpointLost: (String? id) {
|
|
|
|
//called when an advertiser is lost (only if we weren't connected to it )
|
|
|
|
},
|
|
|
|
serviceId: serviceId, // uniquely identifies your app
|
|
|
|
).catchError((e) {
|
|
|
|
print(e);
|
|
|
|
return true;
|
|
|
|
});
|
2021-04-10 20:19:32 +00:00
|
|
|
}
|
2021-04-10 21:04:27 +00:00
|
|
|
|
2021-04-11 02:52:37 +00:00
|
|
|
static void onConnectionInit(String otherId, ConnectionInfo info) {
|
|
|
|
print('Connection initialized with $otherId');
|
2021-04-10 22:06:05 +00:00
|
|
|
// Called whenever a discoverer requests connection
|
|
|
|
//
|
|
|
|
// onConnectionInit
|
|
|
|
if (endpointMap.containsKey(otherId)) {
|
2021-04-11 02:52:37 +00:00
|
|
|
print('Connection rejected to/from $otherId');
|
2021-04-10 22:06:05 +00:00
|
|
|
Nearby().rejectConnection(otherId);
|
2021-04-11 02:52:37 +00:00
|
|
|
return;
|
2021-04-10 22:06:05 +00:00
|
|
|
}
|
2021-04-11 02:52:37 +00:00
|
|
|
print('Connection accepted to/from $otherId');
|
|
|
|
|
2021-04-10 22:06:05 +00:00
|
|
|
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) {
|
2021-04-11 02:52:37 +00:00
|
|
|
print("$_otherId invalid payload: $str");
|
2021-04-10 22:06:05 +00:00
|
|
|
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
|
2021-04-10 22:44:39 +00:00
|
|
|
print('Received $_otherId payload: $combinedHash:$publicKey');
|
2021-04-10 22:06:05 +00:00
|
|
|
// upload hash+otherKey to firebase
|
|
|
|
},
|
|
|
|
onPayloadTransferUpdate: (endid, payloadTransferUpdate) {
|
|
|
|
if (payloadTransferUpdate.status == PayloadStatus.IN_PROGRESS) {
|
|
|
|
print(payloadTransferUpdate.bytesTransferred);
|
|
|
|
} else if (payloadTransferUpdate.status == PayloadStatus.FAILURE) {
|
2021-04-11 02:52:37 +00:00
|
|
|
print(endid + ": FAILED to transfer file");
|
2021-04-10 22:06:05 +00:00
|
|
|
} else if (payloadTransferUpdate.status == PayloadStatus.SUCCESS) {
|
2021-04-11 02:52:37 +00:00
|
|
|
print(
|
2021-04-10 22:06:05 +00:00
|
|
|
"$endid success, total bytes = ${payloadTransferUpdate.totalBytes}");
|
|
|
|
}
|
|
|
|
},
|
2021-04-11 02:52:37 +00:00
|
|
|
).catchError((e) {
|
|
|
|
print(e);
|
|
|
|
return true;
|
|
|
|
});
|
2021-04-10 22:06:05 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-10 21:04:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
// ElevatedButton(
|
|
|
|
// child: Text("Send Random Bytes Payload"),
|
|
|
|
// onPressed: () async {
|
|
|
|
// endpointMap.forEach((key, value) {
|
|
|
|
// String a = Random().nextInt(100).toString();
|
|
|
|
|
|
|
|
// showSnackbar("Sending $a to ${value.endpointName}, id: $key");
|
|
|
|
// Nearby()
|
|
|
|
// .sendBytesPayload(key, Uint8List.fromList(a.codeUnits));
|
|
|
|
// });
|
|
|
|
// },
|
|
|
|
// ),
|
|
|
|
|
|
|
|
// void onConnectionInit(String id, ConnectionInfo info) {
|
|
|
|
// showModalBottomSheet(
|
|
|
|
// context: context,
|
|
|
|
// builder: (builder) {
|
|
|
|
// return Center(
|
|
|
|
// child: Column(
|
|
|
|
// children: <Widget>[
|
|
|
|
// Text("id: " + id),
|
|
|
|
// Text("Token: " + info.authenticationToken),
|
|
|
|
// Text("Name" + info.endpointName),
|
|
|
|
// Text("Incoming: " + info.isIncomingConnection.toString()),
|
|
|
|
// ElevatedButton(
|
|
|
|
// child: Text("Accept Connection"),
|
|
|
|
// onPressed: () {
|
|
|
|
// Navigator.pop(context);
|
|
|
|
// setState(() {
|
|
|
|
// endpointMap[id] = info;
|
|
|
|
// });
|
|
|
|
// Nearby().acceptConnection(
|
|
|
|
// id,
|
|
|
|
// onPayLoadRecieved: (endid, payload) async {
|
|
|
|
// if (payload.type == PayloadType.BYTES) {
|
|
|
|
// String str = String.fromCharCodes(payload.bytes!);
|
|
|
|
// showSnackbar(endid + ": " + str);
|
|
|
|
|
|
|
|
// if (str.contains(':')) {
|
|
|
|
// // used for file payload as file payload is mapped as
|
|
|
|
// // payloadId:filename
|
|
|
|
// int payloadId = int.parse(str.split(':')[0]);
|
|
|
|
// String fileName = (str.split(':')[1]);
|
|
|
|
|
|
|
|
// if (map.containsKey(payloadId)) {
|
|
|
|
// if (await tempFile!.exists()) {
|
|
|
|
// tempFile!.rename(
|
|
|
|
// tempFile!.parent.path + "/" + fileName);
|
|
|
|
// } else {
|
|
|
|
// showSnackbar("File doesn't exist");
|
|
|
|
// }
|
|
|
|
// } else {
|
|
|
|
// //add to map if not already
|
|
|
|
// map[payloadId] = fileName;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// } else if (payload.type == PayloadType.FILE) {
|
|
|
|
// showSnackbar(endid + ": File transfer started");
|
|
|
|
// tempFile = File(payload.filePath!);
|
|
|
|
// }
|
|
|
|
// },
|
|
|
|
// 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}");
|
|
|
|
|
|
|
|
// if (map.containsKey(payloadTransferUpdate.id)) {
|
|
|
|
// //rename the file now
|
|
|
|
// String name = map[payloadTransferUpdate.id]!;
|
|
|
|
// tempFile!.rename(tempFile!.parent.path + "/" + name);
|
|
|
|
// } else {
|
|
|
|
// //bytes not received till yet
|
|
|
|
// map[payloadTransferUpdate.id] = "";
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// },
|
|
|
|
// );
|
|
|
|
// },
|
|
|
|
// ),
|
|
|
|
// ElevatedButton(
|
|
|
|
// child: Text("Reject Connection"),
|
|
|
|
// onPressed: () async {
|
|
|
|
// Navigator.pop(context);
|
|
|
|
// try {
|
|
|
|
// await Nearby().rejectConnection(id);
|
|
|
|
// } catch (e) {
|
|
|
|
// showSnackbar(e);
|
|
|
|
// }
|
|
|
|
// },
|
|
|
|
// ),
|
|
|
|
// ],
|
|
|
|
// ),
|
|
|
|
// );
|
|
|
|
// },
|
|
|
|
// );
|
|
|
|
// }
|
|
|
|
// }
|