From 27bb819cc9d359a6d475a3ba2b7cc8755f33257b Mon Sep 17 00:00:00 2001 From: Thomas Atkins Date: Sat, 10 Apr 2021 15:19:32 -0500 Subject: [PATCH] working on bluetooth --- loc_chain_app/lib/main.dart | 2 +- loc_chain_app/lib/pages/bluetooth.dart | 4 +-- loc_chain_app/lib/util/bluetooth.dart | 39 +++++++++++++++++++++++++- 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/loc_chain_app/lib/main.dart b/loc_chain_app/lib/main.dart index 1b1e62b..07e1381 100644 --- a/loc_chain_app/lib/main.dart +++ b/loc_chain_app/lib/main.dart @@ -6,7 +6,7 @@ import 'dart:math'; void main() async { runApp(App()); final prefs = await SharedPreferences.getInstance(); - final id = prefs.getString('id') ?? '0'; + final userName = prefs.getString('userName') ?? '0'; if (id == '0') { prefs.setString('id', Random().nextInt(10000).toString()); } diff --git a/loc_chain_app/lib/pages/bluetooth.dart b/loc_chain_app/lib/pages/bluetooth.dart index c2fa57b..cbf1c76 100644 --- a/loc_chain_app/lib/pages/bluetooth.dart +++ b/loc_chain_app/lib/pages/bluetooth.dart @@ -31,10 +31,10 @@ class _BluetoothPageState extends State { child: Text("GetID"), onPressed: () async { final prefs = await SharedPreferences.getInstance(); - final id = prefs.getString('id'); + final userName = prefs.getString('userName'); ScaffoldMessenger.of(context) - .showSnackBar(SnackBar(content: Text("Id is $id"))); + .showSnackBar(SnackBar(content: Text("Id is $userName"))); }, ), ), diff --git a/loc_chain_app/lib/util/bluetooth.dart b/loc_chain_app/lib/util/bluetooth.dart index c5ad77a..633ea7f 100644 --- a/loc_chain_app/lib/util/bluetooth.dart +++ b/loc_chain_app/lib/util/bluetooth.dart @@ -7,4 +7,41 @@ import 'package:nearby_connections/nearby_connections.dart'; import 'package:shared_preferences/shared_preferences.dart'; -class Connect {} +class Connect { + void startConnect() async { + final prefs = await SharedPreferences.getInstance(); + final userName = prefs.getString('userName') ?? " "; + final Strategy strategy = Strategy.P2P_STAR; + final serviceId = "com.yourdomain.appname"; + + try { + bool advertise = await Nearby().startAdvertising( + userName, + strategy, + onConnectionInitiated: (String id, ConnectionInfo info) { + // Called whenever a discoverer requests connection + }, + onConnectionResult: (String id, Status status) { + // Called when connection is accepted/rejected + }, + onDisconnected: (String id) { + // Callled whenever a discoverer disconnects from advertiser + }, + serviceId: serviceId, // uniquely identifies your app + ); + bool discovery = await Nearby().startDiscovery( + userName, + strategy, + onEndpointFound: (String id, String userName, String serviceId) { + // called when an advertiser is found + }, + onEndpointLost: (String? id) { + //called when an advertiser is lost (only if we weren't connected to it ) + }, + serviceId: serviceId, // uniquely identifies your app + ); + } catch (exception) { + // platform exceptions like unable to start bluetooth or insufficient permissions + } + } +}