- 当前 Bug 的表现(可附上截图)
小米9 真机调试蓝牙 总出现10004 code ,不知道原因,偶尔不会出现
14:16:40.702 index.js:40 openBluetoothAdapter success {errMsg: "openBluetoothAdapter:ok"}
14:16:40.913 index.js:75 startBluetoothDevicesDiscovery success {errCode: 0, errMsg: "startBluetoothDevicesDiscovery:ok", isDiscovering: true}
14:16:43.572 index.js:115 createBLEConnection success {"errCode":0,"errMsg":"createBLEConnection:ok"}
14:16:44.005 index.js:152 getBLEDeviceServices 34:03:DE:58:7C:3D fail {"errCode":10004,"errMsg":"getBLEDeviceServices:fail:no service"}
- 预期表现
- 复现路径
- 提供一个最简复现 Demo
官方蓝牙demo修改的
openBluetoothAdapter() {
wx.openBluetoothAdapter({
success: (res) => {
console.log('openBluetoothAdapter success', res)
this.startBluetoothDevicesDiscovery()
},
fail: (res) => {
if (res.errCode === 10001) {
wx.onBluetoothAdapterStateChange(function (res) {
console.log('onBluetoothAdapterStateChange', res)
if (res.available) {
this.startBluetoothDevicesDiscovery()
}
})
}
}
})
},
getBluetoothAdapterState() {
wx.getBluetoothAdapterState({
success: (res) => {
console.log('getBluetoothAdapterState', res)
if (res.discovering) {
this.onBluetoothDeviceFound()
} else if (res.available) {
this.startBluetoothDevicesDiscovery()
}
}
})
},
startBluetoothDevicesDiscovery() {
if (this._discoveryStarted) {
return
}
this._discoveryStarted = true
wx.startBluetoothDevicesDiscovery({
allowDuplicatesKey: true,
success: (res) => {
console.log('startBluetoothDevicesDiscovery success', res)
this.onBluetoothDeviceFound()
},
})
},
stopBluetoothDevicesDiscovery() {
wx.stopBluetoothDevicesDiscovery()
},
onBluetoothDeviceFound() {
wx.onBluetoothDeviceFound((res) => {
res.devices.forEach(device => {
if (!device.name && !device.localName) {
return
}
const foundDevices = this.data.devices
const idx = inArray(foundDevices, 'deviceId', device.deviceId)
const data = {}
if (idx === -1) {
data[`devices[${foundDevices.length}]`] = device
} else {
data[`devices[${idx}]`] = device
}
this.setData(data)
})
})
},
onBLEConnectionStateChange(){
wx.onBLEConnectionStateChange(function (res) {
// 该方法回调中可以用于处理连接意外断开等异常情况
console.log(`device ${res.deviceId} state has changed, connected: ${res.connected}`)
})
},
createBLEConnection(e) {
var that = this
const ds = e.currentTarget.dataset
const deviceId = ds.deviceId
const name = ds.name
wx.createBLEConnection({
deviceId,
success: (res) => {
console.log('createBLEConnection success', JSON.stringify(res))
this.setData({
connected: true,
name,
deviceId,
})
//this.getBLEDeviceServices(deviceId)
setTimeout(function () { that.getBLEDeviceServices(deviceId) }, 300)
setInterval(function () { that.onBLEConnectionStateChange()},5000)
}
})
this.stopBluetoothDevicesDiscovery()
},
closeBLEConnection() {
wx.closeBLEConnection({
deviceId: this.data.deviceId
})
this.setData({
connected: false,
chs: [],
canWrite: false,
})
},
getBLEDeviceServices(deviceId) {
var that =this
wx.getBLEDeviceServices({
deviceId,
success: (res) => {
console.log('getBLEDeviceServices ' + deviceId+ ' success', JSON.stringify(res))
for (let i = 0; i < res.services.length; i++) {
if (res.services[i].isPrimary && res.services[i].uuid.indexOf("0000FFF0") != -1) {
this.getBLEDeviceCharacteristics(deviceId, res.services[i].uuid)
}
}
},
fail:(res)=>{
console.log('getBLEDeviceServices ' + deviceId + ' fail', JSON.stringify(res))
//setTimeout(function () { that.getBLEDeviceServices(deviceId)} , 3000 )
}
})
},
getBLEDeviceCharacteristics(deviceId, serviceId) {
wx.getBLEDeviceCharacteristics({
deviceId,
serviceId,
success: (res) => {
console.log('getBLEDeviceCharacteristics2 success', JSON.stringify(res))
//console.log('getBLEDeviceCharacteristics1 success', res.characteristics)
for (let i = 0; i < res.characteristics.length; i++) {
let item = res.characteristics[i]
if (item.properties.read) {
wx.readBLECharacteristicValue({
deviceId,
serviceId,
characteristicId: item.uuid,
})
}
if (item.properties.write && item.uuid.indexOf("0000FFF2") != -1) {
this.setData({
canWrite: true,
ble_status:1,
})
this._deviceId = deviceId
this._serviceId = serviceId
this._characteristicId = item.uuid
this.writeBLECharacteristicValue()
}
if (item.properties.notify || item.properties.indicate) {
wx.notifyBLECharacteristicValueChange({
deviceId,
serviceId,
characteristicId: item.uuid,
state: true,
})
}
}
},
fail(res) {
console.error('getBLEDeviceCharacteristics', res)
}
})
// 操作之前先监听,保证第一时间获取数据
wx.onBLECharacteristicValueChange((characteristic) => {
const idx = inArray(this.data.chs, 'uuid', characteristic.characteristicId)
const data = {}
if (idx === -1) {
data[`chs[${this.data.chs.length}]`] = {
uuid: characteristic.characteristicId,
value: ab2hex(characteristic.value)
}
} else {
data[`chs[${idx}]`] = {
uuid: characteristic.characteristicId,
value: ab2hex(characteristic.value)
}
}
// data[`chs[${this.data.chs.length}]`] = {
// uuid: characteristic.characteristicId,
// value: ab2hex(characteristic.value)
// }
this.setData(data)
})
},
writeBLECharacteristicValue() {
// 向蓝牙设备发送一个0x00的16进制数据
console.log('开始写入数据')
let buffer = new ArrayBuffer(20)
let dataView = new DataView(buffer)
// dataView.setUint8(0, Math.random() * 255 | 0)
for (let i = 0; i < 20; i++) {
dataView[i] = i
}
wx.writeBLECharacteristicValue({
deviceId: this._deviceId,
serviceId: this._serviceId,
characteristicId: this._characteristicId,
value: buffer,
success: (res) => {
console.log('writeBLECharacteristicValue success', JSON.stringify(res))
this.writeBLECharacteristicValue()
},
fail: (res) => {
console.log('writeBLECharacteristicValue fail', JSON.stringify(res))
//writeBLECharacteristicValue()
}
})
},
closeBluetoothAdapter() {
wx.closeBluetoothAdapter()
this._discoveryStarted = false
this.setData({
ble_status: 0,
})
},
看日志没有看出啥...
提供下微信,给个测试包加些日志看看?thx
最近开发小程序蓝牙碰到很多问题,第一是蓝牙下发一包数据很慢,两秒一包,所以我想单纯的测试一下蓝牙下载速度,于是在官方demo基础是做了一个蓝牙下载的测试,期间升级了开发工具,之后频繁出现10004错误代码,偶尔才能成功下载一次。望课代表予以解答。在线等,急
你好,麻烦在手机微信那里上传下日志: 我->设置->帮助与反馈右上角有个上报日志的入口,麻烦提供一下微信号,时间点