Skip to content

feat: 新增react-native-ble-manager中write方法maxByteSize字段 #32

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 18, 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
26 changes: 9 additions & 17 deletions harmony/ble_manager/src/main/ets/BleTurboModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,23 +249,16 @@ export class BleTurboModule extends TurboModule implements TM.ReactNativeBleMana

async write(peripheralId: string, serviceUUID: string, characteristicUUID: string, data: number[],
maxByteSize: number): Promise<void> {
if(serviceUUID === null || characteristicUUID === null) {
return Promise.reject('ServiceUUID and characteristicUUID required.')
}
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 peripheral = this.peripherals.get(peripheralId);
if (peripheral) {
peripheral.write(serviceUUID,characteristicUUID,new Uint8Array(data),maxByteSize,ble.GattWriteType.WRITE);
} else {
return Promise.reject('Peripheral 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 @@ -714,8 +707,7 @@ export class BleTurboModule extends TurboModule implements TM.ReactNativeBleMana
if (peripheralData === null || peripheralData === undefined) {
if (peripheralId) {
peripheralId = peripheralId.toLowerCase();
}
;
};
if (this.isBluetoothAddress(peripheralId)) {
try {
let device: ble.GattClientDevice = ble.createGattClientDevice(peripheralId);
Expand Down
69 changes: 69 additions & 0 deletions harmony/ble_manager/src/main/ets/PeripheralData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,75 @@ export default class PeripheralData {
});
}

async write(serviceUUID: string, characteristicUUID: string, data:Uint8Array,
maxByteSize: number,writeType:number): Promise<void> {
if(!this.isConnected || this.device === null) {
return Promise.reject('Device is not connected')
}
let result:Array<ble.GattService> = await this.device.getServices();
let gattService:ble.GattService = result.find((gattService) => gattService.serviceUuid === serviceUUID)
if(gattService === null || gattService === undefined) {
return Promise.reject(`serviceUUID + ${serviceUUID} + not found.`)
}
let characteristic: ble.BLECharacteristic = this.findWritableCharacteristic(gattService,characteristicUUID,writeType);
if(characteristic === null || characteristic === undefined) {
return Promise.reject(`Characteristic + ${characteristicUUID} + not found.`)
}
if(data.length <= maxByteSize) {
if(!this.doWrite(characteristic,new Uint8Array(data))) {
return Promise.reject("Write failed")
} else {
return Promise.resolve();
}
} else {
let dataLength = data.length;
let count = 0;
let firstMessage:Uint8Array = null;
const splittedMessage = [];
while (count < dataLength && (dataLength -count)> maxByteSize) {
if(count == 0) {
//处理第一个信息
firstMessage = new Uint8Array(data).subarray(count, count + maxByteSize);
} else {
//将其分割数据添加到列表
splittedMessage.push(new Uint8Array(data).subarray(count,count + maxByteSize))
}
count += maxByteSize;
}
if(count < dataLength) {
splittedMessage.push(new Uint8Array(data).subarray(count, data.length))
}
//harmonyOS 默认writeType == ble.GattWriteType.WRITE
let writeError:boolean = false;
if(!this.doWrite(characteristic,firstMessage)) {
writeError = true;
return Promise.reject('Write failed')
}
if(!writeError) {
for (let i = 0;i< splittedMessage.length;i++) {
let message = splittedMessage[i];
if(!this.doWrite(characteristic,message)) {
writeError = true;
return Promise.reject('Write failed')
}
}
}
}
return Promise.resolve();
}

doWrite(characteristic: ble.BLECharacteristic,data:Uint8Array):boolean{
try {
characteristic.characteristicValue = data.buffer;
this.device.writeCharacteristicValue(characteristic,ble.GattWriteType.WRITE)
return true;
} catch (error) {
console.info('Write failed :' + JSON.stringify(error))
return false;
}
}


findWritableCharacteristic(gattService:ble.GattService,characteristicUUID: string,writeType:number):ble.BLECharacteristic{
let writeProperty = ble.GattWriteType.WRITE;
if(writeType == ble.GattWriteType.WRITE_NO_RESPONSE) {
Expand Down
Binary file modified harmony/ble_managerGatt.har
Binary file not shown.
Binary file modified harmony/ble_managerServers.har
Binary file not shown.