微信小程序 蓝牙遇到的问题

微信小程序的蓝牙使用第一步是调用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)

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值