if you discconnect the bluetooth device then start it up again and connect to it it…
- User
if you discconnect the bluetooth device then start it up again and connect to it it sends 2x requiest so and 3x if you do it again:
void connectToDevice(BuildContext context, String macAddress) async { if (isConnecting) return;
try { print("Connecting to device…"); isConnecting = true;
codetargetDevice = BluetoothDevice(remoteId: DeviceIdentifier(macAddress)); await targetDevice!.connect(); await handleBluetoothDevice(); print("Happening"); isConnecting = false; Navigator.push( context, MaterialPageRoute( builder: (context) => BluetoothScreen(takePictureScreenState: this), ), );} catch (e) { print("Connection error: $e"); isConnecting = false;
codeif (canPop(context)) { Navigator.pop(context); }}
connectionStateSubscription = targetDevice!.connectionState.listen( (BluetoothConnectionState state) { if (state == BluetoothConnectionState.disconnected && canPop(context)) { Navigator.pop(context); } }, ); }
Future handleBluetoothDevice() async { if (targetDevice == null) return;
codeList<BluetoothService> services = await targetDevice!.discoverServices(subscribeToServicesChanged: false); services.forEach((service) { service.characteristics.forEach((characteristic) { if (characteristic.uuid.toString() == "06d1e5e7-79ad-4a71-8faa-373789f7d93c") { print(service); targetCharacteristic = characteristic; characteristic.setNotifyValue(true); print(characteristic); characteristic.lastValueStream.listen((value) { handleReceivedData(value); print("foundit"); }); print("Found it"); } }); });}
void handleReceivedData(List data) { String dataString = String.fromCharCodes(data); try { String numberString = RegExp(r'[0-9.]+').firstMatch(dataString)!.group(0)!; print("We are now here"); print(numberString); captureAndSendImage(numberString); } catch (e) { } }
- Assistant