mirror of
https://github.com/hackku21/nearby_connections.git
synced 2024-10-27 19:14:01 +00:00
need Help
This commit is contained in:
parent
1613f9a0c7
commit
c05f71f0c0
@ -39,20 +39,15 @@ class _MyBodyState extends State<Body> {
|
|||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
WidgetsBinding.instance!.addPostFrameCallback((_) {
|
WidgetsBinding.instance!.addPostFrameCallback((_) {
|
||||||
// _userName = await FlutterUdid.consistentUdid;
|
|
||||||
startAdvertising();
|
startAdvertising();
|
||||||
startDiscovery();
|
startDiscovery();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
final String userName =
|
// static String userName =
|
||||||
"00000 11111 22222 33333 44444 55555 66666 77777 88888 99999 aaaaa bbbbb ccccc ddddd eeeee"; // Random().nextInt(10000).toString();
|
// "00000 11111 22222 33333 44444 55555 66666 77777 88888 99999 aaaaa bbbbb ccccc ddddd eeeee"; // Random().nextInt(10000).toString();
|
||||||
final Strategy strategy = Strategy.P2P_STAR;
|
final Strategy strategy = Strategy.P2P_STAR;
|
||||||
Map<String, ConnectionInfo> endpointMap = Map();
|
Map<String, ConnectionInfo> endpointMap = Map();
|
||||||
static String _userName = "";
|
|
||||||
File? tempFile; //reference to the file currently being transferred
|
|
||||||
Map<int, String> map =
|
|
||||||
Map(); //store filename mapped to corresponding payloadId
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
@ -62,28 +57,7 @@ class _MyBodyState extends State<Body> {
|
|||||||
child: ListView(
|
child: ListView(
|
||||||
children: <Widget>[
|
children: <Widget>[
|
||||||
Text("User Name: " + userName),
|
Text("User Name: " + userName),
|
||||||
Wrap(
|
Divider(),
|
||||||
children: <Widget>[
|
|
||||||
ElevatedButton(
|
|
||||||
child: Text("Restart"),
|
|
||||||
onPressed: () async {
|
|
||||||
try {
|
|
||||||
stopAll();
|
|
||||||
startAdvertising();
|
|
||||||
startDiscovery();
|
|
||||||
} catch (exception) {
|
|
||||||
showSnackbar(exception);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
),
|
|
||||||
ElevatedButton(
|
|
||||||
child: Text("Stop"),
|
|
||||||
onPressed: () async {
|
|
||||||
stopAll();
|
|
||||||
},
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
Text("Number of connected devices: ${endpointMap.length}"),
|
Text("Number of connected devices: ${endpointMap.length}"),
|
||||||
ElevatedButton(
|
ElevatedButton(
|
||||||
child: Text("Stop All Endpoints"),
|
child: Text("Stop All Endpoints"),
|
||||||
@ -111,26 +85,6 @@ class _MyBodyState extends State<Body> {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
ElevatedButton(
|
|
||||||
child: Text("Send File Payload"),
|
|
||||||
onPressed: () async {
|
|
||||||
PickedFile? file =
|
|
||||||
await ImagePicker().getImage(source: ImageSource.gallery);
|
|
||||||
|
|
||||||
if (file == null) return;
|
|
||||||
|
|
||||||
for (MapEntry<String, ConnectionInfo> m
|
|
||||||
in endpointMap.entries) {
|
|
||||||
int payloadId =
|
|
||||||
await Nearby().sendFilePayload(m.key, file.path);
|
|
||||||
showSnackbar("Sending file to ${m.key}");
|
|
||||||
Nearby().sendBytesPayload(
|
|
||||||
m.key,
|
|
||||||
Uint8List.fromList(
|
|
||||||
"$payloadId:${file.path.split('/').last}".codeUnits));
|
|
||||||
}
|
|
||||||
},
|
|
||||||
),
|
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@ -143,6 +97,10 @@ class _MyBodyState extends State<Body> {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void getUUID() async {
|
||||||
|
await FlutterUdid.consistentUdid;
|
||||||
|
}
|
||||||
|
|
||||||
void stopAll() async {
|
void stopAll() async {
|
||||||
await Nearby().stopAllEndpoints();
|
await Nearby().stopAllEndpoints();
|
||||||
await Nearby().stopAdvertising();
|
await Nearby().stopAdvertising();
|
||||||
@ -228,27 +186,6 @@ class _MyBodyState extends State<Body> {
|
|||||||
if (payload.type == PayloadType.BYTES) {
|
if (payload.type == PayloadType.BYTES) {
|
||||||
String str = String.fromCharCodes(payload.bytes!);
|
String str = String.fromCharCodes(payload.bytes!);
|
||||||
showSnackbar(endid + ": " + str);
|
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) {
|
onPayloadTransferUpdate: (endid, payloadTransferUpdate) {
|
||||||
@ -260,15 +197,6 @@ class _MyBodyState extends State<Body> {
|
|||||||
} else if (payloadTransferUpdate.status == PayloadStatus.SUCCESS) {
|
} else if (payloadTransferUpdate.status == PayloadStatus.SUCCESS) {
|
||||||
showSnackbar(
|
showSnackbar(
|
||||||
"$endid success, total bytes = ${payloadTransferUpdate.totalBytes}");
|
"$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] = "";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user