Skip to content

fix: 修改react-native-ble-manager中write方法写入数据是变化的,写入特征值是异步处理的 #35

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 29, 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/BuildProfile.ets
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Use these variables when you tailor your ArkTS code. They must be of the const type.
*/
export const HAR_VERSION = '11.5.2-0.0.4';
export const HAR_VERSION = '11.5.2-0.0.7';
export const BUILD_MODE_NAME = 'debug';
export const DEBUG = true;
export const TARGET_NAME = 'default';
Expand Down
12 changes: 6 additions & 6 deletions harmony/ble_manager/src/main/ets/BleTurboModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,12 +466,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
65 changes: 34 additions & 31 deletions harmony/ble_manager/src/main/ets/PeripheralData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,54 +240,58 @@ export default class PeripheralData {
});
}

async write(serviceUUID: string, characteristicUUID: string, data:Uint8Array,
maxByteSize: number,writeType:number): Promise<void> {
if(!this.isConnected || this.device === null) {
//修改后
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) {
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) {
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))) {
const uint8Data = data
if (data.length <= maxByteSize) {
const isDoWriteSuccess = await this.doWrite(characteristic, uint8Data.buffer);
if (!isDoWriteSuccess) {
return Promise.reject("Write failed")
} else {
return Promise.resolve();
}
} else {
let dataLength = data.length;
let dataLength = uint8Data.buffer.byteLength;
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);
let firstMessage: ArrayBuffer = null;
const splittedMessage = [];
while (count < dataLength && (dataLength - count > maxByteSize)) {
if (count == 0) {
firstMessage = uint8Data.buffer.slice(count, count + maxByteSize);
} else {
//将其分割数据添加到列表
splittedMessage.push(new Uint8Array(data).subarray(count,count + maxByteSize))
splittedMessage.push(uint8Data.buffer.slice(count, count + maxByteSize))
}
count += maxByteSize;
}
if(count < dataLength) {
splittedMessage.push(new Uint8Array(data).subarray(count, data.length))
if (count < dataLength) {
splittedMessage.push(uint8Data.buffer.slice(count, dataLength))
}
//harmonyOS 默认writeType == ble.GattWriteType.WRITE
let writeError:boolean = false;
if(!this.doWrite(characteristic,firstMessage)) {
let writeError: boolean = false;
const isDoWriteSuccess = await this.doWrite(characteristic, firstMessage);
if (!isDoWriteSuccess) {
writeError = true;
return Promise.reject('Write failed')
}
if(!writeError) {
for (let i = 0;i< splittedMessage.length;i++) {
if (!writeError) {
for (let i = 0; i < splittedMessage.length; i++) {
let message = splittedMessage[i];
if(!this.doWrite(characteristic,message)) {
const isDoWriteSuccess = await this.doWrite(characteristic, message);
if (!isDoWriteSuccess) {
writeError = true;
return Promise.reject('Write failed')
}
Expand All @@ -297,14 +301,13 @@ export default class PeripheralData {
return Promise.resolve();
}

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

Expand Down
Binary file modified harmony/ble_managerGatt.har
Binary file not shown.
Binary file modified harmony/ble_managerServers.har
Binary file not shown.