From 09fa45ae990df5cc055ffbc1fca5632598424ce9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cliang=5Ftao001=E2=80=9D?= Date: Mon, 9 Sep 2024 17:09:31 +0800 Subject: [PATCH 1/2] =?UTF-8?q?1=E3=80=81=E4=BF=AE=E6=94=B9=E8=B0=83?= =?UTF-8?q?=E7=94=A8setMtu=E6=97=B6=E8=BF=94=E5=9B=9E=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E9=97=AE=E9=A2=98=E3=80=822=E3=80=81?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=B0=83=E7=94=A8write=E8=BF=9B=E8=A1=8C?= =?UTF-8?q?=E7=89=B9=E5=BE=81=E5=80=BC=E5=86=99=E5=85=A5=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E9=83=A8=E5=88=86=E8=AE=BE=E5=A4=87=E6=94=B6=E4=B8=8D=E5=88=B0?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: “liang_tao001” --- AppScope/app.json5 | 2 +- CHANGELOG.md | 5 ++++ entry/oh-package.json5 | 2 +- library/oh-package.json5 | 2 +- .../src/main/ets/bluetooth/BleBluetooth.ets | 6 +++- .../src/main/ets/bluetooth/BleConnector.ets | 29 ++++++++++--------- 6 files changed, 29 insertions(+), 17 deletions(-) diff --git a/AppScope/app.json5 b/AppScope/app.json5 index 9def6f6..1b224fb 100644 --- a/AppScope/app.json5 +++ b/AppScope/app.json5 @@ -3,7 +3,7 @@ "bundleName": "cn.openharmony.fastble", "vendor": "example", "versionCode": 1000000, - "versionName": "2.0.2", + "versionName": "2.0.3", "icon": "$media:app_icon", "label": "$string:app_name" } diff --git a/CHANGELOG.md b/CHANGELOG.md index a61e575..9e59c28 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## v2.0.3 + +- 修改调用setMtu时返回设置失败问题。 +- 修改调用write进行特征值写入时,部分设备收不到数据问题。 + ## v2.0.2 - 修改调用BleManager的notify接口后,在onNotifySuccess回调中再调用BleManager的write方法后,报错device is busy的BUG。 diff --git a/entry/oh-package.json5 b/entry/oh-package.json5 index a7736af..0082d8e 100644 --- a/entry/oh-package.json5 +++ b/entry/oh-package.json5 @@ -5,7 +5,7 @@ "name": "entry", "description": "Please describe the basic information.", "main": "", - "version": "2.0.2", + "version": "2.0.3", "dependencies": { "@ohos/fastble": "file:../library" } diff --git a/library/oh-package.json5 b/library/oh-package.json5 index df77601..62396c8 100644 --- a/library/oh-package.json5 +++ b/library/oh-package.json5 @@ -12,6 +12,6 @@ "description": "FastBle是一个处理蓝牙BLE设备的库,可以对蓝牙BLE设备进行过滤,扫描,连接,读取,写入等", "main": "Index.ets", "repository": "https://gitee.com/openharmony-sig/FastBle", - "version": "2.0.2", + "version": "2.0.3", "dependencies": {} } diff --git a/library/src/main/ets/bluetooth/BleBluetooth.ets b/library/src/main/ets/bluetooth/BleBluetooth.ets index b7b07d1..68db8c6 100644 --- a/library/src/main/ets/bluetooth/BleBluetooth.ets +++ b/library/src/main/ets/bluetooth/BleBluetooth.ets @@ -266,7 +266,11 @@ export default class BleBluetooth { private closeBluetoothGatt(): void { if (this.gattClientDevice != null) { - this.gattClientDevice.close(); + try { + this.gattClientDevice.close(); + } catch (e) { + console.info("closeBluetoothGatt err:"+JSON.stringify(e)); + } } } diff --git a/library/src/main/ets/bluetooth/BleConnector.ets b/library/src/main/ets/bluetooth/BleConnector.ets index 8ebd4c2..14dcc14 100644 --- a/library/src/main/ets/bluetooth/BleConnector.ets +++ b/library/src/main/ets/bluetooth/BleConnector.ets @@ -40,6 +40,7 @@ export default class BleConnector { private mCharacteristic: ble.BLECharacteristic | null = null; private mBleBluetooth: BleBluetooth; private mHandler: Handler; + private bleMtuChangedCallback :BleMtuChangedCallback|null=null constructor(bleBluetooth: BleBluetooth) { this.mBleBluetooth = bleBluetooth; @@ -266,9 +267,7 @@ export default class BleConnector { return; } - if (this.mCharacteristic == null - /*|| (this.mCharacteristic.getProperties() & (bluetooth.BLECharacteristic.PROPERTY_WRITE | bluetooth.BLECharacteristic.PROPERTY_WRITE_NO_RESPONSE)) == 0*/ - ) { + if (this.mCharacteristic == null) { if (bleWriteCallback != null) { bleWriteCallback.onWriteFailure(new OtherException("this characteristic not support write!")); } @@ -280,7 +279,11 @@ export default class BleConnector { console.info("BleConnetor.writeCharacteristic calling gatt.writeCharacteristic"); if (this.mBluetoothGatt !== null) { try { - this.mBluetoothGatt.writeCharacteristicValue(this.mCharacteristic, ble.GattWriteType.WRITE) + if (this.mCharacteristic.properties?.writeNoResponse) { + this.mBluetoothGatt.writeCharacteristicValue(this.mCharacteristic, ble.GattWriteType.WRITE_NO_RESPONSE) + }else { + this.mBluetoothGatt.writeCharacteristicValue(this.mCharacteristic, ble.GattWriteType.WRITE) + } if (bleWriteCallback != null) { this.writeMsgInit(); bleWriteCallback.onWriteSuccess(data.length, data.length, data); @@ -341,19 +344,24 @@ export default class BleConnector { * set mtu */ public setMtu(requiredMtu: number, bleMtuChangedCallback: BleMtuChangedCallback): void { + this.bleMtuChangedCallback = bleMtuChangedCallback; this.handleSetMtuCallback(bleMtuChangedCallback); - try { - if (this.mBluetoothGatt !== null) { + if (!!this.mBluetoothGatt) { this.mBluetoothGatt.setBLEMtuSize(requiredMtu) + bleMtuChangedCallback.setHandler(this.mHandler); + this.mBleBluetooth.addMtuChangedCallback(bleMtuChangedCallback); + let message = this.mHandler.obtainMessage(BleMsg.MSG_SET_MTU_RESULT, bleMtuChangedCallback); + let bundle: Map = new Map(); + bundle.set(BleMsg.KEY_SET_MTU_BUNDLE_VALUE, requiredMtu); + message.setData(bundle); + this.mHandler.sendMessage(message); } } catch (e) { - this.mtuChangedMsgInit(); if (bleMtuChangedCallback != null) { bleMtuChangedCallback.onSetMTUFailure(new OtherException("gatt requestMtu fail")); } - } } @@ -646,14 +654,9 @@ class BleConnectorHandler extends Handler { this.bleConnector.mtuChangedMsgInit(); let mtuChangedCallback: BleMtuChangedCallback = msg.obj as BleMtuChangedCallback; - let status: number = msg.getData().get(BleMsg.KEY_SET_MTU_BUNDLE_STATUS); let value: number = msg.getData().get(BleMsg.KEY_SET_MTU_BUNDLE_VALUE); if (mtuChangedCallback != null) { - if (status == 0) { mtuChangedCallback.onMtuChanged(value); - } else { - mtuChangedCallback.onSetMTUFailure(new GattException(status)); - } } break; } -- Gitee From 9ab7d6232761e55fbbf6289cdbeaec9df0270b2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cliang=5Ftao001=E2=80=9D?= Date: Wed, 11 Sep 2024 10:58:14 +0800 Subject: [PATCH 2/2] =?UTF-8?q?1=E3=80=81=E4=BF=AE=E6=94=B9=E8=B0=83?= =?UTF-8?q?=E7=94=A8write=E6=96=B9=E6=B3=95=E5=90=8E=EF=BC=8C=E5=85=B3?= =?UTF-8?q?=E9=97=AD=E6=89=8B=E6=9C=BA=E8=93=9D=E7=89=99=EF=BC=8C=E7=84=B6?= =?UTF-8?q?=E5=90=8E=E6=89=93=E5=BC=80=E9=87=8D=E6=96=B0=E6=89=AB=E6=8F=8F?= =?UTF-8?q?=E9=93=BE=E6=8E=A5=E5=90=8E=E5=86=8D=E8=B0=83=E7=94=A8write?= =?UTF-8?q?=E6=96=B9=E6=B3=95=EF=BC=8ConCharacteristicChanged=E4=BC=9A?= =?UTF-8?q?=E5=A4=9A=E6=AC=A1=E5=9B=9E=E8=B0=83=E3=80=822=E3=80=81?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=93=9D=E7=89=99=E6=89=AB=E6=8F=8F=E3=80=81?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E3=80=81=E6=96=AD=E5=BC=80=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E3=80=81=E4=BB=A5=E5=8F=8A=E7=9B=91=E5=90=AC=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E6=B3=A8=E5=86=8C=E7=9A=84=E9=80=BB=E8=BE=91=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=BC=82=E5=B8=B8=E6=8D=95=E8=8E=B7=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: “liang_tao001” --- CHANGELOG.md | 2 + .../src/main/ets/bluetooth/BleBluetooth.ets | 18 +++-- .../src/main/ets/bluetooth/BleConnector.ets | 69 ++++++++++++------- .../src/main/ets/scan/BleScanPresenter.ets | 16 +++-- library/src/main/ets/scan/BleScanner.ets | 31 ++++++--- 5 files changed, 91 insertions(+), 45 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e59c28..c04e4f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ - 修改调用setMtu时返回设置失败问题。 - 修改调用write进行特征值写入时,部分设备收不到数据问题。 +- 修改调用write方法后,关闭手机蓝牙,然后打开重新扫描链接后再调用write方法,onCharacteristicChanged会多次回调。 +- 优化蓝牙扫描、连接、断开连接、以及监听事件注册的逻辑,添加异常捕获。 ## v2.0.2 diff --git a/library/src/main/ets/bluetooth/BleBluetooth.ets b/library/src/main/ets/bluetooth/BleBluetooth.ets index 68db8c6..ebeb9fb 100644 --- a/library/src/main/ets/bluetooth/BleBluetooth.ets +++ b/library/src/main/ets/bluetooth/BleBluetooth.ets @@ -179,20 +179,23 @@ export default class BleBluetooth { message.what = BleMsg.MSG_DISCOVER_SERVICES; this.mainHandler.sendMessageDelayed(message, 1000); } - return null; } - this.gattClientDevice = ble.createGattClientDevice(bleDevice.getMac()); try { - this.gattClientDevice.on('BLEConnectionStateChange', this.connectStateChangeCallback); - } catch (err) { - + this.gattClientDevice = ble.createGattClientDevice(bleDevice.getMac()); + } catch (e) { + BleLog.i("createGattClientDevice err: " +JSON.stringify(e)); } + try { + this.gattClientDevice?.on('BLEConnectionStateChange', this.connectStateChangeCallback); + } catch (err) { + BleLog.i("on BLEConnectionStateChange err: " +JSON.stringify(err)); + } try { - this.gattClientDevice.connect() + this.gattClientDevice?.connect() if (this.bleGattCallback != null) { this.bleGattCallback.onStartConnect(); } @@ -247,6 +250,7 @@ export default class BleBluetooth { try { this.gattClientDevice.disconnect(); } catch (e) { + BleLog.i("disconnect err: " +JSON.stringify(e)); } } } @@ -269,7 +273,7 @@ export default class BleBluetooth { try { this.gattClientDevice.close(); } catch (e) { - console.info("closeBluetoothGatt err:"+JSON.stringify(e)); + BleLog.i("closeBluetoothGatt err: " +JSON.stringify(e)); } } } diff --git a/library/src/main/ets/bluetooth/BleConnector.ets b/library/src/main/ets/bluetooth/BleConnector.ets index 14dcc14..681519f 100644 --- a/library/src/main/ets/bluetooth/BleConnector.ets +++ b/library/src/main/ets/bluetooth/BleConnector.ets @@ -29,6 +29,10 @@ import OtherException from '../exception/OtherException'; import TimeoutException from '../exception/TimeoutException'; import { Message, Handler } from '../utils/Handler'; import ble from '@ohos.bluetooth.ble'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { access } from '@kit.ConnectivityKit'; +import { BusinessError } from '@kit.BasicServicesKit'; +import BleLog from '../utils/BleLog'; type UUID = string; type byte = number; @@ -41,11 +45,22 @@ export default class BleConnector { private mBleBluetooth: BleBluetooth; private mHandler: Handler; private bleMtuChangedCallback :BleMtuChangedCallback|null=null + private bleCharacteristicChangeCallBack: Callback|null=null + private TAG: string = "YTB BleConnector"; constructor(bleBluetooth: BleBluetooth) { this.mBleBluetooth = bleBluetooth; this.mBluetoothGatt = bleBluetooth.getBluetoothGatt(); this.mHandler = new BleConnectorHandler(this) + try { + access.on('stateChange', (data: access.BluetoothState) => { + if (data.valueOf()==0) { + this.mBluetoothGatt?.off("BLECharacteristicChange") + } + }); + } catch (err) { + console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); + } } private withUUID(serviceUUID: UUID, characteristicUUID: UUID): BleConnector { @@ -82,8 +97,7 @@ export default class BleConnector { */ public enableCharacteristicNotify(bleNotifyCallback: BleNotifyCallback, uuid_notify: string, userCharacteristicDescriptor: boolean): void { - if (this.mCharacteristic != null && this.mBluetoothGatt != null - ) { + if (this.mCharacteristic != null && this.mBluetoothGatt != null) { this.handleCharacteristicNotifyCallback(bleNotifyCallback, uuid_notify); this.setCharacteristicNotification(this.mBluetoothGatt, this.mCharacteristic, userCharacteristicDescriptor, true, bleNotifyCallback); @@ -302,20 +316,23 @@ export default class BleConnector { * read */ public readCharacteristic(bleReadCallback: BleReadCallback, uuid_read: string): void { - if (this.mCharacteristic != null - /*&& (this.mCharacteristic.getProperties() & bluetooth.BLECharacteristic.PROPERTY_READ) > 0*/ - ) { + if (this.mCharacteristic != null) { this.handleCharacteristicReadCallback(bleReadCallback, uuid_read); - this.mBluetoothGatt?.readCharacteristicValue(this.mCharacteristic, (err, characteristic: ble.BLECharacteristic) => { - this.readMsgInit(); - if (bleReadCallback != null) { - if (!err) { - bleReadCallback.onReadSuccess(new Uint8Array(characteristic.characteristicValue)); - } else { - bleReadCallback.onReadFailure(new OtherException("gatt readCharacteristic fail:" + err.message)); - } - } - }); + try { + this.mBluetoothGatt?.readCharacteristicValue(this.mCharacteristic, + (err, characteristic: ble.BLECharacteristic) => { + this.readMsgInit(); + if (bleReadCallback != null) { + if (!err) { + bleReadCallback.onReadSuccess(new Uint8Array(characteristic.characteristicValue)); + } else { + bleReadCallback.onReadFailure(new OtherException("gatt readCharacteristic fail:" + err.message)); + } + } + }); + } catch (e) { + BleLog.i("readCharacteristicValue err: " +JSON.stringify(e)); + } } else { if (bleReadCallback != null) { bleReadCallback.onReadFailure(new OtherException("this characteristic not support read!")); @@ -328,16 +345,20 @@ export default class BleConnector { */ public readRemoteRssi(bleRssiCallback: BleRssiCallback): void { this.handleRSSIReadCallback(bleRssiCallback); - this.mBluetoothGatt?.getRssiValue((err, number) => { - this.rssiMsgInit(); - if (bleRssiCallback != null) { - if (!err) { - bleRssiCallback.onRssiSuccess(number); - } else { - bleRssiCallback.onRssiFailure(new OtherException("gatt readRemoteRssi fail:" + err.message)); + try { + this.mBluetoothGatt?.getRssiValue((err, number) => { + this.rssiMsgInit(); + if (bleRssiCallback != null) { + if (!err) { + bleRssiCallback.onRssiSuccess(number); + } else { + bleRssiCallback.onRssiFailure(new OtherException("gatt readRemoteRssi fail:" + err.message)); + } } - } - }); + }); + } catch (e) { + BleLog.i("getRssiValue err: " +JSON.stringify(e)); + } } /** diff --git a/library/src/main/ets/scan/BleScanPresenter.ets b/library/src/main/ets/scan/BleScanPresenter.ets index 84991c1..531094a 100644 --- a/library/src/main/ets/scan/BleScanPresenter.ets +++ b/library/src/main/ets/scan/BleScanPresenter.ets @@ -52,12 +52,16 @@ abstract class BleScanPresenter { private handleResult(bleDevice: BleDevice): void { this.onLeScan(bleDevice); - let gattClient = ble.createGattClientDevice(bleDevice.getMac()); - let that = this - gattClient.getDeviceName((err, data) => { - bleDevice.setName(data); - that.checkDevice(bleDevice); - }) + try { + let gattClient = ble.createGattClientDevice(bleDevice.getMac()); + let that = this + gattClient.getDeviceName((err, data) => { + bleDevice.setName(data); + that.checkDevice(bleDevice); + }) + } catch (e) { + BleLog.i("BleScanPresenter getDeviceName err: " +JSON.stringify(e)); + } } public handleScanResults(data: Array): void { diff --git a/library/src/main/ets/scan/BleScanner.ets b/library/src/main/ets/scan/BleScanner.ets index f188432..4b9fc54 100644 --- a/library/src/main/ets/scan/BleScanner.ets +++ b/library/src/main/ets/scan/BleScanner.ets @@ -78,7 +78,11 @@ export default class BleScanner { if (BleManager.MOCK_DEVICE) { BleManager.mockBluetooth.onScan(this.callback); } else { - ble.on('BLEDeviceFind', this.callback); + try { + ble.on('BLEDeviceFind', this.callback); + } catch (e) { + BleLog.i("on BLEDeviceFind err: " +JSON.stringify(e)); + } } console.info("after on") @@ -100,12 +104,15 @@ export default class BleScanner { if (BleManager.MOCK_DEVICE) { BleManager.mockBluetooth.startBLEScan(); } else { - if (filterArry.length == 0) { - ble.startBLEScan(null, scanOption); - } else { - ble.startBLEScan(filterArry, scanOption); + try { + if (filterArry.length == 0) { + ble.startBLEScan(null, scanOption); + } else { + ble.startBLEScan(filterArry, scanOption); + } + } catch (e) { + BleLog.i("startBLEScan err: " +JSON.stringify(e)); } - } this.mBleScanState = BleScanState.STATE_SCANNING; @@ -116,10 +123,18 @@ export default class BleScanner { if (BleManager.MOCK_DEVICE) { BleManager.mockBluetooth.stopBLEScan(); } else { - ble.stopBLEScan(); + try { + ble.stopBLEScan(); + } catch (e) { + BleLog.i("stopBLEScan err: " +JSON.stringify(e)); + } } this.mBleScanState = BleScanState.STATE_IDLE; - ble.off('BLEDeviceFind',this.callback) + try { + ble.off('BLEDeviceFind', this.callback) + } catch (e) { + BleLog.i("off BLEDeviceFind err: " +JSON.stringify(e)); + } this.mBleScanPresenter.notifyScanStopped(); } -- Gitee