From 3f767f738a11a2f0d8fad3d3f89c35c90ba0fd0c Mon Sep 17 00:00:00 2001 From: haroon amjad Date: Sun, 17 Sep 2023 11:36:09 +0300 Subject: [PATCH] Weight scale in progress --- lib/config/config.dart | 4 +-- lib/pages/medical/my_trackers/ecg_ble.dart | 34 ++++++++++++++++------ lib/uitl/ble_utils.dart | 7 ++++- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/lib/config/config.dart b/lib/config/config.dart index e93af7c8..5c9912e4 100644 --- a/lib/config/config.dart +++ b/lib/config/config.dart @@ -20,8 +20,8 @@ var PACKAGES_ORDERS = '/api/orders'; var PACKAGES_ORDER_HISTORY = '/api/orders/items'; var PACKAGES_TAMARA_OPT = '/api/orders/paymentoptions/tamara'; // var BASE_URL = 'http://10.50.100.198:2018/'; -var BASE_URL = 'https://uat.hmgwebservices.com/'; -// var BASE_URL = 'https://hmgwebservices.com/'; +// var BASE_URL = 'https://uat.hmgwebservices.com/'; +var BASE_URL = 'https://hmgwebservices.com/'; // var BASE_URL = 'https://orash.cloudsolutions.com.sa/'; // var BASE_URL = 'https://vidauat.cloudsolutions.com.sa/'; diff --git a/lib/pages/medical/my_trackers/ecg_ble.dart b/lib/pages/medical/my_trackers/ecg_ble.dart index 7e138864..3acf33b8 100644 --- a/lib/pages/medical/my_trackers/ecg_ble.dart +++ b/lib/pages/medical/my_trackers/ecg_ble.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:diplomaticquarterapp/theme/colors.dart'; import 'package:diplomaticquarterapp/uitl/ble_utils.dart'; import 'package:diplomaticquarterapp/uitl/bluetooth_off.dart'; @@ -20,17 +22,19 @@ class _ECG_BLEState extends State { BluetoothCharacteristic ecgWriteCharacteristic; + StreamSubscription bleDevicesStream; + @override void dispose() { super.dispose(); + if (bleDevicesStream != null) bleDevicesStream.cancel(); if (currentConnectedDevice != null) currentConnectedDevice.disconnect(); } - @override void initState() { super.initState(); - FlutterBluePlus.setLogLevel(LogLevel.verbose, color:false); + FlutterBluePlus.setLogLevel(LogLevel.verbose, color: false); } @override @@ -93,14 +97,14 @@ class _ECG_BLEState extends State { connectionStatus = "Connecting..."; }); - FlutterBluePlus.startScan(timeout: const Duration(seconds: 5), androidUsesFineLocation: false).then((value) { - List blueToothDevices = []; - // List blueToothDevices = value; + bleDevicesStream = FlutterBluePlus.scanResults.listen((results) { + List blueToothDevices = results; blueToothDevices.forEach((element) async { if (element.device.localName.isNotEmpty) { if (element.device.localName.toLowerCase() == "pm101897") { + bleDevicesStream.cancel(); element.device.connectionState.listen((BluetoothConnectionState state) async { - if(mounted) { + if (mounted) { setState(() { connectionStatus = state.toString(); }); @@ -110,7 +114,16 @@ class _ECG_BLEState extends State { // Note: you must always re-discover services after disconnection! } if (state == BluetoothConnectionState.connected) { + if (FlutterBluePlus.isScanningNow) { + FlutterBluePlus.stopScan(); + } currentConnectedDevice = element.device; + // currentConnectedDevice.clearGattCache(); + // currentConnectedDevice.requestConnectionPriority(connectionPriorityRequest: ConnectionPriority.high); + // currentConnectedDevice.requestMtu(512); + // currentConnectedDevice.mtu.first.then((value) { + // print("MTU Size: $value"); + // }); List services = await element.device.discoverServices(); services.forEach((service) { if (service.serviceUuid.toString().toLowerCase() == BLEUtils.ECG_SERVICE) { @@ -122,13 +135,13 @@ class _ECG_BLEState extends State { print("onValueReceived Stream"); print(event); }); - await characteristic.setNotifyValue(true); + if (!characteristic.isNotifying) await characteristic.setNotifyValue(true); } if (characteristic.characteristicUuid.toString().toLowerCase() == BLEUtils.ECG_WRITE_CHARACTERISTIC) { - print(characteristic.characteristicUuid); + print("Write Characteristic: ${characteristic.characteristicUuid}"); ecgWriteCharacteristic = characteristic; - ecgWriteCharacteristic.write([0x90], allowLongWrite: true); + await ecgWriteCharacteristic.write([0x83]); } }); return true; @@ -142,6 +155,9 @@ class _ECG_BLEState extends State { } }); }); + FlutterBluePlus.startScan(timeout: const Duration(seconds: 15), androidUsesFineLocation: false).then((value) { + print("Scan Finished"); + }); } } } diff --git a/lib/uitl/ble_utils.dart b/lib/uitl/ble_utils.dart index c771ba23..8e9a4a6a 100644 --- a/lib/uitl/ble_utils.dart +++ b/lib/uitl/ble_utils.dart @@ -23,4 +23,9 @@ class BLEUtils { static const String BLOOD_GLUCOSE_SERVICE = "0000fff0-0000-1000-8000-00805f9b34fb"; static const String BLOOD_GLUCOSE_CHARACTERISTIC = "0000fff4-0000-1000-8000-00805f9b34fb"; -} \ No newline at end of file + // Weight Scale + static const String WEIGHT_MEASUREMENT_CHARACTERISTIC_FFF1 = "0000fff1-0000-1000-8000-00805f9b34fb"; + static const String WEIGHT_MEASUREMENT_CHARACTERISTIC_FFF2 = "0000fff2-0000-1000-8000-00805f9b34fb"; + static const String WEIGHT_MEASUREMENT_CHARACTERISTIC_FFF3 = "0000fff3-0000-1000-8000-00805f9b34fb"; + static const String WEIGHT_MEASUREMENT_SERVICE = "0000fff0-0000-1000-8000-00805f9b34fb"; +}