|
|
|
|
@ -17,24 +17,28 @@ class ECG_BLE extends StatefulWidget {
|
|
|
|
|
|
|
|
|
|
class _ECG_BLEState extends State<ECG_BLE> {
|
|
|
|
|
String connectionStatus = "disconnected";
|
|
|
|
|
|
|
|
|
|
BluetoothDevice currentConnectedDevice;
|
|
|
|
|
|
|
|
|
|
BluetoothCharacteristic ecgWriteCharacteristic;
|
|
|
|
|
|
|
|
|
|
StreamSubscription bleDevicesStream;
|
|
|
|
|
StreamSubscription bleDeviceConnectionStream;
|
|
|
|
|
|
|
|
|
|
final bleConnectionStatus = ValueNotifier<String>("Disconnected");
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void dispose() {
|
|
|
|
|
super.dispose();
|
|
|
|
|
if (bleDevicesStream != null) bleDevicesStream.cancel();
|
|
|
|
|
if (currentConnectedDevice != null) currentConnectedDevice.disconnect();
|
|
|
|
|
bleConnectionStatus.dispose();
|
|
|
|
|
if (bleDeviceConnectionStream != null) bleDeviceConnectionStream.cancel();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
|
|
|
|
FlutterBluePlus.setLogLevel(LogLevel.verbose, color: false);
|
|
|
|
|
// FlutterBluePlus.setLogLevel(LogLevel.verbose, color: false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
@ -42,7 +46,7 @@ class _ECG_BLEState extends State<ECG_BLE> {
|
|
|
|
|
return AppScaffold(
|
|
|
|
|
appBarTitle: "ECG",
|
|
|
|
|
showNewAppBar: true,
|
|
|
|
|
isShowDecPage: true,
|
|
|
|
|
isShowDecPage: false,
|
|
|
|
|
showNewAppBarTitle: true,
|
|
|
|
|
backgroundColor: Color(0xffF8F8F8),
|
|
|
|
|
body: SingleChildScrollView(
|
|
|
|
|
@ -68,7 +72,12 @@ class _ECG_BLEState extends State<ECG_BLE> {
|
|
|
|
|
SizedBox(
|
|
|
|
|
height: 50.0,
|
|
|
|
|
),
|
|
|
|
|
Text("Connection state: $connectionStatus"),
|
|
|
|
|
ValueListenableBuilder(
|
|
|
|
|
valueListenable: bleConnectionStatus,
|
|
|
|
|
builder: (context, value, _) {
|
|
|
|
|
return Text("Connection state: $value");
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
SizedBox(
|
|
|
|
|
height: 50.0,
|
|
|
|
|
),
|
|
|
|
|
@ -93,9 +102,7 @@ class _ECG_BLEState extends State<ECG_BLE> {
|
|
|
|
|
|
|
|
|
|
void startBLEConnection() {
|
|
|
|
|
if (FlutterBluePlus.isScanningNow == false) {
|
|
|
|
|
setState(() {
|
|
|
|
|
connectionStatus = "Connecting...";
|
|
|
|
|
});
|
|
|
|
|
bleConnectionStatus.value = "Connecting...";
|
|
|
|
|
|
|
|
|
|
bleDevicesStream = FlutterBluePlus.scanResults.listen((results) {
|
|
|
|
|
List<ScanResult> blueToothDevices = results;
|
|
|
|
|
@ -103,61 +110,123 @@ class _ECG_BLEState extends State<ECG_BLE> {
|
|
|
|
|
if (element.device.localName.isNotEmpty) {
|
|
|
|
|
if (element.device.localName.toLowerCase() == "pm101897") {
|
|
|
|
|
bleDevicesStream.cancel();
|
|
|
|
|
element.device.connectionState.listen((BluetoothConnectionState state) async {
|
|
|
|
|
if (mounted) {
|
|
|
|
|
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) {
|
|
|
|
|
if (FlutterBluePlus.isScanningNow) {
|
|
|
|
|
FlutterBluePlus.stopScan();
|
|
|
|
|
element.device.connect(timeout: Duration(seconds: 30), autoConnect: false).then((value) async {
|
|
|
|
|
bleConnectionStatus.value = "Connected...";
|
|
|
|
|
print("Device Connected-------");
|
|
|
|
|
currentConnectedDevice = element.device;
|
|
|
|
|
bleDeviceConnectionStream = currentConnectedDevice.connectionState.listen((event) {
|
|
|
|
|
if (event == BluetoothConnectionState.disconnected) {
|
|
|
|
|
bleConnectionStatus.value = "Disconnected...";
|
|
|
|
|
print("Device Disconnected-------");
|
|
|
|
|
// if (_timer.isActive) _timer.cancel();
|
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
});
|
|
|
|
|
FlutterBluePlus.stopScan();
|
|
|
|
|
|
|
|
|
|
List<BluetoothService> services = await element.device.discoverServices(timeout: 30).catchError((err) {
|
|
|
|
|
print(err.toString());
|
|
|
|
|
element.device.disconnect(timeout: 15);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (services != null && services.isNotEmpty) {
|
|
|
|
|
services.forEach((service) {
|
|
|
|
|
if (service.serviceUuid.toString().toLowerCase() == BLEUtils.ECG_SERVICE) {
|
|
|
|
|
print(service.serviceUuid);
|
|
|
|
|
service.characteristics.forEach((characteristic) async {
|
|
|
|
|
if (characteristic.characteristicUuid.toString().toLowerCase() == BLEUtils.ECG_READ_CHARACTERISTIC) {
|
|
|
|
|
print(characteristic.characteristicUuid);
|
|
|
|
|
print(characteristic.properties.toString());
|
|
|
|
|
|
|
|
|
|
characteristic.onValueReceived.listen((event) {
|
|
|
|
|
print("onValueReceived Stream");
|
|
|
|
|
print("onValueReceived 1e4d Stream");
|
|
|
|
|
print(event);
|
|
|
|
|
});
|
|
|
|
|
if (!characteristic.isNotifying) await characteristic.setNotifyValue(true);
|
|
|
|
|
|
|
|
|
|
await Future.delayed(Duration(milliseconds: 1000)).then((value) async {
|
|
|
|
|
print("-----Delayed 1e4d notify true done-----");
|
|
|
|
|
if (!characteristic.isNotifying) await characteristic.setNotifyValue(true).catchError((err) {});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (characteristic.characteristicUuid.toString().toLowerCase() == BLEUtils.ECG_WRITE_CHARACTERISTIC) {
|
|
|
|
|
print("Write Characteristic: ${characteristic.characteristicUuid}");
|
|
|
|
|
ecgWriteCharacteristic = characteristic;
|
|
|
|
|
await ecgWriteCharacteristic.write([0x83]);
|
|
|
|
|
print(characteristic.characteristicUuid);
|
|
|
|
|
print(characteristic.properties.toString());
|
|
|
|
|
|
|
|
|
|
// Write get cases command to 8841
|
|
|
|
|
await Future.delayed(Duration(milliseconds: 1000)).then((value) async {
|
|
|
|
|
// 0x90 00 00 00 1
|
|
|
|
|
characteristic.write([0x90, 0x00, 0x00, 0x00, 0x01], withoutResponse: false).then((value) {
|
|
|
|
|
print("----8841 get device info command data written----");
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}).catchError((onError) {
|
|
|
|
|
print("----- ERRORRRR!!!!!! -------");
|
|
|
|
|
print(onError.toString());
|
|
|
|
|
});
|
|
|
|
|
await element.device.connect(timeout: Duration(seconds: 35));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if (element.device.localName.isNotEmpty) {
|
|
|
|
|
// if (element.device.localName.toLowerCase() == "pm101897") {
|
|
|
|
|
// bleDevicesStream.cancel();
|
|
|
|
|
// element.device.connectionState.listen((BluetoothConnectionState state) async {
|
|
|
|
|
// if (mounted) {
|
|
|
|
|
// 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) {
|
|
|
|
|
// 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) {
|
|
|
|
|
// print(service.serviceUuid);
|
|
|
|
|
// service.characteristics.forEach((characteristic) async {
|
|
|
|
|
// if (characteristic.characteristicUuid.toString().toLowerCase() == BLEUtils.ECG_READ_CHARACTERISTIC) {
|
|
|
|
|
// print(characteristic.characteristicUuid);
|
|
|
|
|
// characteristic.onValueReceived.listen((event) {
|
|
|
|
|
// print("onValueReceived Stream");
|
|
|
|
|
// print(event);
|
|
|
|
|
// });
|
|
|
|
|
// if (!characteristic.isNotifying) await characteristic.setNotifyValue(true);
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// if (characteristic.characteristicUuid.toString().toLowerCase() == BLEUtils.ECG_WRITE_CHARACTERISTIC) {
|
|
|
|
|
// print("Write Characteristic: ${characteristic.characteristicUuid}");
|
|
|
|
|
// ecgWriteCharacteristic = characteristic;
|
|
|
|
|
// await ecgWriteCharacteristic.write([0x83]);
|
|
|
|
|
// }
|
|
|
|
|
// });
|
|
|
|
|
// return true;
|
|
|
|
|
// }
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
// });
|
|
|
|
|
// await element.device.connect(timeout: Duration(seconds: 35));
|
|
|
|
|
// return true;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
FlutterBluePlus.startScan(timeout: const Duration(seconds: 15), androidUsesFineLocation: false).then((value) {
|
|
|
|
|
print("Scan Finished");
|
|
|
|
|
});
|
|
|
|
|
FlutterBluePlus.startScan(timeout: const Duration(seconds: 15), androidUsesFineLocation: false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|