详细问题描述
蓝牙的相关api 运行到微信小程序端没有错误的体现, 一套流程完整走完. 完成了需求的解决. 反观 app端 真机测试 uni.getBluetoothDevices() 方法, 在app端运行时报错. 在蓝牙适配完成后延时调用 uni.startBluetoothDevicesDiscovery(监听蓝牙发现) 的函数, 不延时直接不监听. 不知道什么原因. 在这段执行的过程中, 看到了监听函数的作用, 打印了设备的发现. 在我执行点击事件, 执行获取全部已发现的蓝牙设备(uni.getBlueBoothDevices()) 直接报错, 没有监听的阶段不会报错, 结果是 devices 就是一个 [] 数组. 这些怪异的表现在 小程序都没有出现. 在线等解决. 急急急... QAQ
[内容]
重现步骤
let bArr = [] // uni-app 方法报错了. 所以我用了个东西存东西, 下步调用
export default {
data() {
return {}
},
methods: {
// 获取蓝牙设备列表
getMyLock() {
},
// 开锁操作
openLock(index, item_id) {
uni.stopBluetoothDevicesDiscovery({
success: (res) => {
console.log('--停止蓝牙搜索')
}
})
uni.getBluetoothAdapterState({
success: (res) => {
console.log('--获取搜索状态成功')
console.log(JSON.stringify(res))
},
fail: () => {
console.log('--获取搜索状态失败')
}
})
console.log('***', JSON.stringify(bArr))
let b_uuidArr = []
bArr.forEach(item => {
b_uuidArr.push(item.deviceId)
})
console.log(b_uuidArr)
// 找到所有已发现的蓝牙 T_T 这里的方法, 在监听蓝牙设备发现后, 往其中添加蓝牙项目, 这里直接报错, 不知道是 uni.onBluetoothDeviceFound() 的监听执行往设备列表中放设备出了问题还是就 getBluetoothDevices 出了问题.摸不着头脑
// uni.getBluetoothDevices({
// success(res) {
// console.log(JSON.stringify(res))
// if (res.devices[0]) {
// console.log(ab2hex(res.devices[0].advertisData))
// }
//
// }
// })
},
// 初始化蓝牙适配
openBluetoothAdapter() {
uni.openBluetoothAdapter({
success: (res) => {
console.log('--openBluetoothAdapter(初始化蓝牙适配)成功');
console.log(JSON.stringify(res))
setTimeout(() => {
// 开始搜索附件蓝牙设备
this.startBluetoothDevicesDiscovery()
}, 0)
// 监听本机蓝牙状态变化
this.onBluetoothAdapterStateChange();
},
fail: (err) => {
console.log('--初始化蓝牙适配失败! 蓝牙不可用!')
}
})
},
// 监听本机蓝牙适配发生改变
onBluetoothAdapterStateChange() {
uni.onBluetoothAdapterStateChange(res => {
console.log('--onBluetoothAdapterStateChange(监听本机蓝牙适配状态变化)')
console.log(JSON.stringify(res))
})
},
// 搜索附件蓝牙
startBluetoothDevicesDiscovery() {
uni.startBluetoothDevicesDiscovery({
// services: ['0000ffe0-0000-1000-8000-00805f9b34fb', '0000FFE0-0000-1000-8000-00805F9B34FB', 'ffe0', 'FFE0'],
success: (res) => {
console.log('--startBluetoothDevicesDiscovery(开始搜索蓝牙设备)')
console.log(JSON.stringify(res))
// 监听发现新蓝牙设备, 并将设备添加到所有设备的搜索组中
this.onBluetoothDeviceFound()
}
})
},
// 监听新蓝牙的发现
onBluetoothDeviceFound() {
uni.onBluetoothDeviceFound(function(devices) {
console.log('--onBluetoothDeviceFound(监听发现新蓝牙)')
console.log(JSON.stringify(devices))
// console.log(this.ab2hex(devices[0].advertisData))
bArr = bArr.concat(devices.devices)
console.log('---bArr push', JSON.stringify(bArr))
})
},
ab2hex(buffer) {
let hexArr = Array.prototype.map.call(
new Uint8Array(buffer),
(bit) => ('00' + bit.toString(16)).slice(-2)
)
return hexArr.join('')
}
},
onShow() {
this.openBluetoothAdapter()
},
onLoad() {
this.getMyLock()
}
};
[结果]
[ERROR] : TypeError: null is not an object (evaluating 't.status')filename:file:///var/mobile/Containers/Data/Application/05A30A5F-6A0E-4663-BE24-9CF6DC58E9AB/Documents/Pandora/apps/12452E89FA2AB385C8AA05CAF17BFFB8/www/__uniappservice.htmllineno:3
[期望]
[如果语言难以表述清晰,拍一个视频或截图,有图有真相]
期望能够完美解决这种怪异的bug
IDE运行环境说明
[HBuilder 或 HBuilderX]
最新
[windows版本号]
window7
uni-app运行环境说明
运行 app 及 小程序
App运行环境说明
[iOS版本号]
12.1.4
[手机型号]
苹果6sp
附件
联系方式
[QQ] 1308689683