|
ble 模块连接之前是不是一定要 scan 和 getPeripherals 呢
[复制链接]
开发一个物联网的APP,操控蓝牙设备, 用到 BLE 模块,先用 ble.scan, 然后ble.getPeripherals,然后在 ble.connect, 可以连接蓝牙设备。
连接过一次的设备我就把它保存起来,下次就不用再扫描,直接连接节省时间。但是这样总是连接不上,也不显示回调成功还是失败,一直进度条在那里转(进度条是我自己加的)。
所以请教下官方人员,连接设备需要一个怎样的流程。
连接的代码如下(扫描后再连接和保存后再连接,都是这个代码),如果 scan -> getPeripherals -> connect,能连接成功。保存了的设备再用这个代码连接就一直不提示成功或失败。
u.connectDevice = function(deviceuuid, deviceindex, devicename){
u.init();
api.showProgress({
style: 'default',
animationType: 'fade',
title: 'Connecting',
text: 'Wait a moment',
modal: false
});
setTimeout(function(){
ble.connect({
peripheralUUID: deviceuuid
}, function(ret, err) {
if (ret.status) {
api.hideProgress();
api.toast({
msg: 'Connected successfully',
duration: 1000,
location: 'middle'
});
api.sendEvent({
name: 'connectDevice',
extra: {
devicename: devicename,
deviceindex: deviceindex,
macaddr: deviceuuid
}
});
if(deviceindex=='s'){
api.closeWin();
}
} else {
api.hideProgress();
alert("connect error" + err.code);
if(deviceindex=='s'){
api.closeWin();
}
}
});
}, 1000);
}
|
|