2021-04-11 10:21:40 +00:00
|
|
|
import 'dart:io';
|
|
|
|
import 'dart:typed_data';
|
2021-04-10 15:26:20 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2021-04-11 02:52:37 +00:00
|
|
|
import 'package:flutter_udid/flutter_udid.dart';
|
|
|
|
import 'package:loc_chain_app/util/transaction_manager.dart';
|
2021-04-11 10:21:40 +00:00
|
|
|
import 'package:nearby_connections/nearby_connections.dart';
|
2021-04-10 15:26:20 +00:00
|
|
|
|
|
|
|
class HomePage extends StatefulWidget {
|
2021-04-10 17:58:52 +00:00
|
|
|
HomePage({Key? key, required this.title}) : super(key: key);
|
2021-04-10 15:26:20 +00:00
|
|
|
final String title;
|
|
|
|
|
|
|
|
@override
|
|
|
|
_HomePageState createState() => _HomePageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _HomePageState extends State<HomePage> {
|
2021-04-11 10:21:40 +00:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
WidgetsBinding.instance!.addPostFrameCallback((_) {
|
|
|
|
getUUID();
|
|
|
|
startAdvertising();
|
|
|
|
startDiscovery();
|
2021-04-10 15:26:20 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-11 10:21:40 +00:00
|
|
|
static late String userName;
|
|
|
|
final Strategy strategy = Strategy.P2P_STAR;
|
|
|
|
Map<String, ConnectionInfo> endpointMap = Map();
|
|
|
|
|
2021-04-10 15:26:20 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2021-04-11 10:21:40 +00:00
|
|
|
return Center(
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.all(8.0),
|
|
|
|
child: ListView(
|
|
|
|
children: <Widget>[
|
|
|
|
Text("User Name: " + userName),
|
|
|
|
Divider(),
|
|
|
|
Text("Number of connected devices: ${endpointMap.length}"),
|
|
|
|
ElevatedButton(
|
|
|
|
child: Text("Stop All Endpoints"),
|
|
|
|
onPressed: () async {
|
|
|
|
await Nearby().stopAllEndpoints();
|
|
|
|
setState(() {
|
|
|
|
endpointMap.clear();
|
|
|
|
});
|
2021-04-11 02:52:37 +00:00
|
|
|
},
|
2021-04-11 10:21:40 +00:00
|
|
|
),
|
|
|
|
Divider(),
|
|
|
|
Text(
|
|
|
|
"Sending Data",
|
|
|
|
),
|
|
|
|
ElevatedButton(
|
|
|
|
child: Text("Send Bytes Payload"),
|
|
|
|
onPressed: () async {
|
|
|
|
sendPayload();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2021-04-10 15:26:20 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2021-04-11 02:52:37 +00:00
|
|
|
|
2021-04-11 10:21:40 +00:00
|
|
|
String genPayload(ConnectionInfo value) {
|
|
|
|
print("Sender ID: $userName");
|
|
|
|
print("Receiver ID: ${value.endpointName}");
|
|
|
|
|
|
|
|
var dateTime = DateTime.now();
|
|
|
|
print('Milliseconds since Epoch: ${dateTime.millisecondsSinceEpoch}');
|
|
|
|
|
|
|
|
String payload = userName +
|
|
|
|
":" +
|
|
|
|
value.endpointName +
|
|
|
|
":" +
|
|
|
|
dateTime.millisecondsSinceEpoch.toString();
|
|
|
|
return payload;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sendPayload() {
|
|
|
|
endpointMap.forEach((key, value) {
|
|
|
|
String a = genPayload(value);
|
|
|
|
|
|
|
|
showSnackbar("Sending $a to ${value.endpointName}, id: $key");
|
|
|
|
Nearby().sendBytesPayload(key, Uint8List.fromList(a.codeUnits));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-11 02:52:37 +00:00
|
|
|
void showSnackbar(dynamic a) {
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
|
|
|
content: Text(a.toString()),
|
|
|
|
));
|
|
|
|
}
|
2021-04-11 05:59:30 +00:00
|
|
|
|
2021-04-11 10:21:40 +00:00
|
|
|
void getUUID() async {
|
|
|
|
userName = await FlutterUdid.consistentUdid;
|
|
|
|
}
|
|
|
|
|
|
|
|
void stopAll() async {
|
|
|
|
await Nearby().stopAllEndpoints();
|
|
|
|
await Nearby().stopAdvertising();
|
|
|
|
await Nearby().stopDiscovery();
|
|
|
|
setState(() {});
|
|
|
|
}
|
|
|
|
|
|
|
|
void startDiscovery() async {
|
|
|
|
try {
|
|
|
|
userName = await FlutterUdid.consistentUdid;
|
|
|
|
bool a = await Nearby().startDiscovery(
|
|
|
|
userName,
|
|
|
|
strategy,
|
|
|
|
onEndpointFound: (id, name, serviceId) {
|
|
|
|
print("userName = $userName");
|
|
|
|
print("id = $id");
|
|
|
|
|
|
|
|
Nearby().requestConnection(
|
|
|
|
userName,
|
|
|
|
id,
|
|
|
|
onConnectionInitiated: (id, info) {
|
|
|
|
onConnectionInit(id, info);
|
|
|
|
sleep(Duration(microseconds: 500));
|
|
|
|
sendPayload();
|
|
|
|
},
|
|
|
|
onConnectionResult: (id, status) {
|
|
|
|
showSnackbar(status);
|
|
|
|
},
|
|
|
|
onDisconnected: (id) {
|
|
|
|
setState(() {
|
|
|
|
endpointMap.remove(id);
|
|
|
|
});
|
|
|
|
showSnackbar(
|
|
|
|
"Disconnected from: ${endpointMap[id]!.endpointName}, id $id");
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
onEndpointLost: (id) {
|
|
|
|
showSnackbar(
|
|
|
|
"Lost discovered Endpoint: ${endpointMap[id]!.endpointName}, id $id");
|
|
|
|
},
|
|
|
|
);
|
|
|
|
showSnackbar("DISCOVERING: " + a.toString());
|
|
|
|
} catch (e) {
|
|
|
|
showSnackbar(e);
|
|
|
|
}
|
|
|
|
setState(() {});
|
|
|
|
}
|
|
|
|
|
|
|
|
void startAdvertising() async {
|
|
|
|
try {
|
|
|
|
userName = await FlutterUdid.consistentUdid;
|
|
|
|
|
|
|
|
bool a = await Nearby().startAdvertising(
|
|
|
|
userName,
|
|
|
|
strategy,
|
|
|
|
onConnectionInitiated: (id, info) {
|
|
|
|
onConnectionInit(id, info);
|
|
|
|
sleep(Duration(microseconds: 500));
|
|
|
|
sendPayload();
|
|
|
|
}, //advertisingOnConnectionInit,
|
|
|
|
onConnectionResult: (id, status) {
|
|
|
|
print("userName = $userName");
|
|
|
|
print("id = $id");
|
|
|
|
print(status);
|
|
|
|
showSnackbar(status);
|
|
|
|
},
|
|
|
|
onDisconnected: (id) {
|
|
|
|
showSnackbar(
|
|
|
|
"Disconnected: ${endpointMap[id]!.endpointName}, id $id");
|
|
|
|
setState(() {
|
|
|
|
endpointMap.remove(id);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
|
|
|
showSnackbar("ADVERTISING: " + a.toString());
|
|
|
|
} catch (exception) {
|
|
|
|
showSnackbar(exception);
|
|
|
|
}
|
|
|
|
setState(() {});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Called upon Connection request (on both devices)
|
|
|
|
/// Both need to accept connection to start sending/receiving
|
|
|
|
void onConnectionInit(String id, ConnectionInfo info) {
|
|
|
|
setState(() {
|
|
|
|
endpointMap[id] = info;
|
|
|
|
});
|
|
|
|
Nearby().acceptConnection(
|
|
|
|
id,
|
|
|
|
onPayLoadRecieved: (endid, payload) async {
|
|
|
|
if (payload.type == PayloadType.BYTES) {
|
|
|
|
String str = String.fromCharCodes(payload.bytes!);
|
|
|
|
|
|
|
|
// first device recives here
|
|
|
|
print("Receiving:");
|
|
|
|
print("${str.split(":")}");
|
|
|
|
|
|
|
|
showSnackbar(endid + ": " + str);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
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}");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2021-04-10 15:26:20 +00:00
|
|
|
}
|