有没有大侠实现过BLE设备与APP之间的数据交换呢?我在调试的过程中,出现如下问题:
10:58:51.462 App Show at App.vue:42
10:58:57.387 thirdScriptError
10:58:57.407 undefined is not an object (evaluating 'this.characteristicsData[0].uuid'); [Component] Event Handler Error @ pages/API/bluetooth/bluetooth#handleEvent
10:58:57.427 BLESendDatas
10:58:57.447 BLESendDatas@[native code]
10:58:57.487 forEach@[native code]
10:58:57.527 forEach@[native code]
10:58:57.547 handleEvent
10:58:57.567 safeCallback
10:58:57.647 n
10:58:57.667 subscribeHandler
10:58:57.767 consume
10:58:57.787 map@[native code]
10:59:00.027 App Hide at App.vue:45
我的源码如下,是在hello-uniapp的blutooth.vue源代码平台上调试的。
/**
* 向指定的UUID发送Datas
*/
BLESendDatas() {
// 向蓝牙设备发送一个0x00的16进制数据
let deviceId = this.equipment[0].deviceId;
let serviceId = this.servicesData[0].uuid;
let characteristicId = this.characteristicsData[0].uuid;
/// let characteristicId = this.NowcharacteristicsData;
let characteristicIdA = this.characteristicsData[0] ;
let characteristicIdB = this.characteristicsData[0];
/// let notify = this.characteristicsData[0].properties.notify;
/// characteristicId = "0000FFE1-0000-1000-8000-00805F9B34FB";
characteristicId = characteristicIdA ;
console.log(deviceId);
console.log(serviceId);
console.log(characteristicId);
console.log(characteristicIdA);
/// console.log(characteristicIdB);
/// console.log(notify);
var SendBuffer=new ArrayBuffer(19);//长度为四个字节
/// var SendBuffer=new Uint8Array(buffer);
var i ;
for(i=0;i<9;i++)
{
SendBuffer[i] = '0'+i;
}
/// console.log(SendBuffer);
uni.writeBLECharacteristicValue({
// 这里的 deviceId 需要在 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
deviceId,
// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
serviceId,
// 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
characteristicId,
// 这里的value是ArrayBuffer类型
value: SendBuffer,
success(res) {
console.log('writeBLECharacteristicValue success', res.errMsg);
},
fail: e => {
console.log('writeBLECharacteristicValue,错误码:' + e.errCode);
if (e.errCode !== 0) {
initTypes(e.errCode);
}
},
});
}
这个获取 characteristicId 是直接抄本文件中如下方法体来的:
/**
* 读取低功耗蓝牙设备的特征值的二进制数据值。注意:必须设备的特征值支持 read 才可以成功调用
*/
readBLECharacteristicValue() {
let deviceId = this.equipment[0].deviceId;
let serviceId = this.servicesData[0].uuid;
let characteristicId = this.characteristicsData[0].uuid;
console.log(deviceId);
console.log(serviceId);
console.log(characteristicId);
uni.readBLECharacteristicValue({
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
deviceId,
// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
serviceId,
// 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
characteristicId,
success: res => {
console.log('读取设备数据值成功');
console.log(JSON.stringify(res));
this.notifyBLECharacteristicValueChange();
},
fail(e) {
console.log('读取设备数据值失败,错误码:' + e.errCode);
if (e.errCode !== 0) {
initTypes(e.errCode);
}
}
});
this.onBLECharacteristicValueChange();
},
1 个回复
1***@qq.com (作者) - 代码英雄
问题已经解决了,谢谢。