ElinkThings小程序蓝牙插件SDK集合:aifresh、ailink http://doc.elinkthings.com/web/#/36?page_id=127
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. const plugin = requirePlugin("sdkPlugin").AiLink;
  2. function inArray(arr, key, val) {
  3. for (let i = 0; i < arr.length; i++) {
  4. if (arr[i][key] === val) {
  5. return i;
  6. }
  7. }
  8. return -1;
  9. }
  10. // ArrayBuffer转16进度字符串示例
  11. function ab2hex(buffer) {
  12. var hexArr = Array.prototype.map.call(
  13. new Uint8Array(buffer),
  14. function (bit) {
  15. return ('00' + bit.toString(16)).slice(-2)
  16. }
  17. )
  18. return hexArr.join(',');
  19. }
  20. Page({
  21. data: {
  22. showWriteInput:false,
  23. devices: [
  24. // {
  25. // deviceId: "02:03:04:05:06:07",
  26. // name: "elink",
  27. // localName: "elink",
  28. // mac: "02:03:04:05:06:07",
  29. // RSSI: -69,
  30. // advertisServiceUUIDs:[
  31. // "FFE0","FFC0"
  32. // ],
  33. // analyzeDataText:"0102003241123413"
  34. // }
  35. ],
  36. connected: false,
  37. chs: [],
  38. },
  39. onLoad: function () {
  40. },
  41. openBluetoothAdapter() {
  42. wx.openBluetoothAdapter({
  43. success: (res) => {
  44. console.log('openBluetoothAdapter success', res)
  45. this.startBluetoothDevicesDiscovery()
  46. },
  47. fail: (res) => {
  48. if (res.errCode === 10001) {
  49. wx.showToast({
  50. title: '请打开蓝牙',
  51. icon:"none"
  52. })
  53. wx.onBluetoothAdapterStateChange(function (res) {
  54. console.log('onBluetoothAdapterStateChange', res)
  55. if (res.available) {
  56. this.startBluetoothDevicesDiscovery()
  57. }
  58. })
  59. }
  60. }
  61. })
  62. },
  63. getBluetoothAdapterState() {
  64. wx.getBluetoothAdapterState({
  65. success: (res) => {
  66. console.log('getBluetoothAdapterState', res)
  67. if (res.discovering) {
  68. this.onBluetoothDeviceFound()
  69. } else if (res.available) {
  70. this.startBluetoothDevicesDiscovery()
  71. }
  72. }
  73. })
  74. },
  75. startBluetoothDevicesDiscovery() {
  76. if (this._discoveryStarted) {
  77. return
  78. }
  79. this._discoveryStarted = true
  80. wx.startBluetoothDevicesDiscovery({
  81. allowDuplicatesKey: true,
  82. services: [
  83. "FFE0",
  84. "F0A0",
  85. ],
  86. success: (res) => {
  87. console.log('startBluetoothDevicesDiscovery success', res)
  88. this.onBluetoothDeviceFound()
  89. },
  90. })
  91. },
  92. stopBluetoothDevicesDiscovery() {
  93. wx.stopBluetoothDevicesDiscovery()
  94. },
  95. onBluetoothDeviceFound() {
  96. wx.onBluetoothDeviceFound((res) => {
  97. res.devices.forEach(device => {
  98. if (!device.name && !device.localName) {
  99. return
  100. }
  101. const foundDevices = this.data.devices
  102. const idx = inArray(foundDevices, 'deviceId', device.deviceId)
  103. const data = {}
  104. console.log(device)
  105. // console.log(ab2hex(device.advertisData))
  106. if(device.advertisServiceUUIDs[0].indexOf("F0A0") !== -1){
  107. let parseDataRes = plugin.parseBroadcastData(device.advertisData)
  108. console.log(parseDataRes)
  109. if(parseDataRes.status == 1) {
  110. let analyzeData = plugin.analyzeBroadcastScaleData(parseDataRes)
  111. console.log(analyzeData)
  112. device.analyzeDataText = analyzeData.text
  113. }
  114. }else{
  115. let buff = device.advertisData.slice(-6)
  116. device.mac = new Uint8Array(buff)
  117. }
  118. if (idx === -1) {
  119. data[`devices[${foundDevices.length}]`] = device
  120. } else {
  121. data[`devices[${idx}]`] = device
  122. }
  123. this.setData(data)
  124. })
  125. })
  126. },
  127. createBLEConnection(e) {
  128. const ds = e.currentTarget.dataset
  129. const index = ds.index
  130. this._device = this.data.devices[index]
  131. console.log(this._device)
  132. const deviceId = ds.deviceId
  133. const name = ds.name
  134. this.mac = ds.mac
  135. wx.createBLEConnection({
  136. deviceId,
  137. success: (res) => {
  138. this.setData({
  139. connected: true,
  140. name,
  141. deviceId,
  142. })
  143. console.log("createBLEConnection:success")
  144. wx.stopBluetoothDevicesDiscovery()
  145. this.getBLEDeviceServices(deviceId)
  146. }
  147. })
  148. this.stopBluetoothDevicesDiscovery()
  149. },
  150. closeBLEConnection() {
  151. wx.closeBLEConnection({
  152. deviceId: this._deviceId
  153. })
  154. this.setData({
  155. connected: false,
  156. chs: [],
  157. })
  158. },
  159. getBLEDeviceServices(deviceId) {
  160. wx.getBLEDeviceServices({
  161. deviceId,
  162. success: (res) => {
  163. for (let i = 0; i < res.services.length; i++) {
  164. if (res.services[i].isPrimary) {
  165. this.getBLEDeviceCharacteristics(deviceId, res.services[i].uuid)
  166. return
  167. }
  168. }
  169. }
  170. })
  171. },
  172. getBLEDeviceCharacteristics(deviceId, serviceId) {
  173. const _this = this
  174. this._deviceId = deviceId
  175. this._serviceId = serviceId
  176. this._device.serviceId = serviceId
  177. wx.getBLEDeviceCharacteristics({
  178. deviceId,
  179. serviceId,
  180. success: (res) => {
  181. console.log('getBLEDeviceCharacteristics success', res.characteristics)
  182. // let uuid1, uuid2, uuid3;
  183. for (let i = 0; i < res.characteristics.length; i++) {
  184. let item = res.characteristics[i]
  185. if (item.uuid.indexOf('0000FFE1') != -1) {
  186. this.uuid1 = item.uuid //下发数据
  187. } else if (item.uuid.indexOf('0000FFE2') != -1) {
  188. this.uuid2 = item.uuid //监听数据
  189. } else if (item.uuid.indexOf('0000FFE3') != -1) {
  190. this.uuid3 = item.uuid //写入设置
  191. }
  192. }
  193. // 打开监听
  194. wx.notifyBLECharacteristicValueChange({
  195. deviceId,
  196. serviceId,
  197. characteristicId: this.uuid2,
  198. state: true,
  199. })
  200. wx.notifyBLECharacteristicValueChange({
  201. deviceId,
  202. serviceId,
  203. characteristicId: this.uuid3,
  204. state: true,
  205. })
  206. plugin.initPlugin(res.characteristics, this._device)
  207. wx.onBLECharacteristicValueChange((characteristic) => {
  208. let bleData = plugin.parseBleData(characteristic.value)
  209. if (bleData.status == 0) {
  210. console.log("握手成功")
  211. } else if (bleData.status == 1) {
  212. console.log(bleData)
  213. let parseData = bleData.data //对应协议中的payload数据,可以自行解析该数据
  214. console.log(ab2hex(parseData))
  215. console.log(ab2hex(bleData.completeData))
  216. const idx = inArray(this.data.chs, 'uuid', characteristic.characteristicId)
  217. const data = {}
  218. if (idx === -1) {
  219. data[`chs[${this.data.chs.length}]`] = {
  220. uuid: characteristic.characteristicId,
  221. value: ab2hex(parseData)
  222. }
  223. } else {
  224. data[`chs[${idx}]`] = {
  225. uuid: characteristic.characteristicId,
  226. value: ab2hex(parseData)
  227. }
  228. }
  229. this.setData(data)
  230. } else {
  231. console.log(bleData)
  232. }
  233. })
  234. },
  235. fail(res) {
  236. console.error('getBLEDeviceCharacteristics', res)
  237. }
  238. })
  239. },
  240. writeBLECharacteristicValue(buffer, uuid, deviceId, serviceId) {
  241. // 向蓝牙设备发送一个二进制流数据
  242. wx.writeBLECharacteristicValue({
  243. deviceId,
  244. serviceId,
  245. characteristicId: uuid,
  246. value: buffer,
  247. success(res) {
  248. console.log('writeBLECharacteristicValue success', res)
  249. console.log(ab2hex(buffer))
  250. }
  251. })
  252. },
  253. closeBluetoothAdapter() {
  254. wx.closeBluetoothAdapter()
  255. this._discoveryStarted = false
  256. },
  257. showWriteInputView(){
  258. this.setData({
  259. showWriteInput: true
  260. })
  261. },
  262. hideWriteInputView(){
  263. this.setData({
  264. showWriteInput: false
  265. })
  266. },
  267. writingCmd(e){
  268. this._cmd = e.detail.value
  269. },
  270. submitCmd(){
  271. if(!this._cmd){
  272. return
  273. }
  274. let arr = []
  275. if(this._cmd.indexOf(",") == -1){
  276. arr = this._cmd.split(" ")
  277. } else {
  278. arr = this._cmd.split(",")
  279. }
  280. for(let i in arr){
  281. arr[i] = parseInt(arr[i],16)
  282. }
  283. // let arr = [
  284. // 0xA6,
  285. // 0x01,
  286. // 0x28,
  287. // 0x29,
  288. // 0x6A,
  289. // //A6 01 28 29 6A
  290. // ]
  291. if(arr[0] == 0xA6){
  292. let payload = arr.slice(2,-2)
  293. plugin.sendDataOfA6(payload)
  294. } else if(arr[0] == 0xA7) {
  295. let cid = [arr[1], arr[2]] // 001E
  296. let payload = arr.slice(4,-2)
  297. plugin.sendDataOfA7(payload)
  298. // this.writeBLECharacteristicValue(buff, this.uuid1, this._deviceId, this._serviceId)
  299. }
  300. return
  301. },
  302. });