From 6a47bb25d2a1165e43d8ee169d79c843ab53d6b9 Mon Sep 17 00:00:00 2001 From: qiaocl <3189782663@qq.com> Date: Tue, 7 Feb 2023 16:32:21 +0800 Subject: [PATCH] =?UTF-8?q?L08=20=E8=93=9D=E7=89=99=E8=BF=9E=E6=8E=A5demo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bluetooth_demo_L08.zip | Bin 0 -> 7653 bytes bluetooth_demo_L08/README.md | 8 + bluetooth_demo_L08/app.js | 5 + bluetooth_demo_L08/app.json | 20 ++ bluetooth_demo_L08/app.wxss | 55 ++++ bluetooth_demo_L08/pages/index/index.js | 357 ++++++++++++++++++++++ bluetooth_demo_L08/pages/index/index.json | 4 + bluetooth_demo_L08/pages/index/index.wxml | 46 +++ bluetooth_demo_L08/project.config.json | 52 ++++ bluetooth_demo_L08/sitemap.json | 7 + bluetooth_demo_L08/utils/util.js | 30 ++ 11 files changed, 584 insertions(+) create mode 100644 bluetooth_demo_L08.zip create mode 100644 bluetooth_demo_L08/README.md create mode 100644 bluetooth_demo_L08/app.js create mode 100644 bluetooth_demo_L08/app.json create mode 100644 bluetooth_demo_L08/app.wxss create mode 100644 bluetooth_demo_L08/pages/index/index.js create mode 100644 bluetooth_demo_L08/pages/index/index.json create mode 100644 bluetooth_demo_L08/pages/index/index.wxml create mode 100644 bluetooth_demo_L08/project.config.json create mode 100644 bluetooth_demo_L08/sitemap.json create mode 100644 bluetooth_demo_L08/utils/util.js diff --git a/bluetooth_demo_L08.zip b/bluetooth_demo_L08.zip new file mode 100644 index 0000000000000000000000000000000000000000..88c1551007c66582a05fc7496f3a2b442c83a2eb GIT binary patch literal 7653 zcmb7}2RNL~+Q(OC5z#HtOY|ND(R(keM{i4Xt3@PQbP**w(L2#w5S=9!LG(n879~N1 zAm2((&MPP0_ndEC^UOZi8o#~o=Rfz%{Annopb`NtUOW%wb$`71*M$(#TR3=FL!F(W z5K}8_M`u$NZXqrW!~o!rUl~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kmjv}G@&)Yv3{Q%4KZ&-;IpAoXu3!GA*ibbv1dufcu?{`vxc z0k86jFDDZE-+{ke^WWIkE;C|7YXkCu<=RCMj=I=+7%i#Oh{voM9BmD*b zT|B>(T0+)=ccIj??91q%O8xV|avA;pe??y$UoJ;oYWW#@5$)G#+aJ39mHx}={&Tx{ z89j%8b<}@u9xrq8au%~LxC2h5Kk9&u;eSdP6Y%G1eVIFf$jcS;vEM7|uL^rPr+==mzfd#?)c1cv{cE+oOsteq l;*&>IA3yS0){Cn8>l { + console.log('openBluetoothAdapter success', res) + wx.showToast({ + title: '蓝牙连接中', + icon: "none" + }) + this.startBluetoothDevicesDiscovery() + }, + fail: (res) => { + if (res.errCode === 10001) { + wx.showToast({ + title: '请打开蓝牙', + icon: "none" + }) + // 监听本机蓝牙状态变化的事件 + wx.onBluetoothAdapterStateChange((res) => { + console.log('onBluetoothAdapterStateChange', res) + if (res.available) { + this.startBluetoothDevicesDiscovery() + } + }) + } + } + }) + }, + + // 开始搜寻附近的蓝牙外围设备 + startBluetoothDevicesDiscovery() { + if (this._discoveryStarted) { + return + } + this._discoveryStarted = true + wx.startBluetoothDevicesDiscovery({ + allowDuplicatesKey: true, + interval: 1000, //上报设备的间隔 + services: [ + "FFE0", + ], + success: (res) => { + console.log('startBluetoothDevicesDiscovery success', res) + this.onBluetoothDeviceFound() + }, + }) + }, + // 停止搜寻附近的蓝牙外围设备 + stopBluetoothDevicesDiscovery() { + wx.stopBluetoothDevicesDiscovery() + }, + + // 找到新设备的事件 + onBluetoothDeviceFound() { + wx.onBluetoothDeviceFound((res) => { + res.devices.forEach(device => { + if (!device.name && !device.localName) { + return + } + if (device.name.indexOf('AiLink_') != -1) { + wx.stopBluetoothDevicesDiscovery() //搜索到名称为“AiLink_”的蓝牙后,停止搜寻附近的蓝牙 + const foundDevices = this.data.devices + const idx = inArray(foundDevices, 'deviceId', device.deviceId) + const data = {} + let buff = device.advertisData.slice(-6) + device.mac = new Uint8Array(buff) // 保存广播数据中的mac地址,这是由于iOS不直接返回mac地址 + let tempMac = Array.from(device.mac) + tempMac.reverse() + device.macAddr = ab2hex(tempMac, ':').toUpperCase() + if (idx === -1) { + data[`devices[${foundDevices.length}]`] = device + } else { + data[`devices[${idx}]`] = device + } + this.setData(data) + } + }) + }) + }, + // 连接低功耗蓝牙设备 + createBLEConnection(e) { + wx.showLoading({ + title: '连接中', + }) + const ds = e.currentTarget.dataset + const index = ds.index + this._device = this.data.devices[index] + const deviceId = ds.deviceId + const name = ds.name + this.mac = ds.mac + wx.createBLEConnection({ + deviceId, + success: (res) => { + this.setData({ + connected: true, + name, + deviceId, + }) + console.log("createBLEConnection:success") + this.onBLEConnectionStateChange() + this.getBLEDeviceServices(deviceId) + }, + fail: res => { + wx.hideLoading() + wx.showToast({ + title: '连接失败', + icon: 'none' + }) + } + }) + }, + //监听蓝牙连接状态 + onBLEConnectionStateChange() { + wx.onBLEConnectionStateChange((res) => { + if (!res.connected) { + setTimeout(() => { + wx.showToast({ + title: '连接已断开', + icon: 'none' + }) + }, 500) + this.setData({ + connected: false, + devices: [], + weight: "", + text: "", + imp2: '', + imp3: '', + imp4: '', + imp5: '', + imp7: '', + }) + } + }) + }, + + // 获取蓝牙设备的 serviceId + getBLEDeviceServices(deviceId) { + wx.getBLEDeviceServices({ + deviceId, + success: (res) => { + for (let i = 0; i < res.services.length; i++) { + if (res.services[i].isPrimary && res.services[i].uuid.indexOf('FFE0') > -1) { + wx.showLoading({ + title: '获取设备的UUID成功', + }) + this.getBLEDeviceCharacteristics(deviceId, res.services[i].uuid) + return + } + } + } + }) + }, + + // 获取蓝牙设备某个服务中所有特征值(characteristic) + /** + * read: true读,write: true写,notify: true广播 + */ + getBLEDeviceCharacteristics(deviceId, serviceId) { + let that = this + that._deviceId = deviceId + that._serviceId = serviceId + that._device.serviceId = serviceId + wx.hideLoading() + wx.getBLEDeviceCharacteristics({ + deviceId, + serviceId, + success: (res) => { + console.log('getBLEDeviceCharacteristics success', res.characteristics) + for (let i = 0; i < res.characteristics.length; i++) { + let item = res.characteristics[i]; + if (item.uuid.indexOf('0000FFE1') != -1) { + that.data.uuid1 = item.uuid //主服务 UUID + } else if (item.uuid.indexOf('0000FFE2') != -1) { + that.data.uuid2 = item.uuid //写入设置 + } else if (item.uuid.indexOf('0000FFE3') != -1) { + that.data.uuid3 = item.uuid //监听数据 + } + } + wx.notifyBLECharacteristicValueChange({ + deviceId, + serviceId, + characteristicId: that.data.uuid2, + state: true, + }) + wx.notifyBLECharacteristicValueChange({ + deviceId, + serviceId, + characteristicId: that.data.uuid3, + state: true, + }) + plugin.initPlugin(res.characteristics, this._device) + wx.onBLECharacteristicValueChange((characteristic) => { + let bleData = plugin.parseBleData(characteristic.value) + if (bleData.status == 0) { + console.log("握手成功") + } else if (bleData.status == 1) { + let payload = ab2hex(bleData.data, '') + let dw1 = "kg" + let type = payload.substring(0, 2) + let typeInfo = payload.substring(2, 4) + console.log("测量开始:", payload, type, ) + if (type == "01") { //体脂模式 + let data = parseInt(payload.substring(4, 10), 16) + let num = payload.substring(10, 11) + let dw = payload.substring(11, 12) + if (dw == "1") { + dw1 = "斤" + } + if (num == "1") { + data = data / 10 + } + if (num == "2") { + data = data / 100 + } + if (num == "3") { + data = data / 1000 + } + if (typeInfo == "01") { + that.setData({ + weight: "实时体重是:" + data + dw1 + }) + } + if (typeInfo == "02") { + that.setData({ + weight: "稳定体重是:" + data + dw1 + }) + } + } + if (type == "02") { + if (typeInfo == "02") { + that.setData({ + imp7: "阻抗测量失败" + }) + } + if (typeInfo == "03") { + let mcu = payload.substring(4, 6) + if (mcu == "02") { + that.setData({ + imp2: "左手阻抗:" + parseInt(payload.substring( + 6, 14), 16) + }) + } + if (mcu == "03") { + that.setData({ + imp3: "右手阻抗:" + parseInt(payload.substring( + 6, 14), 16) + }) + } + if (mcu == "04") { + that.setData({ + imp4: "左脚阻抗:" + parseInt(payload.substring( + 6, 14), 16) + }) + } + if (mcu == "05") { + that.setData({ + imp5: "右脚阻抗:" + parseInt(payload.substring( + 6, 14), 16) + }) + } + + if (mcu == "07") { + that.setData({ + imp7: "躯干阻抗:" + parseInt(payload.substring( + 6, 14), 16) + }) + } + } + if (typeInfo == "05") { + let mcu = payload.substring(4, 6) + let imp = parseInt(payload.substring(6, 14), 16) + console.log("阻抗类型:", payload, type, typeInfo, mcu,imp) //阻抗模式 + } + } + if (type == "0f") { + that.setData({ + text: "测量完成" + }) + } + } + }) + }, + fail(res) { + console.error('getBLEDeviceCharacteristics', res) + } + }) + }, + + /** + * 断开蓝牙模块 + */ + closeBluetoothAdapter() { + wx.closeBluetoothAdapter() + this._discoveryStarted = false + wx.showToast({ + title: '断开蓝牙模块', + icon: 'none' + }) + this.setData({ + devices: [], + weight: "", + text: "", + imp2: '', + imp3: '', + imp4: '', + imp5: '', + imp7: '', + }) + }, + // 断开与低功耗蓝牙设备的连接 + closeBLEConnection() { + wx.closeBLEConnection({ + deviceId: this._deviceId + }) + wx.showToast({ + title: '断开蓝牙连接', + icon: 'none' + }) + this.setData({ + connected: false, + devices: [], + weight: "", + text: "", + imp2: '', + imp3: '', + imp4: '', + imp5: '', + imp7: '', + }) + }, + +}); diff --git a/bluetooth_demo_L08/pages/index/index.json b/bluetooth_demo_L08/pages/index/index.json new file mode 100644 index 0000000..b55b5a2 --- /dev/null +++ b/bluetooth_demo_L08/pages/index/index.json @@ -0,0 +1,4 @@ +{ + "usingComponents": { + } +} \ No newline at end of file diff --git a/bluetooth_demo_L08/pages/index/index.wxml b/bluetooth_demo_L08/pages/index/index.wxml new file mode 100644 index 0000000..b847b4f --- /dev/null +++ b/bluetooth_demo_L08/pages/index/index.wxml @@ -0,0 +1,46 @@ + +module.exports.max = function(n1, n2) { + return Math.max(n1, n2) +} +module.exports.len = function(arr) { + arr = arr || [] + return arr.length +} + + + + + + + + + + {{weight}} + {{text}} + {{imp2}} + {{imp3}} + {{imp4}} + {{imp5}} + {{imp7}} + + 已发现 {{devices.length}} 个外围设备: + + + + {{item.name}} + (信号强度: {{item.RSSI}}dBm) + + mac地址: {{item.macAddr || item.deviceId}} + + + + + + diff --git a/bluetooth_demo_L08/project.config.json b/bluetooth_demo_L08/project.config.json new file mode 100644 index 0000000..35dc8c4 --- /dev/null +++ b/bluetooth_demo_L08/project.config.json @@ -0,0 +1,52 @@ +{ + "description": "项目配置文件,详见文档:https://developers.weixin.qq.com/miniprogram/dev/devtools/projectconfig.html", + "setting": { + "urlCheck": true, + "es6": false, + "enhance": true, + "postcss": true, + "preloadBackgroundData": false, + "minified": true, + "newFeature": false, + "coverView": true, + "nodeModules": false, + "autoAudits": false, + "showShadowRootInWxmlPanel": true, + "scopeDataCheck": false, + "uglifyFileName": false, + "checkInvalidKey": true, + "checkSiteMap": true, + "uploadWithSourceMap": true, + "compileHotReLoad": false, + "lazyloadPlaceholderEnable": false, + "useMultiFrameRuntime": true, + "useApiHook": true, + "useApiHostProcess": true, + "babelSetting": { + "ignore": [], + "disablePlugins": [], + "outputPath": "" + }, + "useIsolateContext": true, + "userConfirmedBundleSwitch": false, + "packNpmManually": false, + "packNpmRelationList": [], + "minifyWXSS": true, + "disableUseStrict": false, + "minifyWXML": true, + "showES6CompileOption": false, + "useCompilerPlugins": false, + "ignoreUploadUnusedFiles": true + }, + "compileType": "miniprogram", + "simulatorType": "wechat", + "simulatorPluginLibVersion": {}, + "projectname": "bluetooth_demo_L08", + "libVersion": "2.23.2", + "appid": "wxcea3504a31518eb6", + "editorSetting": { + "tabIndent": "insertSpaces", + "tabSize": 2 + }, + "condition": {} +} \ No newline at end of file diff --git a/bluetooth_demo_L08/sitemap.json b/bluetooth_demo_L08/sitemap.json new file mode 100644 index 0000000..ca02add --- /dev/null +++ b/bluetooth_demo_L08/sitemap.json @@ -0,0 +1,7 @@ +{ + "desc": "关于本文件的更多信息,请参考文档 https://developers.weixin.qq.com/miniprogram/dev/framework/sitemap.html", + "rules": [{ + "action": "allow", + "page": "*" + }] +} \ No newline at end of file diff --git a/bluetooth_demo_L08/utils/util.js b/bluetooth_demo_L08/utils/util.js new file mode 100644 index 0000000..7aafdf3 --- /dev/null +++ b/bluetooth_demo_L08/utils/util.js @@ -0,0 +1,30 @@ +function inArray(arr, key, val) { + if (!arr || !arr.length || typeof arr != 'object' || !Array.isArray(arr)) { + return -1 + } + for (let i = 0; i < arr.length; i++) { + if (!key) { + if (arr[i] == val) { + return i + } + } else if (arr[i][key] === val) { + return i + } + } + return -1; +} + +// ArrayBuffer转16进度字符串示例 +function ab2hex(buffer, split) { + var hexArr = Array.prototype.map.call( + new Uint8Array(buffer), + function(bit) { + return ('00' + bit.toString(16)).slice(-2) + } + ) + return hexArr.join(split); +} +module.exports = { + inArray, + ab2hex, +}