微信小程序的蓝牙使用第一步是调用openBluetoothAdapter,但是有时候现在 版本的openBluetoothAdapter有时候不会返回任何结果()可能是我程序的问题)
那么设置一个标记位isUserOpenAdapter 调用openBluetoothAdapter,再设置一个3s延时器,如果openBluetoothAdapter有返回值则把isUserOpenAdapter置为true,
当延时器方法开始执行时,也就是openBluetoothAdapter没有返回值,那么这个时候调用startBluetoothDevicesDiscovery。
wx.openBluetoothAdapter({
success: function (res) {
isUserOpenAdapter=true;
console.log("------success openBluetoothAdapter------");
console.log(res);
//开始搜索
// wx.showToast({
// title: '搜索中',
// icon: 'none',
// duration: 5000
// })
thisapp.startDiscover(thisapp);
},
fail: function (res) {
isUserOpenAdapter = true;
if(res.errCode=='10001'){//当前蓝牙适配器不可用
// wx.showToast({
// title: '请打开蓝牙权限',
// icon: 'none',
// duration: 2000
// })
}
if (res.errCode == '10009') {//Android 系统特有,系统版本低于 4.3 不支持 BLE
wx.showToast({
title: '该系统不支持蓝牙4.0',
icon: 'none',
duration: 2000
})
}
console.log("蓝牙打开失败");
//fail
console.log("----fail openBluetoothAdapter" + res.errCode);
WxNotificationCenter.postNotificationName('Notificationmbleadapterfail', res);
},
complete: function (res) {
//complete
console.log('----complete openBluetoothAdapter-----');
}
});
setTimeout(function () {
if (!isUserOpenAdapter) {//openBluetoothAdapter未返回结果
console.log('----openBluetoothAdapter未返回结果');
thisapp.startDiscover(thisapp);
}
}, 3000)