diff --git a/lib/pages/medical/my_trackers/my_trackers.dart b/lib/pages/medical/my_trackers/my_trackers.dart index 5f621360..4b1904f3 100644 --- a/lib/pages/medical/my_trackers/my_trackers.dart +++ b/lib/pages/medical/my_trackers/my_trackers.dart @@ -6,6 +6,7 @@ import 'package:diplomaticquarterapp/pages/medical/my_trackers/blood_suger/blood import 'package:diplomaticquarterapp/pages/medical/my_trackers/ecg_ble.dart'; import 'package:diplomaticquarterapp/pages/medical/my_trackers/spirometer.dart'; import 'package:diplomaticquarterapp/pages/medical/my_trackers/temperature.dart'; +import 'package:diplomaticquarterapp/pages/medical/my_trackers/weight_scale_ble.dart'; import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart'; import 'package:diplomaticquarterapp/widgets/data_display/medical/medical_profile_item.dart'; import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart'; @@ -140,6 +141,19 @@ class MyTrackers extends StatelessWidget { height: 45.0, ), ), + InkWell( + onTap: () { + Navigator.push(context, FadePage(page: WeightScaleBLE())); + }, + child: MedicalProfileItem( + title: "Weight BLE", + imagePath: 'assets/tracker/weight.png', + subTitle: null, + isPngImage: true, + width: 45.0, + height: 45.0, + ), + ), ], ), ), diff --git a/lib/pages/medical/my_trackers/weight_scale_ble.dart b/lib/pages/medical/my_trackers/weight_scale_ble.dart new file mode 100644 index 00000000..dc2c0e82 --- /dev/null +++ b/lib/pages/medical/my_trackers/weight_scale_ble.dart @@ -0,0 +1,142 @@ +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 WeightScaleBLE extends StatefulWidget { + @override + State createState() => _WeightScaleBLEState(); +} + +class _WeightScaleBLEState extends State { + String connectionStatus = "disconnected"; + String currentBloodGlucose = "0.0"; + + BluetoothDevice currentConnectedDevice; + + StreamSubscription bleDevicesStream; + + @override + Widget build(BuildContext context) { + return AppScaffold( + appBarTitle: TranslationBase.of(context).weight, + 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() == "sdic") { + 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.WEIGHT_MEASUREMENT_SERVICE) { + print(service.serviceUuid); + service.characteristics.forEach((characteristic) async { + // if (characteristic.characteristicUuid.toString().toLowerCase() == BLEUtils.WEIGHT_MEASUREMENT_CHARACTERISTIC_FFF3) { + // print(characteristic.characteristicUuid); + // characteristic.onValueReceived.listen((event) { + // print("onValueReceived fff3 Stream"); + // print(event); + // }); + // await characteristic.setNotifyValue(true); + // } + if (characteristic.characteristicUuid.toString().toLowerCase() == BLEUtils.WEIGHT_MEASUREMENT_CHARACTERISTIC_FFF1) { + print(characteristic.characteristicUuid); + characteristic.onValueReceived.listen((event) { + print("onValueReceived fff1 Stream"); + print(event); + }); + 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); + } + } +}