Spirometer implementation

dev_3.3_BLE
haroon amjad 2 years ago
parent b183518a19
commit e282d73dcf

@ -17,6 +17,7 @@ import 'package:diplomaticquarterapp/pages/DrawerPages/family/my-family.dart';
import 'package:diplomaticquarterapp/pages/ToDoList/ToDo.dart'; import 'package:diplomaticquarterapp/pages/ToDoList/ToDo.dart';
import 'package:diplomaticquarterapp/pages/landing/home_page_2.dart'; import 'package:diplomaticquarterapp/pages/landing/home_page_2.dart';
import 'package:diplomaticquarterapp/pages/medical/medical_profile_page_new.dart'; import 'package:diplomaticquarterapp/pages/medical/medical_profile_page_new.dart';
import 'package:diplomaticquarterapp/pages/medical/my_trackers/spirometer.dart';
import 'package:diplomaticquarterapp/pages/medical/my_trackers/weight_scale_ble.dart'; import 'package:diplomaticquarterapp/pages/medical/my_trackers/weight_scale_ble.dart';
import 'package:diplomaticquarterapp/pages/videocall-webrtc-rnd/webrtc/start_video_call.dart'; import 'package:diplomaticquarterapp/pages/videocall-webrtc-rnd/webrtc/start_video_call.dart';
import 'package:diplomaticquarterapp/services/authentication/auth_provider.dart'; import 'package:diplomaticquarterapp/services/authentication/auth_provider.dart';
@ -568,7 +569,7 @@ class _LandingPageState extends State<LandingPage> with WidgetsBindingObserver {
}, },
onLoginClick: () { onLoginClick: () {
// login(); // login();
Navigator.push(context, FadePage(page: WeightScaleBLE())); Navigator.push(context, FadePage(page: SpirometerBLE()));
}, },
onMedicalFileClick: () { onMedicalFileClick: () {
changeCurrentTab(1); changeCurrentTab(1);

@ -19,16 +19,27 @@ class _SpirometerBLEState extends State<SpirometerBLE> {
String connectionStatus = "disconnected"; String connectionStatus = "disconnected";
BluetoothDevice currentConnectedDevice; BluetoothDevice currentConnectedDevice;
StreamSubscription bleDevicesStream;
BluetoothCharacteristic spirometerReadCharacteristicFf0a;
BluetoothCharacteristic spirometerWriteCharacteristicFf0b;
final bleConnectionStatus = ValueNotifier<String>("Disconnected");
Timer _timer; Timer _timer;
Timer _timerRead; Timer _timerRead;
bool isHistoryCommandWritten = false;
@override @override
void dispose() { void dispose() {
super.dispose(); super.dispose();
if (currentConnectedDevice != null) currentConnectedDevice.disconnect(); if (currentConnectedDevice != null) currentConnectedDevice.disconnect();
if (_timer != null && _timer.isActive) _timer.cancel(); if (_timer != null && _timer.isActive) _timer.cancel();
if (_timerRead != null && _timerRead.isActive) _timerRead.cancel(); if (_timerRead != null && _timerRead.isActive) _timerRead.cancel();
bleConnectionStatus.dispose();
if (bleDevicesStream != null) bleDevicesStream.cancel();
} }
@override @override
@ -36,7 +47,7 @@ class _SpirometerBLEState extends State<SpirometerBLE> {
return AppScaffold( return AppScaffold(
appBarTitle: TranslationBase.of(context).spirometer, appBarTitle: TranslationBase.of(context).spirometer,
showNewAppBar: true, showNewAppBar: true,
isShowDecPage: true, isShowDecPage: false,
showNewAppBarTitle: true, showNewAppBarTitle: true,
backgroundColor: Color(0xffF8F8F8), backgroundColor: Color(0xffF8F8F8),
body: SingleChildScrollView( body: SingleChildScrollView(
@ -62,7 +73,12 @@ class _SpirometerBLEState extends State<SpirometerBLE> {
SizedBox( SizedBox(
height: 50.0, height: 50.0,
), ),
Text("Connection state: $connectionStatus"), ValueListenableBuilder(
valueListenable: bleConnectionStatus,
builder: (context, value, _) {
return Text("Connection state: $value");
},
),
SizedBox( SizedBox(
height: 50.0, height: 50.0,
), ),
@ -87,77 +103,105 @@ class _SpirometerBLEState extends State<SpirometerBLE> {
void startBLEConnection() { void startBLEConnection() {
if (FlutterBluePlus.isScanningNow == false) { if (FlutterBluePlus.isScanningNow == false) {
setState(() { bleConnectionStatus.value = "Connecting...";
connectionStatus = "Connecting...";
});
FlutterBluePlus.startScan(timeout: const Duration(seconds: 5), androidUsesFineLocation: false).then((value) { bleDevicesStream = FlutterBluePlus.scanResults.listen(
List<ScanResult> blueToothDevices = []; (results) {
// List<ScanResult> blueToothDevices = value; List<ScanResult> blueToothDevices = results;
blueToothDevices.forEach((element) async { blueToothDevices.forEach(
(element) {
if (element.device.localName.isNotEmpty) { if (element.device.localName.isNotEmpty) {
if (element.device.localName.toLowerCase() == "ble-msa") { if (element.device.localName.toLowerCase() == "ble-msa") {
element.device.connectionState.listen((BluetoothConnectionState state) async { bleDevicesStream.cancel();
setState(() { element.device.connect(timeout: Duration(seconds: 5), autoConnect: false).then((value) async {
connectionStatus = state.toString(); bleConnectionStatus.value = "Connected...";
}); print("Device Connected-------");
if (state == BluetoothConnectionState.disconnected) {
// typically, start a periodic timer that tries to periodically reconnect.
// Note: you must always re-discover services after disconnection!
if (_timer != null && _timer.isActive) _timer.cancel();
if (_timerRead != null && _timerRead.isActive) _timerRead.cancel();
}
if (state == BluetoothConnectionState.connected) {
currentConnectedDevice = element.device; currentConnectedDevice = element.device;
FlutterBluePlus.stopScan();
List<int> BLEToMSAWriteCharacters = []; List<BluetoothService> services = await element.device.discoverServices(timeout: 5);
BLEToMSAWriteCharacters.add(int.parse("5506", radix: 16)); services.forEach(
(service) {
List<BluetoothService> services = await element.device.discoverServices();
services.forEach((service) {
if (service.serviceUuid.toString().toLowerCase() == BLEUtils.BLE_TO_MSA100_SERVICE) { if (service.serviceUuid.toString().toLowerCase() == BLEUtils.BLE_TO_MSA100_SERVICE) {
print(service.serviceUuid); print(service.serviceUuid);
service.characteristics.forEach((characteristic) async { service.characteristics.forEach(
if (characteristic.characteristicUuid.toString().toLowerCase() == BLEUtils.BLE_TO_MSA100_CHARACTERISTIC) { (characteristic) async {
print(characteristic.characteristicUuid); if (characteristic.characteristicUuid.toString().toLowerCase() == BLEUtils.BLE_TO_MSA100_READ_CHARACTERISTIC) {
_timer = Timer.periodic(Duration(seconds: 2), (Timer timer) { spirometerReadCharacteristicFf0a = characteristic;
characteristic.write(BLEToMSAWriteCharacters).then((value) { print(spirometerReadCharacteristicFf0a.characteristicUuid);
print("Characteristic response:"); print(spirometerReadCharacteristicFf0a.properties.toString());
}).catchError((err) {}); spirometerReadCharacteristicFf0a.onValueReceived.listen((event) async {
print("onValueReceived ff0a Stream");
print(event);
//Sample response
// dd14d93d5c1f01a302f8de3d5c1c017302
if (event[0] == 170) {
_timer.cancel();
// Write get history command to ff0b
if (!isHistoryCommandWritten) {
await Future.delayed(Duration(milliseconds: 1000)).then((value) async {
spirometerWriteCharacteristicFf0b.write([0x55, 0x01], withoutResponse: false).then((value) {
print("----ff0b get history command data written----");
isHistoryCommandWritten = true;
}); });
} });
if (characteristic.characteristicUuid.toString().toLowerCase() == BLEUtils.MSA100_TO_BLE_CHARACTERISTIC) {
print(characteristic.characteristicUuid); // Write get 1st history command to ff0b
_timerRead = Timer.periodic(Duration(seconds: 2), (Timer timer) { await Future.delayed(Duration(milliseconds: 1000)).then((value) async {
characteristic.read().then((value) { spirometerWriteCharacteristicFf0b.write([0x55, 0x02, 0x01, 0x00], withoutResponse: false).then((value) {
print("Characteristic Read value: "); print("----ff0b get 1st history command data written----");
print(value); isHistoryCommandWritten = true;
}); });
}); });
// characteristic.onValueReceived.listen((event) { // Write delete history command to ff0b
// print("onValueReceived Stream"); // await Future.delayed(Duration(milliseconds: 1000)).then((value) async {
// print(event); // spirometerWriteCharacteristicFf0b.write([0x55, 0x03], withoutResponse: false).then((value) {
// setState(() { // print("----ff0b delete history command data written----");
// currentTempInCelsius = convertIntListToHex(event); // isHistoryCommandWritten = true;
// String currentTempInFahrenheit = ((num.parse(currentTempInCelsius) * 1.8) + 32).toStringAsFixed(1);
// currentTempInCelsius = currentTempInCelsius + "\u2103" + " / " + currentTempInFahrenheit + "\u2109";
// }); // });
// }); // });
// await characteristic.setNotifyValue(true);
} }
});
return true;
} }
}); });
await Future.delayed(Duration(milliseconds: 1000)).then((value) async {
print("Delayed ff0a done");
if (!spirometerReadCharacteristicFf0a.isNotifying) await spirometerReadCharacteristicFf0a.setNotifyValue(true).catchError((err) {});
});
} }
if (characteristic.characteristicUuid.toString().toLowerCase() == BLEUtils.BLE_TO_MSA100_WRITE_CHARACTERISTIC) {
spirometerWriteCharacteristicFf0b = characteristic;
print(spirometerWriteCharacteristicFf0b.characteristicUuid);
print(spirometerWriteCharacteristicFf0b.properties.toString());
// Write ACK command to ff0b every 500 ms
await Future.delayed(Duration(milliseconds: 1000)).then((value) async {
_timer = Timer.periodic(Duration(milliseconds: 500), (Timer t) {
spirometerWriteCharacteristicFf0b.write([0x55, 0x06], withoutResponse: false).then((value) {
print("----ff0b ACK command data written----");
});
});
}); });
await element.device.connect(timeout: Duration(seconds: 35));
return true;
} }
},
);
} }
},
);
}); });
}); }
}
},
);
},
// onError(e) => print(e);
);
FlutterBluePlus.startScan(timeout: const Duration(seconds: 5), androidUsesFineLocation: false);
} }
} }
} }

@ -30,10 +30,6 @@ class _WeightScaleBLEState extends State<WeightScaleBLE> {
final bleConnectionStatus = ValueNotifier<String>("Disconnected"); final bleConnectionStatus = ValueNotifier<String>("Disconnected");
BluetoothCharacteristic ble_fff1;
BluetoothCharacteristic ble_fff2;
BluetoothCharacteristic ble_fff3;
String resultSet1 = ""; String resultSet1 = "";
String resultSet2 = ""; String resultSet2 = "";
@ -140,7 +136,6 @@ class _WeightScaleBLEState extends State<WeightScaleBLE> {
print(service.serviceUuid); print(service.serviceUuid);
service.characteristics.forEach((characteristic) async { service.characteristics.forEach((characteristic) async {
if (characteristic.characteristicUuid.toString().toLowerCase() == BLEUtils.WEIGHT_MEASUREMENT_CHARACTERISTIC_FFF1) { if (characteristic.characteristicUuid.toString().toLowerCase() == BLEUtils.WEIGHT_MEASUREMENT_CHARACTERISTIC_FFF1) {
ble_fff1 = characteristic;
print(characteristic.characteristicUuid); print(characteristic.characteristicUuid);
print(characteristic.properties.toString()); print(characteristic.properties.toString());
characteristic.onValueReceived.listen((event) { characteristic.onValueReceived.listen((event) {
@ -181,7 +176,6 @@ class _WeightScaleBLEState extends State<WeightScaleBLE> {
} }
if (characteristic.characteristicUuid.toString().toLowerCase() == BLEUtils.WEIGHT_MEASUREMENT_CHARACTERISTIC_FFF2) { if (characteristic.characteristicUuid.toString().toLowerCase() == BLEUtils.WEIGHT_MEASUREMENT_CHARACTERISTIC_FFF2) {
ble_fff2 = characteristic;
print(characteristic.characteristicUuid); print(characteristic.characteristicUuid);
print(characteristic.properties.toString()); print(characteristic.properties.toString());
characteristic.onValueReceived.listen((event) { characteristic.onValueReceived.listen((event) {

@ -9,7 +9,8 @@ class BLEUtils {
//Spirometer //Spirometer
static const String BLE_TO_MSA100_SERVICE = "0000fff0-0000-1000-8000-00805f9b34fb"; static const String BLE_TO_MSA100_SERVICE = "0000fff0-0000-1000-8000-00805f9b34fb";
static const String BLE_TO_MSA100_CHARACTERISTIC = "0000ff0b-0000-1000-8000-00805f9b34fb"; static const String BLE_TO_MSA100_WRITE_CHARACTERISTIC = "0000ff0b-0000-1000-8000-00805f9b34fb";
static const String BLE_TO_MSA100_READ_CHARACTERISTIC = "0000ff0a-0000-1000-8000-00805f9b34fb";
static const String MSA100_TO_BLE_SERVICE = "0000fff0-0000-1000-8000-00805f9b34fb"; static const String MSA100_TO_BLE_SERVICE = "0000fff0-0000-1000-8000-00805f9b34fb";
static const String MSA100_TO_BLE_CHARACTERISTIC = "0000ff0a-0000-1000-8000-00805f9b34fb"; static const String MSA100_TO_BLE_CHARACTERISTIC = "0000ff0a-0000-1000-8000-00805f9b34fb";

Loading…
Cancel
Save