熊晨灃藍牙實戰--小程序藍牙連接2.0
微信小程序藍牙連接2.0說明:
1、本版本區分了ANDROID和IOS系統下藍牙連接的不同方式。
2、兼容了更多情況下的鏈接包括:
(1)未開啟設備藍牙,當監聽到開啟了藍牙後自動開始連接。
(2)初始化藍牙失敗後每3000ms自動重新初始化藍牙適配器。
(3)安卓端開啟藍牙適配器掃描失敗,每3000ms自動重新開啟。
(4)IOS端獲取已連接藍牙設備為空,每3000ms自動重新獲取。
(5)安卓端藍牙開始鏈接後中斷掃描,連接失敗了,重新開始掃描。
(6)IOS端開始連接設備後,停止獲取已連接設備,連接失敗自動重新開啟獲取。
(7)連接成功後,關閉系統藍牙,藍牙適配器重置。
(8)連接成功後,關閉系統藍牙,再次打開藍牙,自動重新開始連接。
(10)連接成功後,最小化小程序(連接未中斷),打開小程序顯示已連接。
(11)連接成功後,殺掉小程序進程,連接關閉,自動重新開始掃描(獲取)。
3、想起來了再來更新....。
4、流程圖,明天或後天或...誰有空幫我畫一下也行。
我的連接是在App.js中做的。
在App.js中的onLaunch觸發是調用 init()方法。
init代碼:
init: function (n) { this.list = []; this.serviceId = "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"; this.serviceId_2 = "00001803-0000-1000-8000-00805F9B34FB"; this.serviceId_3 = "00001814-0000-1000-8000-00805F9B34FB"; this.serviceId_4 = "00001802-0000-1000-8000-00805F9B34FB"; this.serviceId_5 = "00001804-0000-1000-8000-00805F9B34FB"; this.serviceId_6 = "00001535-1212-EFDE-1523-785FEABCD123"; this.characterId_write = "6E400042-B5A3-F393-E0A9-E50E24DCCA9E"; this.characterId_read = "6E400012-B5A3-F393-E0A9-E50E24DCCA9E"; this.connectDeviceIndex = 0; this.isGettingConnected = false; this.isDiscovering = false; this.isConnecting = false; this.connectedDevice = {}; console.log(‘init state‘, this.connectedDevice.state); if (!this.connectedDevice.state || n == 200) { this.connectedDevice.state = false; this.connectedDevice.deviceId = ‘‘; this.adapterHasInit = false } this.startConnect(); }
說明:
1、 serviceId_2~6 是我已知的想要連接的藍牙設備的serviceId可以只寫一個。
2、characterId_write 是我已知的想要連接的藍牙設備寫入數據的特征值。
3、characterId_read是我已知的想要連接的藍牙設備讀取數據的特征值。
(以上3個都是為了做比對,真實的操作按照獲取到的sericeid, characterid為準)。
4、connectedDevice 是已連接了的設備信息對象。
init完成後開始調用連接 startConnect();
startConnect代碼:
startConnect: function () { var that = this; if (that.connectedDevice.state) return; that.connectedDevice.deviceId = ""; that.connectedDevice.state = false; // 如果適配器已經初始化不在調用初始化(重復初始化會報錯) if (this.adapterHasInit == undefined || this.adapterHasInit) return; wx.showLoading({ title: ‘初始化藍牙‘, duration: 2000 }); // 開啟藍牙適配器狀態監聽 this.listenAdapterStateChange(); // 初始化藍牙適配器狀態(必須步驟,否則無法進行後續的任何操作) wx.openBluetoothAdapter({ success: function (res) { console.log("初始化藍牙適配器成功"); that.getBluetoothAdapterState(); that.adapterHasInit = true; }, fail: function (err) { console.log(err); wx.showLoading({ title: ‘請開藍牙‘, icon: ‘loading‘, duration: 2000 }) } }); }
說明:這段有註釋,就不多說了,比較簡單。
在初始化藍牙適配器狀態成功後調用getBluetoothAdapterState()方法。
getBluetoothAdapterState代碼:
getBluetoothAdapterState: function () {
var that = this;
wx.getBluetoothAdapterState({
success: function (res) {
console.log(res);
var available = res.available;
that.isDiscovering = res.discovering;
if (!available) {
wx.showLoading({
title: ‘請開藍牙‘,
icon: ‘loading‘,
duration: 2000
})
} else {
if (!that.connectedDevice[‘state‘]) {
that.judegIfDiscovering(res.discovering);
}
}
},
fail: function (err) {
console.log(err);
}
})
}
說明:此方法是用來獲取當前藍牙狀態。
當檢測到藍牙可用時調用judegIfDiscovering方法。
judegIfDiscovering代碼:
judegIfDiscovering: function (discovering) {
var that = this;
if (this.isConnectinng) return;
wx.getConnectedBluetoothDevices({
services: [that.serviceId],
success: function (res) {
console.log("獲取處於連接狀態的設備", res);
var devices = res[‘devices‘];
if (devices[0]) {
if (that.isAndroidPlatform) {
wx.showToast({
title: ‘藍牙連接成功‘,
icon: ‘success‘,
duration: 2000
});
} else {
that.getConnectedBluetoothDevices(256);
}
} else {
if (discovering) {
wx.showLoading({
title: ‘藍牙搜索中‘
})
} else {
if (that.isAndroidPlatform) {
that.startBluetoothDevicesDiscovery();
} else {
that.getConnectedBluetoothDevices(267);
}
}
}
},
fail: function (err) {
console.log(‘getConnectedBluetoothDevices err 264‘, err);
if (that.isAndroidPlatform) {
that.startBluetoothDevicesDiscovery();
} else {
that.getConnectedBluetoothDevices(277);
}
}
});
}
說明:
1、此方法是用來判斷是否正在掃描。
2、isAndroidPlatform 是通過小程序的getSystemInfo獲取到的判斷是安卓設備還是IOS設備。
如果是安卓設備調用startBluetoothDevicesDiscovery()開啟掃描,如果是IOS設備調用getConnectedBluetoothDevices() 開啟獲取已配對的藍牙設備。
startBluetoothDevicesDiscovery代碼:
startBluetoothDevicesDiscovery: function () {
var that = this;
if (!this.isAndroidPlatform) return;
if (!this.connectedDevice[‘state‘]) {
wx.getBluetoothAdapterState({
success: function (res) {
console.log(res);
var available = res.available;
that.isDiscovering = res.discovering;
if (!available) {
wx.showLoading({
title: ‘請開藍牙‘,
icon: ‘loading‘,
duration: 2000
})
} else {
if (res.discovering) {
wx.showLoading({
title: ‘藍牙搜索中‘
})
} else {
wx.startBluetoothDevicesDiscovery({
services: [],
allowDuplicatesKey: true,
success: function (res) {
that.onBluetoothDeviceFound();
wx.showLoading({
title: ‘藍牙搜索中‘
})
},
fail: function (err) {
if (err.isDiscovering) {
wx.showLoading({
title: ‘藍牙搜索中‘
})
} else {
that.startDiscoveryTimer = setTimeout(function () {
if (!that.connectedDevice.state) {
that.startBluetoothDevicesDiscovery();
}
}, 5000)
}
}
});
}
}
},
fail: function (err) {
console.log(err);
}
})
}
說明:
1、僅在安卓端設備上開啟掃描附近藍牙設備。
2、在開啟成功的回調中開啟發現新藍牙設備的事件監聽onBluetoothDeviceFound()。
onBluetoothDeviceFound代碼:
[mw_shl_code=javascript,true]onBluetoothDeviceFound: function () {
var that = this;
wx.onBluetoothDeviceFound(function (res) {
console.log(‘new device list has founded‘);
if (res.devices[0]) {
var name = res.devices[0][‘name‘];
if (name.indexOf(‘FeiZhi‘) != -1) {
var deviceId = res.devices[0][‘deviceId‘];
console.log(deviceId);
that.deviceId = deviceId;
if (!that.isConnecting) {
that.startConnectDevices();
}
}
}
})
}
說明:
1、此處對已發現的藍牙設備根據name屬性進行了過濾。
2、當篩選出含有需要連接的設備的name屬性的設備是獲取到deviceId,開始連接調用startConnectDevices()方法。
startConnectDevices代碼:
startConnectDevices: function (ltype, array) {
var that = this;
clearTimeout(this.getConnectedTimer);
clearTimeout(this.startDiscoveryTimer);
this.getConnectedTimer = null;
this.startDiscoveryTimer = null;
this.isConnectinng = true;
wx.showLoading({
title: ‘正在連接‘
});
that.stopBluetoothDevicesDiscovery();
wx.createBLEConnection({
deviceId: that.deviceId,
success: function (res) {
console.log(‘連接成功‘, res);
wx.showLoading({
title: ‘正在連接‘
});
that.connectedDevice.state = true;
that.connectedDevice.deviceId = that.deviceId;
if (res.errCode == 0) {
setTimeout(function () {
that.getService(that.deviceId);
}, 5000)
}
wx.onBLEConnectionStateChange(function (res) {
console.log(‘連接變化‘, res);
that.connectedDevice.state = res.connected;
that.connectedDevice.deviceId = res.deviceId;
if (!res.connected) {
that.init(‘200‘);
}
});
},
fail: function (err) {
console.log(‘連接失敗:‘, err);
wx.hideLoading();
if (ltype == ‘loop‘) {
array = array.splice(0, 1);
console.log(array);
that.loopConnect(array);
} else {
if (that.isAndroidPlatform) {
that.startBluetoothDevicesDiscovery();
} else {
that.getConnectedBluetoothDevices(488);
}
}
},
complete: function () {
that.isConnectinng = false;
}
});
}
說明:
1、開啟連接後終止掃描(獲取已配對)方法。
2、根據deviceId創建低功耗藍牙連接。如果連接成功,就繼續做後續讀寫操作。
3、如果連接失敗根據設備系統分別調用startBluetoothDevicesDiscovery() 或 getConnectedBluetoothDevices();
getConnectedBluetoothDevices代碼:
getConnectedBluetoothDevices: function (n) {
var that = this;
that.isGettingConnected = true;
wx.showLoading({
title: ‘藍牙搜索中‘
});
wx.getConnectedBluetoothDevices({
services: [that.serviceId],
success: function (res) {
console.log("獲取處於連接狀態的設備", res);
var devices = res[‘devices‘],
flag = false,
index = 0,
conDevList = [];
devices.forEach(function (value, index, array) {
if (value[‘name‘].indexOf(‘FeiZhi‘) != -1) {
// 如果存在包含FeiZhi字段的設備
flag = true;
index += 1;
conDevList.push(value[‘deviceId‘]);
that.deviceId = value[‘deviceId‘];
}
});
if (flag) {
that.connectDeviceIndex = 0;
that.loopConnect(conDevList);
} else {
that.failToGetConnected();
}
},
fail: function (err) {
that.failToGetConnected();
},
complete: function () {
that.isGettingConnected = false;
}
});
}
說明:如果獲取藍牙已配對的藍牙設備失敗了,或獲取到的列表為空調用failToGetConnected();
failToGetConnected代碼:
failToGetConnected: function () {
var that = this;
if (!that.getConnectedTimer) {
clearTimeout(that.getConnectedTimer);
that.getConnectedTimer = null;
}
that.getConnectedTimer = setTimeout(function () {
wx.getBluetoothAdapterState({
success: function (res) {
console.log(res);
var available = res.available;
if (!available) {
wx.showLoading({
title: ‘請開藍牙‘,
icon: ‘loading‘,
duration: 2000
})
} else {
if (!that.connectedDevice[‘state‘]) {
that.getConnectedBluetoothDevices();
}
}
},
fail: function (err) {
console.log(err);
}
})
}, 5000);
}
說明:
1、該方法調用成功後返回的devices是一個數組包含多個已經系統配對的藍牙設備。
2、如果devices列表獲取到調用loopConnect()方法開始遞歸調用連接藍牙設備。
loopConnect代碼:
loopConnect: function (array) {
var that = this;
var listLen = array.length;
if (array[0]) {
that.deviceId = array[0];
if (!that.isConnecting) {
that.startConnectDevices(‘loop‘, array);
}
} else {
console.log(‘已配對的設備小程序藍牙連接失敗‘);
if (!that.isAndroidPlatform) {
that.getConnectedBluetoothDevices(431);
}
}
}
說明:looConnect在創建連接的方法連接失敗後會操作刪除數組的第一個值,然後繼續調用該方法,直到其中所有的設備都連接過。
差點漏了:在app.js的onShow裏調用init()方法。
特別說明:
1、安卓和IOS的藍牙連接在當前版本中推薦采用不同方式。安卓設備直接使用小程序的藍牙連接,取消系統配對。IOS設備先系統配對在打開小程序可以時效秒連接成功。
2、此版本的連接仍然有待完善,連接不會自動終止(需要的可以自己加),會無限掃描重連,直到成功。
3、鏈接成功後的操作如果寫入數據和開啟notify需要同時進行,建議先寫入,後開啟notify。(原因未知,否則必然出現10008錯誤)。
熊晨灃藍牙實戰--小程序藍牙連接2.0