1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
| const app = getApp()
function inArray(arr, key, val) { for (let i = 0; i < arr.length; i++) { if (arr[i][key] === val) { return i; } } return -1; }
// ArrayBuffer转16进度字符串示例 function ab2hex(buffer) { var hexArr = Array.prototype.map.call( new Uint8Array(buffer), function (bit) { return ('00' + bit.toString(16)).slice(-2) } ) return hexArr.join(''); }
Page({ data: { devices: [], connected: false, chs: [], },
//首先是IOS指定设备的 connectBLE() { //先初始化(在打开蓝牙之后也执行了搜寻的任务) this.openBluetoothAdapter()
//然后列出所有的设备ID,安卓是Mac地址,IOS是设备的UUID //console.log()
},
//1.初始化蓝牙模块 openBluetoothAdapter() { console.log("初始化蓝牙") wx.openBluetoothAdapter({ success: (res) => { //初始化成功 console.log('openBluetoothAdapter success', res) this.startBluetoothDevicesDiscovery() //开始搜寻周围的设备 }, fail: (res) => { wx.showModal({ title: '温馨提示', content: '请检查手机蓝牙是否打开', showCancel: false, }); } }) },
//2.开始搜寻周围的设备(一般在连接后使用wx.stopBluetoothDevicesDiscovery停止搜索) startBluetoothDevicesDiscovery() { console.log("开始搜寻周围的设备") if (this._discoveryStarted) { return //一直搜索 } this._discoveryStarted = true console.log('***************************************') wx.startBluetoothDevicesDiscovery({ allowDuplicatesKey: true, //允许重复上报同意设备(RSSI值可能会不同) success: (res) => { console.log('startBluetoothDevicesDiscovery success', res) this.onBluetoothDeviceFound() //监听寻找到新设备的事件 }, })
},
//3.监听寻找新设备的事件 onBluetoothDeviceFound() { console.log("监听寻找新设备的事件") wx.onBluetoothDeviceFound(function (devices) { console.log(devices)
if (devices.name == 'HitManTube') { this.stopBluetoothDevicesDiscovery(); console.log("device found") } }) wx.onBluetoothDeviceFound((res) => { res.devices.forEach(device => { console.log("Name:" + device.name)
if (!device.name && !device.localName) { return }
const foundDevices = this.data.devices const idx = inArray(foundDevices, 'deviceId', device.deviceId) const data = {} if (idx === -1) { data[`devices[${foundDevices.length}]`] = device } else { data[`devices[${idx}]`] = device } this.setData(data)
if (device.name == "HitManTube") { console.log("找到相应的ID")
this.createBLEConnection(device.name) }
}) }) },
//4.连接BLE设备(可直接传入之前小程序连接的蓝牙设备) createBLEConnection(e) { console.log("开始连接设备!!" + e) const ds = e.currentTarget.dataset const deviceId = ds.deviceId const name = ds.name wx.createBLEConnection({ deviceId, success: (res) => { this.setData({ connected: true, name, deviceId, }) this.getBLEDeviceServices(deviceId) //获取蓝牙设备所有服务 } }) this.stopBluetoothDevicesDiscovery() //连接之后停止蓝牙搜索 },
createBLEConnection(founddeviceId) { console.log("开始连接设备!!" + e) deviceId = founddeviceId wx.createBLEConnection({ deviceId, success: (res) => { this.setData({ connected: true, name, deviceId, }) this.getBLEDeviceServices(deviceId) //获取蓝牙设备所有服务 } }) this.stopBluetoothDevicesDiscovery() //连接之后停止蓝牙搜索 }, //5.停止寻找蓝牙设备(在连接之后调用,因为搜寻蓝牙设备比较耗费系统资源) stopBluetoothDevicesDiscovery() { console.log("停止搜寻蓝牙设备") wx.stopBluetoothDevicesDiscovery() },
//6.获取蓝牙设备所有服务(serviceID) getBLEDeviceServices(deviceId) { console.log("获取蓝牙设备所有服务(service) ") wx.getBLEDeviceServices({ deviceId, success: (res) => { //如果成功就所有的设备展示出来 for (let i = 0; i < res.services.length; i++) { if (res.services[i].isPrimary) { this.getBLEDeviceCharacteristics(deviceId, res.services[i].uuid) return } } } }) },
//7.获取蓝牙设备某个服务中所有特征值(characteristicID) getBLEDeviceCharacteristics(deviceId, serviceId) { console.log("获取蓝牙设备某个服务中所有特征值(characteristic) ") wx.getBLEDeviceCharacteristics({ deviceId, serviceId, //蓝牙服务 uuid,需要使用 getBLEDeviceServices 获取 success: (res) => { console.log('getBLEDeviceCharacteristics success', res.characteristics)
for (let i = 0; i < res.characteristics.length - 1; i++) { let item = res.characteristics[i] //显示所有的特征值 console.log(item)
if (item.properties.read) { wx.readBLECharacteristicValue({ deviceId, serviceId, characteristicId: item.uuid, }) } if (item.properties.write) { this.setData({ canWrite: true }) this._deviceId = deviceId this._serviceId = serviceId this._characteristicId = item.uuid this.writeBLECharacteristicValue() } if (item.properties.notify || item.properties.indicate) { wx.notifyBLECharacteristicValueChange({ deviceId, serviceId, characteristicId: item.uuid, state: true, }) } } }, fail(res) { console.error('getBLEDeviceCharacteristics', res) } })
// 操作之前先监听,保证第一时间获取数据 wx.onBLECharacteristicValueChange((characteristic) => { const idx = inArray(this.data.chs, 'uuid', characteristic.characteristicId) const data = {} if (idx === -1) { data[`chs[${this.data.chs.length}]`] = { uuid: characteristic.characteristicId, value: ab2hex(characteristic.value) } } else { data[`chs[${idx}]`] = { uuid: characteristic.characteristicId, value: ab2hex(characteristic.value) } } this.setData(data) }) }, })
|