|
|
|
|
@ -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<ECG_BLE> {
|
|
|
|
|
|
|
|
|
|
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<ECG_BLE> {
|
|
|
|
|
connectionStatus = "Connecting...";
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
FlutterBluePlus.startScan(timeout: const Duration(seconds: 5), androidUsesFineLocation: false).then((value) {
|
|
|
|
|
List<ScanResult> blueToothDevices = [];
|
|
|
|
|
// List<ScanResult> blueToothDevices = value;
|
|
|
|
|
bleDevicesStream = FlutterBluePlus.scanResults.listen((results) {
|
|
|
|
|
List<ScanResult> 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<ECG_BLE> {
|
|
|
|
|
// 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<BluetoothService> 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<ECG_BLE> {
|
|
|
|
|
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<ECG_BLE> {
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
FlutterBluePlus.startScan(timeout: const Duration(seconds: 15), androidUsesFineLocation: false).then((value) {
|
|
|
|
|
print("Scan Finished");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|