看了網(wǎng)上的幾個例子,發(fā)現(xiàn)寫的都比較亂,正好有業(yè)務(wù)需要,所以就封裝了一個藍牙庫,采用Promise實現(xiàn),將過程變得極其簡單,主要代碼如下:
connectDevice: function () {
return bleComm.open().then(res => {
return bleComm.getStatus();
}).then(res => {
return bleComm.startScan();
}).then(res => {
return bleComm.getDevices();
}).then(res => {
return bleComm.foundDevice();
}).then(res => {
return bleComm.stopScan();
}).then(res => {
return bleComm.connect();
}).then(res => {
return bleComm.getServices();
}).then(res => {
return bleComm.getCharacteristics();
}).then(res => {
return bleComm.registerNotify();
})
}