diff --git a/quick_blue/android/src/main/kotlin/com/example/quick_blue/QuickBluePlugin.kt b/quick_blue/android/src/main/kotlin/com/example/quick_blue/QuickBluePlugin.kt index f73ad2f..f8aeded 100644 --- a/quick_blue/android/src/main/kotlin/com/example/quick_blue/QuickBluePlugin.kt +++ b/quick_blue/android/src/main/kotlin/com/example/quick_blue/QuickBluePlugin.kt @@ -61,6 +61,8 @@ class QuickBluePlugin: FlutterPlugin, MethodCallHandler, EventChannel.StreamHand mainThreadHandler.post { messageChannel.send(message) } } + fun trace() = Arrays.toString(Throwable().stackTrace) + override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) { when (call.method) { "isBluetoothAvailable" -> { @@ -92,7 +94,7 @@ class QuickBluePlugin: FlutterPlugin, MethodCallHandler, EventChannel.StreamHand "disconnect" -> { val deviceId = call.argument("deviceId")!! val gatt = knownGatts.find { it.device.address == deviceId } - ?: return result.error("IllegalArgument", "Unknown deviceId: $deviceId", null) + ?: return result.error("IllegalArgument", "Unknown deviceId: $deviceId", trace()) cleanConnection(gatt) result.success(null) //FIXME If `disconnect` is called before BluetoothGatt.STATE_CONNECTED @@ -101,7 +103,7 @@ class QuickBluePlugin: FlutterPlugin, MethodCallHandler, EventChannel.StreamHand "discoverServices" -> { val deviceId = call.argument("deviceId")!! val gatt = knownGatts.find { it.device.address == deviceId } - ?: return result.error("IllegalArgument", "Unknown deviceId: $deviceId", null) + ?: return result.error("IllegalArgument", "Unknown deviceId: $deviceId", trace()) gatt.discoverServices() result.success(null) } @@ -111,7 +113,7 @@ class QuickBluePlugin: FlutterPlugin, MethodCallHandler, EventChannel.StreamHand val characteristic = call.argument("characteristic")!! val bleInputProperty = call.argument("bleInputProperty")!! val gatt = knownGatts.find { it.device.address == deviceId } - ?: return result.error("IllegalArgument", "Unknown deviceId: $deviceId", null) + ?: return result.error("IllegalArgument", "Unknown deviceId: $deviceId", trace()) gatt.setNotifiable(service to characteristic, bleInputProperty) result.success(null) } @@ -119,7 +121,7 @@ class QuickBluePlugin: FlutterPlugin, MethodCallHandler, EventChannel.StreamHand val deviceId = call.argument("deviceId")!! val expectedMtu = call.argument("expectedMtu")!! val gatt = knownGatts.find { it.device.address == deviceId } - ?: return result.error("IllegalArgument", "Unknown deviceId: $deviceId", null) + ?: return result.error("IllegalArgument", "Unknown deviceId: $deviceId", trace()) gatt.requestMtu(expectedMtu) result.success(null) } @@ -128,14 +130,14 @@ class QuickBluePlugin: FlutterPlugin, MethodCallHandler, EventChannel.StreamHand val service = call.argument("service")!! val characteristic = call.argument("characteristic")!! val gatt = knownGatts.find { it.device.address == deviceId } - ?: return result.error("IllegalArgument", "Unknown deviceId: $deviceId", null) + ?: return result.error("IllegalArgument", "Unknown deviceId: $deviceId", trace()) val readResult = gatt.getCharacteristic(service to characteristic)?.let { gatt.readCharacteristic(it) } if (readResult == true) result.success(null) else - result.error("Characteristic unavailable", null, null) + result.error("Characteristic unavailable", null, trace()) } "writeValue" -> { val deviceId = call.argument("deviceId")!! @@ -143,7 +145,7 @@ class QuickBluePlugin: FlutterPlugin, MethodCallHandler, EventChannel.StreamHand val characteristic = call.argument("characteristic")!! val value = call.argument("value")!! val gatt = knownGatts.find { it.device.address == deviceId } - ?: return result.error("IllegalArgument", "Unknown deviceId: $deviceId", null) + ?: return result.error("IllegalArgument", "Unknown deviceId: $deviceId", trace()) val writeResult = gatt.getCharacteristic(service to characteristic)?.let { it.value = value gatt.writeCharacteristic(it) @@ -151,7 +153,7 @@ class QuickBluePlugin: FlutterPlugin, MethodCallHandler, EventChannel.StreamHand if (writeResult == true) result.success(null) else - result.error("Characteristic unavailable", null, null) + result.error("Characteristic unavailable", null, trace()) } else -> { result.notImplemented() @@ -286,8 +288,9 @@ val ScanResult.manufacturerDataHead: ByteArray? fun Short.toByteArray(byteOrder: ByteOrder = ByteOrder.LITTLE_ENDIAN): ByteArray = ByteBuffer.allocate(2 /*Short.SIZE_BYTES*/).order(byteOrder).putShort(this).array() -fun BluetoothGatt.getCharacteristic(serviceCharacteristic: Pair) = - getService(UUID.fromString(serviceCharacteristic.first)).getCharacteristic(UUID.fromString(serviceCharacteristic.second)) +fun BluetoothGatt.getCharacteristic(serviceCharacteristic: Pair): BluetoothGattCharacteristic { + return getService(UUID.fromString(serviceCharacteristic.first)).getCharacteristic(UUID.fromString(serviceCharacteristic.second)) +} private val DESC__CLIENT_CHAR_CONFIGURATION = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb") diff --git a/quick_blue/lib/quick_blue.dart b/quick_blue/lib/quick_blue.dart index 7be2302..49a0089 100644 --- a/quick_blue/lib/quick_blue.dart +++ b/quick_blue/lib/quick_blue.dart @@ -49,24 +49,24 @@ class QuickBlue { static Future isBluetoothAvailable() => _platform.isBluetoothAvailable(); - static void startScan() => _platform.startScan(); + static Future startScan() => _platform.startScan(); - static void stopScan() => _platform.stopScan(); + static Future stopScan() => _platform.stopScan(); static Stream get scanResultStream { return _platform.scanResultStream .map((item) => BlueScanResult.fromMap(item)); } - static void connect(String deviceId) => _platform.connect(deviceId); + static Future connect(String deviceId) => _platform.connect(deviceId); - static void disconnect(String deviceId) => _platform.disconnect(deviceId); + static Future disconnect(String deviceId) => _platform.disconnect(deviceId); static void setConnectionHandler(OnConnectionChanged? onConnectionChanged) { _platform.onConnectionChanged = onConnectionChanged; } - static void discoverServices(String deviceId) => _platform.discoverServices(deviceId); + static Future discoverServices(String deviceId) => _platform.discoverServices(deviceId); static void setServiceHandler(OnServiceDiscovered? onServiceDiscovered) { _platform.onServiceDiscovered = onServiceDiscovered; diff --git a/quick_blue_linux/lib/quick_blue_linux.dart b/quick_blue_linux/lib/quick_blue_linux.dart index 83b9f2d..b348f39 100644 --- a/quick_blue_linux/lib/quick_blue_linux.dart +++ b/quick_blue_linux/lib/quick_blue_linux.dart @@ -45,20 +45,21 @@ class QuickBlueLinux extends QuickBluePlatform { } @override - void startScan() async { + Future startScan() async { await _ensureInitialized(); _log('startScan invoke success'); - _activeAdapter!.startDiscovery(); + final ret = _activeAdapter!.startDiscovery(); _client.devices.forEach(_onDeviceAdd); + return ret; } @override - void stopScan() async { + Future stopScan() async { await _ensureInitialized(); _log('stopScan invoke success'); - _activeAdapter!.stopDiscovery(); + return _activeAdapter!.stopDiscovery(); } // FIXME Close @@ -77,19 +78,19 @@ class QuickBlueLinux extends QuickBluePlatform { } @override - void connect(String deviceId) { + Future connect(String deviceId) { // TODO: implement connect throw UnimplementedError(); } @override - void disconnect(String deviceId) { + Future disconnect(String deviceId) { // TODO: implement disconnect throw UnimplementedError(); } @override - void discoverServices(String deviceId) { + Future discoverServices(String deviceId) { // TODO: implement discoverServices throw UnimplementedError(); } diff --git a/quick_blue_platform_interface/lib/method_channel_quick_blue.dart b/quick_blue_platform_interface/lib/method_channel_quick_blue.dart index 1e673a0..ee8aae2 100644 --- a/quick_blue_platform_interface/lib/method_channel_quick_blue.dart +++ b/quick_blue_platform_interface/lib/method_channel_quick_blue.dart @@ -28,19 +28,18 @@ class MethodChannelQuickBlue extends QuickBluePlatform { @override Future isBluetoothAvailable() async { - bool result = await _method.invokeMethod('isBluetoothAvailable'); - return result; + return await _method.invokeMethod('isBluetoothAvailable'); } @override - void startScan() { - _method.invokeMethod('startScan') + Future startScan() { + return _method.invokeMethod('startScan') .then((_) => print('startScan invokeMethod success')); } @override - void stopScan() { - _method.invokeMethod('stopScan') + Future stopScan() { + return _method.invokeMethod('stopScan') .then((_) => print('stopScan invokeMethod success')); } @@ -50,22 +49,22 @@ class MethodChannelQuickBlue extends QuickBluePlatform { Stream get scanResultStream => _scanResultStream; @override - void connect(String deviceId) { - _method.invokeMethod('connect', { + Future connect(String deviceId) { + return _method.invokeMethod('connect', { 'deviceId': deviceId, }).then((_) => _log('connect invokeMethod success')); } @override - void disconnect(String deviceId) { - _method.invokeMethod('disconnect', { + Future disconnect(String deviceId) { + return _method.invokeMethod('disconnect', { 'deviceId': deviceId, }).then((_) => _log('disconnect invokeMethod success')); } @override - void discoverServices(String deviceId) { - _method.invokeMethod('discoverServices', { + Future discoverServices(String deviceId) { + return _method.invokeMethod('discoverServices', { 'deviceId': deviceId, }).then((_) => _log('discoverServices invokeMethod success')); } @@ -96,7 +95,7 @@ class MethodChannelQuickBlue extends QuickBluePlatform { @override Future setNotifiable(String deviceId, String service, String characteristic, BleInputProperty bleInputProperty) async { - _method.invokeMethod('setNotifiable', { + return _method.invokeMethod('setNotifiable', { 'deviceId': deviceId, 'service': service, 'characteristic': characteristic, @@ -106,7 +105,7 @@ class MethodChannelQuickBlue extends QuickBluePlatform { @override Future readValue(String deviceId, String service, String characteristic) async { - _method.invokeMethod('readValue', { + return _method.invokeMethod('readValue', { 'deviceId': deviceId, 'service': service, 'characteristic': characteristic, @@ -115,7 +114,7 @@ class MethodChannelQuickBlue extends QuickBluePlatform { @override Future writeValue(String deviceId, String service, String characteristic, Uint8List value, BleOutputProperty bleOutputProperty) async { - _method.invokeMethod('writeValue', { + return _method.invokeMethod('writeValue', { 'deviceId': deviceId, 'service': service, 'characteristic': characteristic, @@ -137,7 +136,9 @@ class MethodChannelQuickBlue extends QuickBluePlatform { _method.invokeMethod('requestMtu', { 'deviceId': deviceId, 'expectedMtu': expectedMtu, - }).then((_) => _log('requestMtu invokeMethod success')); + }).then((_) => _log('requestMtu invokeMethod success')).catchError((e) { + _log("requestMtu error: $e"); //TODO Return future that errors if this errors + }); return await _mtuConfigController.stream.first; } } diff --git a/quick_blue_platform_interface/lib/quick_blue_platform_interface.dart b/quick_blue_platform_interface/lib/quick_blue_platform_interface.dart index 722c7f1..ae7e924 100644 --- a/quick_blue_platform_interface/lib/quick_blue_platform_interface.dart +++ b/quick_blue_platform_interface/lib/quick_blue_platform_interface.dart @@ -37,19 +37,19 @@ abstract class QuickBluePlatform extends PlatformInterface { Future isBluetoothAvailable(); - void startScan(); + Future startScan(); - void stopScan(); + Future stopScan(); Stream get scanResultStream; - void connect(String deviceId); + Future connect(String deviceId); - void disconnect(String deviceId); + Future disconnect(String deviceId); OnConnectionChanged? onConnectionChanged; - void discoverServices(String deviceId); + Future discoverServices(String deviceId); OnServiceDiscovered? onServiceDiscovered;