From 13eeafabaa4e9a99c79185977bd85cf5f35b563b Mon Sep 17 00:00:00 2001 From: Thomas Atkins Date: Sat, 10 Apr 2021 16:01:14 -0500 Subject: [PATCH] update bluetooth --- loc_chain_app/lib/util/bluetooth.dart | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/loc_chain_app/lib/util/bluetooth.dart b/loc_chain_app/lib/util/bluetooth.dart index 633ea7f..568c48e 100644 --- a/loc_chain_app/lib/util/bluetooth.dart +++ b/loc_chain_app/lib/util/bluetooth.dart @@ -8,29 +8,44 @@ import 'package:nearby_connections/nearby_connections.dart'; import 'package:shared_preferences/shared_preferences.dart'; class Connect { + final serviceId = "com.yourdomain.appname"; + final Strategy strategy = Strategy.P2P_STAR; + late final _userName; + + Map endpointMap = Map(); + + Connect() { + SharedPreferences.getInstance() + .then((s) => _userName = s.getString('userName') ?? '0'); + } + void startConnect() async { - final prefs = await SharedPreferences.getInstance(); - final userName = prefs.getString('userName') ?? " "; - final Strategy strategy = Strategy.P2P_STAR; - final serviceId = "com.yourdomain.appname"; + // final prefs = await SharedPreferences.getInstance(); + // final userName = prefs.getString('userName') ?? " "; try { bool advertise = await Nearby().startAdvertising( - userName, + _userName, strategy, onConnectionInitiated: (String id, ConnectionInfo info) { // Called whenever a discoverer requests connection + // + // onConnectionInit }, onConnectionResult: (String id, Status status) { // Called when connection is accepted/rejected + // if connection is accepted send the transaction + // + // }, onDisconnected: (String id) { // Callled whenever a discoverer disconnects from advertiser }, serviceId: serviceId, // uniquely identifies your app ); + bool discovery = await Nearby().startDiscovery( - userName, + _userName, strategy, onEndpointFound: (String id, String userName, String serviceId) { // called when an advertiser is found