diff --git a/lib/pages/medical/my_trackers/blood_glucose_ble.dart b/lib/pages/medical/my_trackers/blood_glucose_ble.dart new file mode 100644 index 00000000..66df5fe4 --- /dev/null +++ b/lib/pages/medical/my_trackers/blood_glucose_ble.dart @@ -0,0 +1,171 @@ +import 'dart:async'; + +import 'package:diplomaticquarterapp/theme/colors.dart'; +import 'package:diplomaticquarterapp/uitl/ble_utils.dart'; +import 'package:diplomaticquarterapp/uitl/bluetooth_off.dart'; +import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart'; +import 'package:diplomaticquarterapp/widgets/buttons/defaultButton.dart'; +import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_blue_plus/flutter_blue_plus.dart'; +import 'package:permission_handler/permission_handler.dart'; + +class BloodGlucoseBLE extends StatefulWidget { + @override + State createState() => _BloodGlucoseBLEState(); +} + +class _BloodGlucoseBLEState extends State { + String connectionStatus = "disconnected"; + String currentBloodGlucose = "0.0"; + + BluetoothDevice currentConnectedDevice; + + StreamSubscription bleDevicesStream; + StreamSubscription bloodGlucoseValuesStream; + + bool isDataMeasured = false; + + @override + void dispose() { + super.dispose(); + if (bleDevicesStream != null) bleDevicesStream.cancel(); + if (bloodGlucoseValuesStream != null) bloodGlucoseValuesStream.cancel(); + if (currentConnectedDevice != null) currentConnectedDevice.disconnect(); + } + + @override + Widget build(BuildContext context) { + return AppScaffold( + appBarTitle: TranslationBase.of(context).bloodSugar, + showNewAppBar: true, + isShowDecPage: true, + showNewAppBarTitle: true, + backgroundColor: Color(0xffF8F8F8), + body: SingleChildScrollView( + child: StreamBuilder( + stream: FlutterBluePlus.adapterState, + initialData: BluetoothAdapterState.unknown, + builder: (c, snapshot) { + final adapterState = snapshot.data; + if (adapterState == BluetoothAdapterState.on) { + return Container( + margin: EdgeInsets.only(top: 200.0, left: 50.0, right: 50.0), + child: Column( + children: [ + Center( + child: DefaultButton( + TranslationBase.of(context).start.toUpperCase(), + () { + checkBLEPermissions(); + }, + color: CustomColors.green, + ), + ), + SizedBox( + height: 50.0, + ), + Text("Connection state: $connectionStatus"), + SizedBox( + height: 50.0, + ), + Text("Current Temp: $currentBloodGlucose" + " mg/dL"), + ], + ), + ); + } else { + FlutterBluePlus.stopScan(); + return SizedBox(height: 300.0, child: BluetoothOffScreen(adapterState: adapterState)); + } + }), + ), + ); + } + + void checkBLEPermissions() { + [Permission.location, Permission.storage, Permission.bluetooth, Permission.bluetoothConnect, Permission.bluetoothScan].request().then((status) { + startBLEConnection(); + }); + } + + void startBLEConnection() { + if (FlutterBluePlus.isScanningNow == false) { + setState(() { + connectionStatus = "Connecting..."; + }); + + bleDevicesStream = FlutterBluePlus.scanResults.listen( + (results) { + List blueToothDevices = results; + blueToothDevices.forEach((element) async { + if (element.device.localName.isNotEmpty) { + if (element.device.localName.toLowerCase() == "samico gl") { + element.device.connectionState.listen((BluetoothConnectionState state) async { + setState(() { + connectionStatus = state.toString(); + }); + if (state == BluetoothConnectionState.disconnected) { + // typically, start a periodic timer that tries to periodically reconnect. + // Note: you must always re-discover services after disconnection! + } + if (state == BluetoothConnectionState.connected) { + currentConnectedDevice = element.device; + bleDevicesStream.cancel(); + List services = await element.device.discoverServices(); + services.forEach((service) { + if (service.serviceUuid.toString().toLowerCase() == BLEUtils.BLOOD_GLUCOSE_SERVICE) { + print(service.serviceUuid); + service.characteristics.forEach((characteristic) async { + if (characteristic.characteristicUuid.toString().toLowerCase() == BLEUtils.BLOOD_GLUCOSE_CHARACTERISTIC) { + print(characteristic.characteristicUuid); + bloodGlucoseValuesStream = characteristic.onValueReceived.listen((event) { + print("onValueReceived Stream"); + print(event); + setState(() { + if (!isDataMeasured) currentBloodGlucose = getBloodGlucoseMeasurements(event)[0]; + // currentTempInCelsius = convertIntListToHex(event); + // String currentTempInFahrenheit = ((num.parse(currentTempInCelsius) * 1.8) + 32).toStringAsFixed(1); + // currentTempInCelsius = currentTempInCelsius + "\u2103" + " / " + currentTempInFahrenheit + "\u2109"; + }); + }); + await characteristic.setNotifyValue(true); + } + }); + return true; + } + }); + } + }); + await element.device.connect(timeout: Duration(seconds: 35)); + return true; + } + } + }); + }, + // onError(e) => print(e); + ); + + FlutterBluePlus.startScan(timeout: const Duration(seconds: 5), androidUsesFineLocation: false); + } + } + + List getBloodGlucoseMeasurements(List byteArray) { + List results = []; + + //[85, 6, 2, 0, 4, 99] + //[85, 12, 3, 23, 9, 12, 12, 25, 0, 75, 0, 2] + + if (byteArray.length == 6) { + int secsRemaining = 0; + secsRemaining = byteArray[4]; + results.add("Measuring: $secsRemaining"); + } else { + num measuredValue = byteArray[9]; + results.add("Measured: $measuredValue"); + isDataMeasured = true; + bloodGlucoseValuesStream.cancel(); + } + + return results; + } +} diff --git a/lib/pages/medical/my_trackers/blood_pressure/BloodPressureBLE.dart b/lib/pages/medical/my_trackers/blood_pressure/BloodPressureBLE.dart index 2dee12d8..75d61dcc 100644 --- a/lib/pages/medical/my_trackers/blood_pressure/BloodPressureBLE.dart +++ b/lib/pages/medical/my_trackers/blood_pressure/BloodPressureBLE.dart @@ -99,7 +99,8 @@ class _BloodPressureBLEState extends State { }); FlutterBluePlus.startScan(timeout: const Duration(seconds: 5), androidUsesFineLocation: false).then((value) { - List blueToothDevices = value; + // List blueToothDevices = value; + List blueToothDevices = []; blueToothDevices.forEach((element) async { if (element.device.localName.isNotEmpty) { if (element.device.localName.toLowerCase() == "bpm") { diff --git a/lib/pages/medical/my_trackers/ecg_ble.dart b/lib/pages/medical/my_trackers/ecg_ble.dart index 29e4726a..7e138864 100644 --- a/lib/pages/medical/my_trackers/ecg_ble.dart +++ b/lib/pages/medical/my_trackers/ecg_ble.dart @@ -94,7 +94,8 @@ class _ECG_BLEState extends State { }); FlutterBluePlus.startScan(timeout: const Duration(seconds: 5), androidUsesFineLocation: false).then((value) { - List blueToothDevices = value; + List blueToothDevices = []; + // List blueToothDevices = value; blueToothDevices.forEach((element) async { if (element.device.localName.isNotEmpty) { if (element.device.localName.toLowerCase() == "pm101897") { diff --git a/lib/pages/medical/my_trackers/my_trackers.dart b/lib/pages/medical/my_trackers/my_trackers.dart index 0c202e93..5f621360 100644 --- a/lib/pages/medical/my_trackers/my_trackers.dart +++ b/lib/pages/medical/my_trackers/my_trackers.dart @@ -1,5 +1,6 @@ import 'package:diplomaticquarterapp/core/model/ImagesInfo.dart'; import 'package:diplomaticquarterapp/pages/medical/my_trackers/Weight/WeightHomePage.dart'; +import 'package:diplomaticquarterapp/pages/medical/my_trackers/blood_glucose_ble.dart'; import 'package:diplomaticquarterapp/pages/medical/my_trackers/blood_pressure/BloodPressureBLE.dart'; import 'package:diplomaticquarterapp/pages/medical/my_trackers/blood_suger/blood_sugar_home_page.dart'; import 'package:diplomaticquarterapp/pages/medical/my_trackers/ecg_ble.dart'; @@ -126,6 +127,19 @@ class MyTrackers extends StatelessWidget { height: 45.0, ), ), + InkWell( + onTap: () { + Navigator.push(context, FadePage(page: BloodGlucoseBLE())); + }, + child: MedicalProfileItem( + title: "Blood Glucose BLE", + imagePath: 'assets/tracker/blood-pressure.png', + subTitle: null, + isPngImage: true, + width: 45.0, + height: 45.0, + ), + ), ], ), ), diff --git a/lib/pages/medical/my_trackers/spirometer.dart b/lib/pages/medical/my_trackers/spirometer.dart index a00f8e7b..77925ac5 100644 --- a/lib/pages/medical/my_trackers/spirometer.dart +++ b/lib/pages/medical/my_trackers/spirometer.dart @@ -92,7 +92,8 @@ class _SpirometerBLEState extends State { }); FlutterBluePlus.startScan(timeout: const Duration(seconds: 5), androidUsesFineLocation: false).then((value) { - List blueToothDevices = value; + List blueToothDevices = []; + // List blueToothDevices = value; blueToothDevices.forEach((element) async { if (element.device.localName.isNotEmpty) { if (element.device.localName.toLowerCase() == "ble-msa") { diff --git a/lib/pages/medical/my_trackers/temperature.dart b/lib/pages/medical/my_trackers/temperature.dart index f4afd9ce..b6f61779 100644 --- a/lib/pages/medical/my_trackers/temperature.dart +++ b/lib/pages/medical/my_trackers/temperature.dart @@ -21,7 +21,6 @@ class _TemperatureHomePageState extends State { BluetoothDevice currentConnectedDevice; - @override void dispose() { super.dispose(); @@ -89,7 +88,8 @@ class _TemperatureHomePageState extends State { }); FlutterBluePlus.startScan(timeout: const Duration(seconds: 5), androidUsesFineLocation: false).then((value) { - List blueToothDevices = value; + List blueToothDevices = []; + // List blueToothDevices = value; blueToothDevices.forEach((element) async { if (element.device.localName.isNotEmpty) { if (element.device.localName.toLowerCase() == "temp") { diff --git a/lib/uitl/ble_utils.dart b/lib/uitl/ble_utils.dart index 4f6059b8..c771ba23 100644 --- a/lib/uitl/ble_utils.dart +++ b/lib/uitl/ble_utils.dart @@ -19,4 +19,8 @@ class BLEUtils { static const String ECG_READ_CHARACTERISTIC = "49535343-1e4d-4bd9-ba61-23c647249616"; //49535343-1e4d-4bd9-ba61-23c647249616 static const String ECG_WRITE_CHARACTERISTIC = "49535343-8841-43f4-a8d4-ecbe34729bb3"; //49535343-8841-43f4-a8d4-ecbe34729bb3 + //Blood Glucose + 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