diff --git a/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/ble/BleBridge.kt b/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/ble/BleBridge.kt index 7d8e3aad..87ac63b5 100644 --- a/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/ble/BleBridge.kt +++ b/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/ble/BleBridge.kt @@ -6,6 +6,7 @@ import android.content.Intent.getIntent import android.net.Uri import android.os.Build import android.os.Bundle +import android.os.Handler import android.provider.Settings import android.widget.Toast import androidx.core.app.ActivityCompat.startActivityForResult @@ -15,6 +16,7 @@ import io.flutter.plugin.common.MethodCall import io.flutter.plugin.common.MethodChannel import io.flutter.plugin.common.EventChannel import android.util.SparseArray +import androidx.annotation.RequiresApi //Ble @@ -43,6 +45,7 @@ class BleBridge( private const val CHANNEL = "BLE-Platform-Bridge" private const val EVENTCHANNEL = "BLE-Platform-Bridge-Event" private const val SCAN_DEVICE = "scan" + private const val DISCONNECT_DEVICE = "disconnect_device" } @@ -107,6 +110,22 @@ class BleBridge( private var list = arrayListOf() + private var model = Bluetooth.MODEL_O2RING + + /** + * PS: O2 devices do not support processing multiple commands. + * If you want to use other command and start rtTask, you must stop rtTask. + */ + private var rtHandler = Handler() + private var rtTask = RtTask() + + inner class RtTask : Runnable { + override fun run() { + rtHandler.postDelayed(rtTask, 1000) + BleServiceHelper.BleServiceHelper.oxyGetRtParam(model) + } + } + fun create() { channel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL) Echannel = EventChannel(flutterEngine.dartExecutor.binaryMessenger, EVENTCHANNEL) @@ -114,6 +133,8 @@ class BleBridge( if (methodCall.method == SCAN_DEVICE) { scanDevice(methodCall, result) + } else if (methodCall.method == DISCONNECT_DEVICE) { + disconnectDevice(methodCall, result) } else { result.notImplemented() } @@ -130,6 +151,12 @@ class BleBridge( } + @RequiresApi(Build.VERSION_CODES.Q) + fun disconnectDevice(methodCall: MethodCall, result: MethodChannel.Result) { + if (rtHandler.hasCallbacks(rtTask)) rtHandler.removeCallbacks(rtTask) + BleServiceHelper.BleServiceHelper.disconnect(false) + } + fun scanDevice(methodCall: MethodCall, result: MethodChannel.Result) { println("This is Test of Scanning") @@ -145,7 +172,12 @@ class BleBridge( var deviceName: String = "" for (b in BluetoothController.getDevices()) { // println(b.name) - if (b.name.contains("POD-1_SN8187", true)) { + if (b.name.contains("POD-1_SN8187", true) || b.name.contains( + "O2M 1670", + true + ) + ) { + model = b.model deviceName = b.name BleServiceHelper.BleServiceHelper.setInterfaces(b.model) BleServiceHelper.BleServiceHelper.stopScan() @@ -159,13 +191,34 @@ class BleBridge( println("EventDeviceFound") println(deviceName) - - LiveEventBus.get(InterfaceEvent.PC60Fw.EventPC60FwRtParam) - .observe(this.mainActivity) { - val data = it.data as RtParam - println("OXY DATA: $data") - eventSink?.success(data.toString()) - } + + if (deviceName == "POD-1_SN8187") { + LiveEventBus.get(InterfaceEvent.PC60Fw.EventPC60FwRtParam) + .observe(this.mainActivity) { + val data = it.data as RtParam + println("OXY DATA POD-1W: $data") + eventSink?.success(data.toString()) + } + } + + if (deviceName == "O2M 1670") { + rtHandler.removeCallbacks(rtTask) + rtHandler.post(rtTask) + + LiveEventBus.get(InterfaceEvent.Oxy.EventOxyRtParamData) + .observe(this.mainActivity) { + val data = it.data as com.lepu.blepro.ext.oxy.RtParam + println("OXY DATA O2M: $data") + eventSink?.success(data.toString()) +// tv_oxy.text = data.spo2.toString() +// tv_pr.text = data.pr.toString() +// tv_pi.text = data.pi.toString() +// data_log.text = "$data" + // data.battery:0-100 + // data.batteryState:0(no charge),1(charging),2(charging complete) + // data.state:0(lead off),1(lead on),other(error) + } + } } diff --git a/lib/pages/ble_screen.dart b/lib/pages/ble_screen.dart index eec1fb85..f21a6d3d 100644 --- a/lib/pages/ble_screen.dart +++ b/lib/pages/ble_screen.dart @@ -39,7 +39,7 @@ class _BLEScreenState extends State { Center( child: GestureDetector( onTap: () async { - await BleChannel.getScanningResult(); + await BleChannel.getScanningResult("oximeter"); }, child: Container( width: 50, diff --git a/lib/pages/medical/my_trackers/viatom_devices/oxymeter_ble.dart b/lib/pages/medical/my_trackers/viatom_devices/oxymeter_ble.dart index 4392f346..421cf91b 100644 --- a/lib/pages/medical/my_trackers/viatom_devices/oxymeter_ble.dart +++ b/lib/pages/medical/my_trackers/viatom_devices/oxymeter_ble.dart @@ -19,6 +19,7 @@ class _Oxymeter_BLEState extends State { void dispose() { oxymeterValueNotifier.dispose(); super.dispose(); + BleChannel.disconnect(); } @override @@ -28,6 +29,9 @@ class _Oxymeter_BLEState extends State { eventChannel.receiveBroadcastStream().listen((event) { print('Received event---: $event'); oxymeterValueNotifier.value = event; + + // O2M response: RtParam{spo2=0, pr=0, steps=0, battery=72, batteryState=0, vector=0, pi=0.0, state=0, countDown=0, invalidIvState=0, spo2IvState=0, hrIvState=0, vectorIvState=0} + // POD-1W response: RtParam{spo2=98, pr=104, pi=5.8, isProbeOff=false, isPulseSearching=false} }); } @@ -73,11 +77,11 @@ class _Oxymeter_BLEState extends State { value.toString().replaceAll("RtParam", ""), ), ), - Text( - getPI( - value.toString().replaceAll("RtParam", ""), - ), - ), + // Text( + // getPI( + // value.toString().replaceAll("RtParam", ""), + // ), + // ), ], ) : Text(value); @@ -92,7 +96,7 @@ class _Oxymeter_BLEState extends State { void checkBLEPermissions() async { [Permission.location, Permission.storage, Permission.bluetooth, Permission.bluetoothConnect, Permission.bluetoothScan].request().then((status) async { // startBLEConnection(); - await BleChannel.getScanningResult(); + await BleChannel.getScanningResult("oximeter"); }); } diff --git a/lib/viatom_ble/ble_connect.dart b/lib/viatom_ble/ble_connect.dart index f9fa96e3..569383a4 100644 --- a/lib/viatom_ble/ble_connect.dart +++ b/lib/viatom_ble/ble_connect.dart @@ -5,7 +5,7 @@ class BleChannel { //BLE-Platform-Bridge - static Future getScanningResult() async { + static Future getScanningResult(String deviceType) async { try { print("----------Flutter Init -------"); final String result = await platform.invokeMethod('scan'); @@ -16,4 +16,17 @@ class BleChannel { return "Error: $e"; } } + + static Future disconnect() async { + try { + print("----------Flutter Init -------"); + final String result = await platform.invokeMethod('disconnect_device'); + print("----------Flutter Result -------"); + print(result); + return result; + } catch (e) { + return "Error: $e"; + } + } + }