小程序使用标准蓝牙接口搜索不到车机蓝牙设备,微信版本:7.0.16 手机:小米mix2 系统:android 9
initBT: function() {
this.printLog("启动蓝牙适配器, 蓝牙初始化")
var that = this;
wx.openBluetoothAdapter({
success: function(res) {
console.log("initBT:",res);
that.findBT();
},
fail: function(res) {
console.log('请先打开蓝牙');
}
})
},
findBT: function() {
this.printLog("打开蓝牙成功.")
var that = this
wx.startBluetoothDevicesDiscovery({
allowDuplicatesKey: false,
interval: 0,
success: function(res) {
wx.showLoading({
title: '正在搜索设备',
})
console.log("startBluetoothDevicesDiscovery:",res);
delayTimer = setInterval(function(){
that.discoveryBT() //3.0 //这里的discovery需要多次调用
}, 1000);
console.log("delayTimer:"+delayTimer)
setTimeout(function () {
if (isFound) {
return;
} else {
wx.hideLoading();
console.log("搜索设备超时");
wx.stopBluetoothDevicesDiscovery({
success: function (res) {
console.log('连接蓝牙成功之后关闭蓝牙搜索');
}
})
clearInterval(delayTimer)
wx.showModal({
title: '搜索设备超时',
content: '请检查蓝牙设备是否正常工作,Android手机请打开GPS定位.',
showCancel: false
})
console.log("搜索设备超时,请打开GPS定位,再搜索")
return
}
}, 15000);
},
fail: function(res) {
that.printLog("蓝牙设备服务发现失败: " + res.errMsg);
}
})
},
discoveryBT: function() {
var that = this
wx.getBluetoothDevices({
success: function(res) {
var list = res.devices;
console.log(list);
if(list.length <= 0){
return ;
}
var devices = [];
for (var i = 0; i < list.length; i++) {
//that.data.inputValue:表示的是需要连接的蓝牙设备ID,
//简单点来说就是我想要连接这个蓝牙设备,
//所以我去遍历我搜索到的蓝牙设备中是否有这个ID
var name = list[i].name || list[i].localName;
console.log("name:",name+" RSSI:"+list[i].RSSI)
// if(this.isEmpty(name)){
if (name!="") {
continue;
}
if(list[i].RSSI != 0){//name.indexOf('JL') >= 0 &&
console.log(list[i]);
devices.push(list[i]);
}
}
console.log('总共有' + devices.length + "个设备需要设置")
if (devices.length <= 0) {
return;
}
that.connectBT(devices);
},
fail: function() {
console.log('搜索蓝牙设备失败');
}
})
},
麻烦提供出现问题的具体机型、微信版本号、系统版本号,以及能复现问题的代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)