微信小程序蓝牙特性有write和writeNoResponse权限但写入数据时报1007错的uni-app解决方案
微信小程序蓝牙特性有write和writeNoResponse权限但写入数据时报1007错的uni-app解决方案
| 开发环境 | 版本号 | 项目创建方式 |
|---|---|---|
| Windows | Windows 11 | HBuilderX |
| 专业版(26100.6584) | ||
| HBuilderX | 4.76 | |
| 第三方开发者工具 | Stable 1.062504030 | |
| 基础库 | 3.9.3 |
示例代码:
// 连接蓝牙
uni.createBLEConnection({
deviceId: BleData.deviceId,
success: (res) => {
console.log("连接成功", res)
OpenPopup('message','success','蓝牙连接成功');
BleData.bleState = true;
// 监听蓝牙连接状态
uni.onBLEConnectionStateChange((res)=>{
console.log(`device ${res.deviceId} state has changed, connected: ${res.connected}`)
BleData.bleState = res.connected;
if(!BleData.bleState){
OpenPopup('alertDialog','error','蓝牙已断开,是否重连!');
}
});
// 获取所有的服务
uni.getBLEDeviceServices({
deviceId: BleData.deviceId,
success(res) {
console.log('服务:', res);
if(res.services[0].isPrimary){
//验证服务ID并连接
for (let index = 0; index < res.services.length; index++) {
if(res.services[index].uuid == "0000FFF0-0000-1000-8000-00805F9B34FB"){
BleData.serviceId = res.services[index].uuid;
}
}
}
// 获取特征码
uni.getBLEDeviceCharacteristics({
deviceId:BleData.deviceId,
serviceId:BleData.serviceId,
success(res) {
console.log('特征:', res);
for (let index = 0; index < res.characteristics.length; index++) {
if(res.characteristics[index].properties.notify == true){
BleData.notifyId = res.characteristics[index].uuid;
// 监听接收数据
uni.notifyBLECharacteristicValueChange({
state: true,
deviceId:BleData.deviceId,
serviceId:BleData.serviceId,
characteristicId:BleData.notifyId,
success(res) {
console.log("开启 notify 功能成功...,notifyId:",BleData.notifyId)
// 必须先启用 notifyBLECharacteristicValueChange 接口才能接收到设备推送
uni.onBLECharacteristicValueChange(res => {
ReceiveBleData(res.value);
})
},
fail(err) {
console.log("监听失败,发生错误", err);
console.log("this.characteristicId", BleData.notifyId);
}
})
continue;
}
if((res.characteristics[index].properties.write == true) || (res.characteristics[index].properties.writeNoResponse == true)){
BleData.writeId = res.characteristics[index].uuid;
continue;
}
}
}
})
}
})
},
fail: (error) => {
console.log("连接失败", error)
// uni.showToast({title: '蓝牙连接失败,请稍后重试',icon:"none",duration: 2000});
OpenPopup('alertDialog','error','蓝牙连接失败,请重试!');
}
})
```
更多关于微信小程序蓝牙特性有write和writeNoResponse权限但写入数据时报1007错的uni-app解决方案的实战教程也可以访问 https://www.itying.com/category-93-b0.html
4 回复
试试原生微信小程序有没有这个问题
更多关于微信小程序蓝牙特性有write和writeNoResponse权限但写入数据时报1007错的uni-app解决方案的实战教程也可以访问 https://www.itying.com/category-93-b0.html
回复 8***@qq.com: 发一下你写的uniapp的代码和原生微信小程序测试的代码
在uni-app中遇到蓝牙写入返回1007错误,通常是由于写入数据格式或时机问题导致的。以下是针对该问题的解决方案:
1. 数据格式处理
确保写入数据为ArrayBuffer格式,使用uni.writeArrayBufferToObject或uni.arrayBufferToBase64进行转换:
const buffer = new ArrayBuffer(data.length);
const dataView = new DataView(buffer);
// 填充数据...
uni.writeBLECharacteristicValue({
deviceId: BleData.deviceId,
serviceId: BleData.serviceId,
characteristicId: BleData.writeId,
value: buffer,
success: (res) => {
console.log("写入成功", res);
},
fail: (err) => {
console.log("写入失败", err);
}
});
2. 写入时机控制 在特征值读取完成后进行写入操作,避免设备未就绪:
uni.getBLEDeviceCharacteristics({
deviceId: BleData.deviceId,
serviceId: BleData.serviceId,
success: (res) => {
// 特征值获取完成后执行写入
setTimeout(() => {
// 写入操作
}, 200);
}
});
3. 数据分包处理 单次写入数据长度不超过20字节(BLE4.0限制),长数据需分包:
function writeData(data) {
const chunkSize = 20;
for (let i = 0; i < data.length; i += chunkSize) {
const chunk = data.slice(i, i + chunkSize);
// 依次写入每个数据包
}
}
4. 错误重试机制 添加写入失败的重试逻辑:
function writeWithRetry(data, retryCount = 3) {
uni.writeBLECharacteristicValue({
// ...参数
fail: (err) => {
if (retryCount > 0) {
setTimeout(() => {
writeWithRetry(data, retryCount - 1);
}, 100);
}
}
});
}
5. 连接状态验证 在写入前确认蓝牙连接状态:
if (BleData.bleState && BleData.writeId) {
// 执行写入操作
} else {
console.log("蓝牙未就绪");
}


