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代码: