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...";
}); bleDevicesStream = FlutterBluePlus.scanResults.listen(
(results) {
FlutterBluePlus.startScan(timeout: const Duration(seconds: 5), androidUsesFineLocation: false).then((value) { List<ScanResult> blueToothDevices = results;
List<ScanResult> blueToothDevices = []; blueToothDevices.forEach(
// List<ScanResult> blueToothDevices = value; (element) {
blueToothDevices.forEach((element) async { if (element.device.localName.isNotEmpty) {
if (element.device.localName.isNotEmpty) { if (element.device.localName.toLowerCase() == "ble-msa") {
if (element.device.localName.toLowerCase() == "ble-msa") { bleDevicesStream.cancel();
element.device.connectionState.listen((BluetoothConnectionState state) async { element.device.connect(timeout: Duration(seconds: 5), autoConnect: false).then((value) async {
setState(() { bleConnectionStatus.value = "Connected...";
connectionStatus = state.toString(); print("Device Connected-------");
}); currentConnectedDevice = element.device;
if (state == BluetoothConnectionState.disconnected) { FlutterBluePlus.stopScan();
// typically, start a periodic timer that tries to periodically reconnect. List<BluetoothService> services = await element.device.discoverServices(timeout: 5);
// Note: you must always re-discover services after disconnection! services.forEach(
if (_timer != null && _timer.isActive) _timer.cancel(); (service) {
if (_timerRead != null && _timerRead.isActive) _timerRead.cancel(); if (service.serviceUuid.toString().toLowerCase() == BLEUtils.BLE_TO_MSA100_SERVICE) {
} print(service.serviceUuid);
if (state == BluetoothConnectionState.connected) { service.characteristics.forEach(
currentConnectedDevice = element.device; (characteristic) async {
if (characteristic.characteristicUuid.toString().toLowerCase() == BLEUtils.BLE_TO_MSA100_READ_CHARACTERISTIC) {
List<int> BLEToMSAWriteCharacters = []; spirometerReadCharacteristicFf0a = characteristic;
BLEToMSAWriteCharacters.add(int.parse("5506", radix: 16)); print(spirometerReadCharacteristicFf0a.characteristicUuid);
print(spirometerReadCharacteristicFf0a.properties.toString());
List<BluetoothService> services = await element.device.discoverServices(); spirometerReadCharacteristicFf0a.onValueReceived.listen((event) async {
services.forEach((service) { print("onValueReceived ff0a Stream");
if (service.serviceUuid.toString().toLowerCase() == BLEUtils.BLE_TO_MSA100_SERVICE) { print(event);
print(service.serviceUuid);
service.characteristics.forEach((characteristic) async { //Sample response
if (characteristic.characteristicUuid.toString().toLowerCase() == BLEUtils.BLE_TO_MSA100_CHARACTERISTIC) { // dd14d93d5c1f01a302f8de3d5c1c017302
print(characteristic.characteristicUuid);
_timer = Timer.periodic(Duration(seconds: 2), (Timer timer) { if (event[0] == 170) {
characteristic.write(BLEToMSAWriteCharacters).then((value) { _timer.cancel();
print("Characteristic response:");
}).catchError((err) {}); // Write get history command to ff0b
}); if (!isHistoryCommandWritten) {
} await Future.delayed(Duration(milliseconds: 1000)).then((value) async {
if (characteristic.characteristicUuid.toString().toLowerCase() == BLEUtils.MSA100_TO_BLE_CHARACTERISTIC) { spirometerWriteCharacteristicFf0b.write([0x55, 0x01], withoutResponse: false).then((value) {
print(characteristic.characteristicUuid); print("----ff0b get history command data written----");
_timerRead = Timer.periodic(Duration(seconds: 2), (Timer timer) { isHistoryCommandWritten = true;
characteristic.read().then((value) { });
print("Characteristic Read value: "); });
print(value);
}); // Write get 1st history command to ff0b
}); await Future.delayed(Duration(milliseconds: 1000)).then((value) async {
spirometerWriteCharacteristicFf0b.write([0x55, 0x02, 0x01, 0x00], withoutResponse: false).then((value) {
// characteristic.onValueReceived.listen((event) { print("----ff0b get 1st history command data written----");
// print("onValueReceived Stream"); isHistoryCommandWritten = true;
// print(event); });
// setState(() { });
// currentTempInCelsius = convertIntListToHex(event);
// String currentTempInFahrenheit = ((num.parse(currentTempInCelsius) * 1.8) + 32).toStringAsFixed(1); // Write delete history command to ff0b
// currentTempInCelsius = currentTempInCelsius + "\u2103" + " / " + currentTempInFahrenheit + "\u2109"; // await Future.delayed(Duration(milliseconds: 1000)).then((value) async {
// }); // spirometerWriteCharacteristicFf0b.write([0x55, 0x03], withoutResponse: false).then((value) {
// }); // print("----ff0b delete history command data written----");
// await characteristic.setNotifyValue(true); // isHistoryCommandWritten = 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----");
});
});
});
}
},
);
} }
}); },
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);
} }
} }
} }

@ -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