Skip to content

feat: 修改react-native-ble-manager的stopScan方法抛出异常的显示结果 #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion harmony/ble_manager/oh-package.json5
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
type: 'module',
version: '11.5.2-0.0.4',
dependencies: {
"@rnoh/react-native-openharmony": 'file:../libs/react_native_openharmony-5.0.0.500.har'
"@rnoh/react-native-openharmony": "file:../libs/react_native_openharmony-5.0.0.500.har"
},
}
87 changes: 33 additions & 54 deletions harmony/ble_manager/src/main/ets/BleTurboModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,54 +247,29 @@ export class BleTurboModule extends TurboModule implements TM.ReactNativeBleMana
}


write(peripheralId: string, serviceUUID: string, characteristicUUID: string, data: number[],
async write(peripheralId: string, serviceUUID: string, characteristicUUID: string, data: number[],
maxByteSize: number): Promise<void> {
let peripheral: PeripheralData = this.retrieveOrCreatePeripheral(peripheralId);
return new Promise((resolve, reject) => {
peripheral.retrieveServices(peripheralId, [serviceUUID]).then((result: Array<ble.GattService>) => {
if (result.length > 0) {
result.forEach(services => {
if (services.serviceUuid == serviceUUID) {
Utils.ArrayBuffer2String(services?.characteristics[0]?.characteristicValue)
let descriptors: Array<ble.BLEDescriptor> = [];
let descriptor: ble.BLEDescriptor = {
serviceUuid: services.serviceUuid,
characteristicUuid: characteristicUUID,
descriptorUuid: services.characteristics[0].descriptors[0].descriptorUuid,
descriptorValue: services.characteristics[0].descriptors[0].descriptorValue
};
descriptors[0] = descriptor;
let characteristic: ble.BLECharacteristic = {
serviceUuid: services.serviceUuid,
characteristicUuid: characteristicUUID,
characteristicValue: new Uint8Array(data).buffer,
descriptors: descriptors
};

function writeCharacteristicValueCallBack(code: BusinessError) {
if (code != null) {
return;
}
}
try {
const per: PeripheralData = this.retrieveOrCreatePeripheral(peripheralId);
let device: ble.GattClientDevice = per.getDevice();
device.writeCharacteristicValue(characteristic, ble.GattWriteType.WRITE,
writeCharacteristicValueCallBack);
resolve();
} catch (error) {
reject('errCode: ' + (error as BusinessError).code + ', errMessage: ' +
(error as BusinessError).message);
}
}
})
} else {
reject("Write failed");
}
}).catch(error => {
reject(error + "Write failed")
})
});
try {
let peripheral: PeripheralData = this.retrieveOrCreatePeripheral(peripheralId);
if(serviceUUID === null || characteristicUUID === null) {
return Promise.reject('ServiceUUID and characteristicUUID required.')
}
let result:Array<ble.GattService> = await peripheral.retrieveServices(peripheralId, [serviceUUID]);
let gattService:ble.GattService = result.find((gattService) => gattService.serviceUuid === serviceUUID)
console.info('gattService =====' + JSON.stringify(gattService))
if(gattService === null || gattService === undefined) {
return Promise.reject(`serviceUUID + ${serviceUUID} + not found.`)
}
let characteristic: ble.BLECharacteristic = peripheral.findWritableCharacteristic(gattService,characteristicUUID,ble.GattWriteType.WRITE);
if(characteristic === null || characteristic === undefined) {
return Promise.reject(`Characteristic + ${characteristicUUID} + not found.`)
}
let device: ble.GattClientDevice = peripheral.getDevice();
device.writeCharacteristicValue(characteristic, ble.GattWriteType.WRITE);
return Promise.resolve()
} catch (error) {
return Promise.reject(error + "Write failed")
}
}


Expand Down Expand Up @@ -498,12 +473,12 @@ export class BleTurboModule extends TurboModule implements TM.ReactNativeBleMana

start(options: StartOptions): Promise<void> {
Logger.info("start")
// this.startAdvertising()
// this.addService()
// this.onCharacteristicWrite()
// this.onCharacteristicRead()
// this.onDescriptorWrite()
// this.onDescriptorRead()
this.startAdvertising()
this.addService()
this.onCharacteristicWrite()
this.onCharacteristicRead()
this.onDescriptorWrite()
this.onDescriptorRead()
this.scanManager = new DefaultScanManager(this.ctx, this)
access.on('stateChange', this.onStateChange.bind(this));
connection.on('bondStateChange', this.onBondStateChange.bind(this));
Expand Down Expand Up @@ -605,7 +580,11 @@ export class BleTurboModule extends TurboModule implements TM.ReactNativeBleMana
}

stopScan(): Promise<void> {
ble.stopBLEScan();
try {
ble.stopBLEScan();
} catch (error) {
return Promise.reject('No bluetooth support')
}
this.scanManager.setIsScanning()
let bleStopScanEvent: BleStopScanEvent = {
status: 0
Expand Down
38 changes: 27 additions & 11 deletions harmony/ble_manager/src/main/ets/PeripheralData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export default class PeripheralData {
if (!this.connected && this.device) {
this.onBLEConnectionStateChange(this.device)
try {

this.device.connect();
this.connecting = true;
return true
Expand Down Expand Up @@ -129,10 +130,6 @@ export default class PeripheralData {
return this.connected
}

isConnecting() {

}

readRSSI(): Promise<number> {
if (!this.isConnected()) {
return Promise.reject('Device is not connected')
Expand All @@ -144,9 +141,13 @@ export default class PeripheralData {
}

disconnect() {
this.connected = false;
if (this.device) {
this.device.disconnect();
try {
this.connected = false;
if (this.device) {
this.device.disconnect();
}
}catch (error){
Logger.error('errCode: ' + (error as BusinessError).code + ', errMessage: ' + (error as BusinessError).message);
}
}

Expand All @@ -171,10 +172,6 @@ export default class PeripheralData {
}
}

getServices() {

}

requestMTU(mtu: number): Promise<number> {
if (!this.isConnected()) {
return Promise.reject("Device is not connected")
Expand Down Expand Up @@ -242,4 +239,23 @@ export default class PeripheralData {
}
});
}

findWritableCharacteristic(gattService:ble.GattService,characteristicUUID: string,writeType:number):ble.BLECharacteristic{
let writeProperty = ble.GattWriteType.WRITE;
if(writeType == ble.GattWriteType.WRITE_NO_RESPONSE) {
writeProperty = ble.GattWriteType.WRITE_NO_RESPONSE
}
let characteristics: Array<ble.BLECharacteristic> = gattService.characteristics;
try {
let chara = characteristics.find((characteristic) => {
let properties = characteristic.properties?.write ? 1 : 0
return ((properties & writeProperty) != 0 && characteristic.characteristicUuid === characteristicUUID) ?
characteristic : null
})
return chara;
}catch (error){
Logger.error(TAG,"Error on findWritableCharacteristic",error);
return null;
}
}
}
Binary file modified harmony/ble_managerGatt.har
Binary file not shown.
Binary file modified harmony/ble_managerServers.har
Binary file not shown.