1var app = getApp(); 2var ble_util = require("../../utils/ble_util.js") 3var hex_util = require("../../utils/hex_util.js") 4 5Page({ 6 data: { 7 ProductKey: '', 8 DeviceName: '', 9 DeviceSecret: '', 10 11 link_lp_devices: [], 12 selected_device_index: 0, 13 14 LinkLpState: { 15 scanning: false, 16 linking: false, 17 }, 18 19 LinkState: 0 20 }, 21 22 23 notifyCheckInt: null, 24 notifyCheckCnt: 0, 25 26 onLoad() { 27 }, 28 29 genTriadPacket(ProductKey, DeviceName, DeviceSecret) { 30 let ret = "ffb0" 31 ret += ProductKey.length < 16 ? ('0' + (ProductKey.length).toString(16)) : ((ProductKey.length).toString(16)) 32 ret += DeviceName.length < 16 ? ('0' + (DeviceName.length).toString(16)) : ((DeviceName.length).toString(16)) 33 ret += DeviceSecret.length < 16 ? ('0' + (DeviceSecret.length).toString(16)) : ((DeviceSecret.length).toString(16)) 34 for (let i = 0; i < ProductKey.length; i++) { 35 ret += ProductKey.charCodeAt(i) < 16 ? ('0' + ProductKey.charCodeAt(i).toString(16)) : ProductKey.charCodeAt(i).toString(16); 36 } 37 for (let i = 0; i < DeviceName.length; i++) { 38 ret += DeviceName.charCodeAt(i) < 16 ? ('0' + DeviceName.charCodeAt(i).toString(16)) : DeviceName.charCodeAt(i).toString(16); 39 } 40 for (let i = 0; i < DeviceSecret.length; i++) { 41 ret += DeviceSecret.charCodeAt(i) < 16 ? ('0' + DeviceSecret.charCodeAt(i).toString(16)) : DeviceSecret.charCodeAt(i).toString(16); 42 } 43 return ret 44 }, 45 46 scanTriadQr() { 47 my.scan({ 48 scanType: ['qrCode'], 49 success: (res) => { 50 console.log(res.code) 51 let Triad = JSON.parse(res.code) 52 console.log(Triad) 53 if (Triad.ProductKey != undefined && Triad.DeviceName != undefined && Triad.DeviceSecret != undefined) { 54 this.setData({ 55 ProductKey: Triad.ProductKey, 56 DeviceName: Triad.DeviceName, 57 DeviceSecret: Triad.DeviceSecret 58 }) 59 } else { 60 my.alert({ content: "二维码格式错误" }); 61 } 62 }, 63 }); 64 }, 65 66 async startLinkLpDevicesDiscovery() { 67 this.setData({ 'LinkLpState.scanning': true }) 68 this.setData({ link_lp_devices: [] }) 69 70 await ble_util.OpenBluetoothAdapter().catch(err => { console.error(err) }) 71 72 let res; 73 for (let retry = 10; retry > 0; retry--) { 74 console.log(retry) 75 res = await ble_util.DiscoveryBLEDevice('FFB0') 76 77 if (res && res.devices != null) { 78 console.log(res.devices) 79 this.setData({ 80 link_lp_devices: res.devices 81 }) 82 break; 83 } 84 } 85 console.log(res) 86 if (res.devices == null) 87 my.alert({ content: res.content }) 88 this.setData({ 'LinkLpState.scanning': false }) 89 }, 90 91 bindDevicePickerChange(event) { 92 this.setData({ 93 selected_device_index: event.detail.value, 94 }); 95 }, 96 97 async sendTriad() { 98 if (this.data.ProductKey.length < 1 || this.data.DeviceName.length < 1 || this.data.DeviceSecret.length < 1) { 99 my.alert({ content: '请输入正确的三元组信息' }) 100 } 101 102 await this.OpenBluetoothAdapter().catch(err => { console.error(err) }) 103 104 this.setData({ 'LinkLpState.linking': true }) 105 await ble_util.DisconnectBLEDevice(this.data.net_config_devices[this.data.selected_device_index].deviceId).catch(err => { console.error(err) }) 106 107 my.connectBLEDevice({ 108 deviceId: this.data.link_lp_devices[this.data.selected_device_index].deviceId, 109 success: res => { 110 console.log('connect with ' + this.data.link_lp_devices[this.data.selected_device_index].deviceId + ' success') 111 console.log(res) 112 113 my.notifyBLECharacteristicValueChange({ 114 state: true, 115 deviceId: this.data.link_lp_devices[this.data.selected_device_index].deviceId, 116 serviceId: ble_util.UUID_VENDOR_SERVICE[app.globalData.sysname], 117 characteristicId: ble_util.UUID_VENDOR_CHAR_NOTIFY[app.globalData.sysname], 118 success: () => { 119 my.offBLECharacteristicValueChange(); 120 my.onBLECharacteristicValueChange((res) => { 121 console.log('得到响应数据 = ' + res.value); 122 console.log(hex_util.hexString2String(res.value)) 123 this.LinkLpNotifyFormat(res.value) 124 }); 125 console.log('监听成功'); 126 }, 127 fail: error => { 128 this.setData({ 'LinkLpState.linking': false }) 129 console.log({ content: '监听失败' + JSON.stringify(error) }); 130 }, 131 }); 132 133 my.writeBLECharacteristicValue({ 134 deviceId: this.data.link_lp_devices[this.data.selected_device_index].deviceId, 135 serviceId: ble_util.UUID_VENDOR_SERVICE[app.globalData.sysname], 136 characteristicId: ble_util.UUID_VENDOR_CHAR_WRITE[app.globalData.sysname], 137 value: this.genTriadPacket(this.data.ProductKey, this.data.DeviceName, this.data.DeviceSecret), 138 success: res => { 139 console.log(res); 140 141 // retry here 142 143 var notifyCheckCnt = 5 144 var notifyCheckInt = setInterval(() => { 145 if (this.data.LinkLpState.linking == 0) { 146 notifyCheckCnt = notifyCheckCnt - 1 147 console.log('notifyCheckCnt ' + notifyCheckCnt) 148 if (notifyCheckCnt < 0) { 149 clearInterval(notifyCheckInt) 150 this.setData({ 'LinkLpState.linking': false }) 151 my.alert({ content: "下发三元组失败:设备无回复" }) 152 } 153 } 154 }, 1000) 155 156 157 }, 158 fail: error => { 159 this.setData({ 'linkLpState.linking': false }) 160 console.log(error); 161 }, 162 }); 163 164 }, 165 fail: error => { 166 this.setData({ 'LinkLpState.linking': false }) 167 console.log(error); 168 }, 169 }) 170 }, 171 172 LinkLpNotifyFormat(str) { 173 console.log("LinkLpNotifyFormat") 174 if (hex_util.hexString2String(str) == 'DEVSETOK') { 175 console.log("link done") 176 this.setData({ 177 'LinkLpState.linking': false, 178 LinkState: 1 179 }) 180 } 181 }, 182 183 pkOnInput(e) { 184 this.setData({ 185 ProductKey: e.detail.value 186 }) 187 }, 188 189 dnOnInput(e) { 190 this.setData({ 191 DeviceName: e.detail.value 192 }) 193 }, 194 195 dsOnInput(e) { 196 this.setData({ 197 DeviceSecret: e.detail.value 198 }) 199 }, 200 201 onHide() { 202 my.closeBluetoothAdapter(); 203 } 204 205}); 206