当小程序第一次成功搜索到并了连接蓝牙后,成功调用wx.closeBLEConnection()断开蓝牙释放资源后,再次重新搜索连接蓝牙一直返回错误代码,(经过测试部分概率下
手动开关蓝牙再次搜索连接可以成功),我这里初步判断可能是wx.closeBLEConnection()调用后没有彻底断开蓝牙连接,导致再次搜索时无法连接,所以想问:小程序官方
是否有小程序彻底清除已连接的蓝牙连接资源的API或者解决这个问题的方案。调用wx.startBluetoothDevicesDiscovery() 能否搜索到已连接或者未成功
断开的蓝牙设备信息进行回调断开操作。 微信版本8.0.2,微信开发者工具版本1.05.2102010
function DiscoverBLE(ms) {
return new Promise(function(resolve, reject) {
if (m_Deviceid == "") {
DevicesDiscovery(resolve, ms);
} else {
wx.closeBLEConnection({
deviceId: m_Deviceid,
success: function(res) {
console.log('DisconnectBLE', res)
connectSuccess = '';
wx.closeBluetoothAdapter();
setTimeout(function() {
DevicesDiscovery(resolve, ms);
}, 2000);
},
fail: (res) => {
connectSuccess = '';
wx.closeBluetoothAdapter();
setTimeout(function() {
DevicesDiscovery(resolve, ms);
}, 2000);
}
})
receive_apdu = ""
}
})
}
function ConnectDeviceid(s_Deviceid) {
m_Deviceid = s_Deviceid;
connectSuccess = '';
console.log('m_Deviceid:', m_Deviceid);
wx.createBLEConnection({
deviceId: m_Deviceid,
success: (res) => {
console.log('createBLEConnection success:')
console.log(' ', res)
setTimeout(() => {
getBLEDeviceServices();
}, 4000)
wx.onBLEConnectionStateChange(function(res) {
console.log(`device ${res.deviceId} state has changed, connected: ${res.connected}`);
if ((!res.connected) && (connectSuccess == 1)) {
DisconnectBLE();
}
})
},
fail: (res) => {
console.log('createBLEConnection fail:')
console.log(' ', res)
connectSuccess = '0';
if (res.errCode === 10003) {
console.log('res.errCode === 10003')
wx.closeBLEConnection({
deviceId: m_Deviceid,
success: function(res) {
console.log('DisconnectBLE success', res)
connectSuccess = '0';
},
fail: (res) => {
console.log('DisconnectBLE fail', res)
wx.closeBluetoothAdapter();
}
})
}
wx.showToast({
title: '连接失败',
icon: '错误',
duration: 2000
})
}
})
}
function CheckConnectBLE(mode) {
return new Promise(function(resolve, reject) {
var times = 0;
var i = setInterval(function() {
if (times >= 30) {
resolve('0');
clearInterval(i)
} else {
console.log("times=", times);
if (connectSuccess == '1') {
times++;
if (mode == 1) {
CheckByMode1();
ReceiveCheckByMode1(2000, mode).then(function(_receive_apdu) {
if (_receive_apdu == '01') {
times = 30;
resolve('1');
}
});
} else if (mode == 2) {
SendAPDU('00');
ReceiveAPDU(2000, mode).then(function(_receive_apdu) {
if (_receive_apdu == '01') {
times = 30;
resolve('1');
}
});
}
} else if (connectSuccess == '0') {
times = 30;
resolve('0');
} else {
console.log("wait connect...");
}
}
}, 2000);
});
}
1. 应该不是假断开的问题
2. startBluetoothDevicesDiscovery能否搜索到已连接或者未成功断开的蓝牙设备?不能
3. 如果说的是Android手机的问题,建议试试最开始startBluetoothDevicesDiscovery的时候多传个参数refreshCache:false试试;