- 当前问题的表现
已搜索到蓝牙设备的mac地址,通过wx.createBLEConnection连接该设备,要么连接失败,或者连接成功后但无法写特征值。
调试信息如下:
(16) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]0: {deviceId: "88:83:5D:56:CD:F1", name: "V1 Plus", RSSI: -61, advertisData: ArrayBuffer, advertisServiceUUIDs: Array(0), …}1: {deviceId: "88:83:5D:56:C8:B0", name: "V7c", RSSI: -50, advertisData: ArrayBuffer, advertisServiceUUIDs: Array(0), …}2: {deviceId: "88:83:5D:56:2D:37", name: "V1 Plus", RSSI: -85, advertisData: ArrayBuffer, advertisServiceUUIDs: Array(0), …}3: {deviceId: "88:83:5D:56:73:D4", name: "YX-PR-01A", RSSI: -78, advertisData: ArrayBuffer, advertisServiceUUIDs: Array(0), …}4: {deviceId: "88:83:5D:56:CB:94", name: "QQ亲戚", RSSI: -61, advertisData: ArrayBuffer, advertisServiceUUIDs: Array(0), …}5: {deviceId: "88:83:5D:57:13:F6", name: "V1 Plus", RSSI: -85, advertisData: ArrayBuffer, advertisServiceUUIDs: Array(0), …}6: {deviceId: "88:83:5D:56:77:58", name: "V1 Plus", RSSI: -67, advertisData: ArrayBuffer, advertisServiceUUIDs: Array(0), …}7: {deviceId: "88:83:5D:56:2D:6D", name: "V1 Plus", RSSI: -64, advertisData: ArrayBuffer, advertisServiceUUIDs: Array(0), …}8: {deviceId: "88:83:5D:56:2D:4D", name: "V1 Plus", RSSI: -68, advertisData: ArrayBuffer, advertisServiceUUIDs: Array(0), …}9: {deviceId: "88:83:5D:56:C8:C9", name: "厨房", RSSI: -37, advertisData: ArrayBuffer, advertisServiceUUIDs: Array(0), …}10: {deviceId: "88:83:5D:57:19:E7", name: "V1 Plus", RSSI: -65, advertisData: ArrayBuffer, advertisServiceUUIDs: Array(0), …}11: {deviceId: "88:83:5D:56:CA:13", name: "V1 Plus", RSSI: -88, advertisData: ArrayBuffer, advertisServiceUUIDs: Array(0), …}12: {deviceId: "88:83:5D:56:30:41", name: "V1 Plus", RSSI: -89, advertisData: ArrayBuffer, advertisServiceUUIDs: Array(0), …}13: {deviceId: "88:83:5D:56:C4:F7", name: "卧室", RSSI: -62, advertisData: ArrayBuffer, advertisServiceUUIDs: Array(0), …}14: {deviceId: "88:83:5D:57:16:C7", name: "家庭活动室", RSSI: -79, advertisData: ArrayBuffer, advertisServiceUUIDs: Array(0), …}15: {deviceId: "88:83:5D:56:73:B0", name: "rrww", RSSI: -56, advertisData: ArrayBuffer, advertisServiceUUIDs: Array(0), …}length: 16__proto__: Array(0)
index.js? [sm]:460 stop
index.js? [sm]:460 stop
index.js? [sm]:460 stop
index.js? [sm]:505 deviceId 88:83:5D:56:C8:C9
index.js? [sm]:566 连接失败 {errCode: 10012, errMsg: "createBLEConnection:fail:operate time out"}
fail @ index.js? [sm]:566
(anonymous) @ [publib]:3
(anonymous) @ [publib]:4
b @ [publib]:1
handleCallInterfaceResult @ [publib]:543
handleProcessMessage @ [publib]:782
ws.on @ [publib]:894
emitOne @ events.js:115
emit @ events.js:210
_receiver.onmessage @ C:\Program Files (x86)\Tencent\微信web开发者工具\package.nw\node_modules\ws\lib\WebSocket.js:141
dataMessage @ C:\Program Files (x86)\Tencent\微信web开发者工具\package.nw\node_modules\ws\lib\Receiver.js:389
getData @ C:\Program Files (x86)\Tencent\微信web开发者工具\package.nw\node_modules\ws\lib\Receiver.js:330
startLoop @ C:\Program Files (x86)\Tencent\微信web开发者工具\package.nw\node_modules\ws\lib\Receiver.js:165
add @ C:\Program Files (x86)\Tencent\微信web开发者工具\package.nw\node_modules\ws\lib\Receiver.js:139
_ultron.on @ C:\Program Files (x86)\Tencent\微信web开发者工具\package.nw\node_modules\ws\lib\WebSocket.js:138
emitOne @ events.js:115
emit @ events.js:210
addChunk @ _stream_readable.js:263
readableAddChunk @ _stream_readable.js:250
Readable.push @ _stream_readable.js:208
onread @ net.js:595
index.js? [sm]:460 stop
index.js? [sm]:505 deviceId 88:83:5D:56:C8:C9
index.js? [sm]:525 连接成功 {errCode: 0, errMsg: "createBLEConnection:ok"}
index.js? [sm]:460 stop
index.js? [sm]:170 obj {top: false, bottom: true, left: false, right: false, startTime: 1530013063376, …}
index.js? [sm]:598 写入内存时间 1530013063388 bottompress press
index.js? [sm]:630 发送 1530013063388
index.js? [sm]:181 end {top: false, bottom: false, left: false, right: false, isTouch: false, …}
index.js? [sm]:598 写入内存时间 1530013063516 bottompop pop
index.js? [sm]:598 写入内存时间 1530013063540 bottompop pop
index.js? [sm]:630 发送 1530013063641
writeDeviceCharacter.js? [sm]:19 用时 261 {errCode: 10004, errMsg: "writeBLECharacteristicValue:fail:no service"} press
writeDeviceCharacter.js? [sm]:19 用时 182 {errCode: 10004, errMsg: "writeBLECharacteristicValue:fail:no service"} pop
使用IOS测试无任何问题,使用低版本的Android6.0手机也没有问题。
请参考具体的建立连接代码片段如下:
wx.createBLEConnection({
deviceId: deviceId,
// mainThread: false,
// needDiscoryService: true,
success: function (res) {
// wx.hideToast();
console.log('连接成功', res)
if (that.data.ios){
//从连接中获取serviceUUid与characterUUid 苹果机限制必须先获取才能使用
getDeviceService.call(that);
}
else {
that.setData({ tip: `当前连接设备: ${deviceName}`, isConnected: true, isModal: false, selectDevice: '' })
wx.showToast({
title: '连接成功',
})
that.closeModal();
that.selfData.connecting = false
}
不知是我代码调用有问题,还是手机有何设置未开启
请遇到类似问题的朋友方便的话帮忙答复一下,谢谢!
解决了么
你好,请问你的问题解决了吗?
我的也是,同一款手机,同一个蓝牙设备,有时候是可以成功的,有时候就报10012,或者写特征值错误,为啥有时候成功,有时候失败呢
感谢您的答复,
我这边特征值肯定是开启了写权限的而且单次写入才2个字节而已。
实际上,我测试发现 createBLEConnection 这个API 在 android 高版本上有很大问题,先后在华为P9 Android7.1 , 360 N6 android 7.1 上测试过。连都连不上。 在华为mate 10 Android8.0 上,首次也是连不上,第二次才可以连接的上,但是写特征值的时候就报 “writeBLECharacteristicValue:fail:no service”
小程序在android上建立蓝牙连接失败后,不要立马重试,稍等一会再试。 另外你提到即使建立连接也无法写入特征值,请先确认以下几点:
该特征值是否已开启write权限
单次写入数据是否超过20字节
实际上,我又做了个实验,当搜到蓝牙设备后,接着调用 getBLEDeviceSerivces , 结果返回错误1004 的错误码
也就是说,在华为Mate10 上,可以成功连接蓝牙设备,但是getBLEDeviceSerivces 返回失败,所以当我调用writeBLECharacteristicValue 的时候才返回了 no service的错误。
请问这个问题你解决了吗?