ElinkThings小程序蓝牙插件SDK集合:aifresh、ailink http://doc.elinkthings.com/web/#/36?page_id=127
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

index.js 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // const plugin = requirePlugin("myPlugin").AiFresh;
  2. const {AiFresh: plugin} = requirePlugin("sdkPlugin");
  3. function inArray(arr, key, val) {
  4. for (let i = 0; i < arr.length; i++) {
  5. if (arr[i][key] === val) {
  6. return i;
  7. }
  8. }
  9. return -1;
  10. }
  11. // ArrayBuffer转16进度字符串示例
  12. function ab2hex(buffer) {
  13. var hexArr = Array.prototype.map.call(
  14. new Uint8Array(buffer),
  15. function (bit) {
  16. return ('00' + bit.toString(16)).slice(-2)
  17. }
  18. )
  19. return hexArr.join(',');
  20. }
  21. Page({
  22. data: {
  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. mac: "",
  37. connected: false,
  38. text: "",
  39. unitList_all: ['g', 'ml', 'lb:oz', 'oz', 'kg', '斤', '牛奶 ml', '水 ml', '牛奶 floz', '水 floz', 'lb'],
  40. unitList: ['g', 'ml', 'lb:oz', 'oz'],
  41. weight: 0,
  42. unit: 'g',
  43. unitIndex: 0,
  44. min: 0,
  45. second: 0,
  46. },
  47. onLoad: function () {
  48. },
  49. clearDevices() {
  50. this.setData({
  51. devices: []
  52. })
  53. this.closeBluetoothAdapter()
  54. wx.nextTick(()=>{
  55. this.openBluetoothAdapter()
  56. })
  57. },
  58. tare() {
  59. console.log("去皮")
  60. plugin.cmdTare()
  61. },
  62. getUnits() {
  63. console.log("获取设备支持的单位")
  64. plugin.cmdGetAllUnits()
  65. },
  66. countUp() {
  67. console.log("正计时开始")
  68. plugin.cmdTimeCountUp()
  69. },
  70. countUpPause() {
  71. console.log("正计时暂停")
  72. plugin.cmdTimeCountUpPause(this.data.min, this.data.second)
  73. },
  74. countDown() {
  75. console.log("倒计时开始")
  76. if (this.data.min || this.data.second) {
  77. plugin.cmdTimeCountDown(this.data.min, this.data.second)
  78. } else {
  79. plugin.cmdTimeCountDown(5, 30)
  80. }
  81. },
  82. countDownPause() {
  83. console.log("倒计时暂停")
  84. plugin.cmdTimeCountDownPause(this.data.min, this.data.second)
  85. },
  86. resetTime() {
  87. console.log("重置时间")
  88. plugin.cmdTimeReset()
  89. this.setData({
  90. min: 0,
  91. second: 0,
  92. })
  93. },
  94. selectUnit(e) {
  95. let unit = e.currentTarget.dataset.unit
  96. let index = e.currentTarget.dataset.index
  97. let idx = this.data.unitList_all.indexOf(unit)
  98. plugin.cmdSwitchUnit(idx)
  99. },
  100. openBluetoothAdapter() {
  101. wx.openBluetoothAdapter({
  102. success: (res) => {
  103. console.log('openBluetoothAdapter success', res)
  104. this.startBluetoothDevicesDiscovery()
  105. },
  106. fail: (res) => {
  107. if (res.errCode === 10001) {
  108. wx.showToast({
  109. title: '请打开蓝牙',
  110. icon:"none"
  111. })
  112. wx.onBluetoothAdapterStateChange(function (res) {
  113. console.log('onBluetoothAdapterStateChange', res)
  114. if (res.available) {
  115. this.startBluetoothDevicesDiscovery()
  116. }
  117. })
  118. }
  119. }
  120. })
  121. },
  122. getBluetoothAdapterState() {
  123. wx.getBluetoothAdapterState({
  124. success: (res) => {
  125. console.log('getBluetoothAdapterState', res)
  126. if (res.discovering) {
  127. this.onBluetoothDeviceFound()
  128. } else if (res.available) {
  129. this.startBluetoothDevicesDiscovery()
  130. }
  131. }
  132. })
  133. },
  134. startBluetoothDevicesDiscovery() {
  135. if (this._discoveryStarted) {
  136. return
  137. }
  138. this._discoveryStarted = true
  139. wx.startBluetoothDevicesDiscovery({
  140. allowDuplicatesKey: true,
  141. services: [
  142. "FFB0",
  143. ],
  144. success: (res) => {
  145. console.log('startBluetoothDevicesDiscovery success', res)
  146. this.onBluetoothDeviceFound()
  147. },
  148. })
  149. },
  150. stopBluetoothDevicesDiscovery() {
  151. wx.stopBluetoothDevicesDiscovery()
  152. },
  153. onBluetoothDeviceFound() {
  154. wx.onBluetoothDeviceFound((res) => {
  155. res.devices.forEach(device => {
  156. const foundDevices = this.data.devices
  157. const idx = inArray(foundDevices, 'deviceId', device.deviceId)
  158. const data = {}
  159. console.log(device)
  160. // console.log(ab2hex(device.advertisData))
  161. if (idx === -1) {
  162. data[`devices[${foundDevices.length}]`] = device
  163. } else {
  164. data[`devices[${idx}]`] = device
  165. }
  166. this.setData(data)
  167. })
  168. })
  169. },
  170. createBLEConnection(e) {
  171. const ds = e.currentTarget.dataset
  172. const deviceId = ds.deviceId
  173. const name = ds.name
  174. const index = ds.index
  175. this._device = this.data.devices[index]
  176. console.log(this._device)
  177. wx.createBLEConnection({
  178. deviceId,
  179. success: (res) => {
  180. let buff = this._device.advertisData.slice(-6)
  181. let arr = new Uint8Array(buff).reverse()
  182. let mac = ab2hex(arr).replace(/,/g,":")
  183. this.setData({
  184. connected: true,
  185. name,
  186. deviceId,
  187. mac: mac,
  188. })
  189. console.log("createBLEConnection:success")
  190. wx.stopBluetoothDevicesDiscovery()
  191. this.getBLEDeviceServices(deviceId)
  192. }
  193. })
  194. this.stopBluetoothDevicesDiscovery()
  195. },
  196. closeBLEConnection() {
  197. wx.closeBLEConnection({
  198. deviceId: this._deviceId
  199. })
  200. this.setData({
  201. connected: false,
  202. min: 0,
  203. second: 0,
  204. weight: 0,
  205. unitList: ['g', 'ml', 'lb:oz', 'oz'],
  206. })
  207. },
  208. getBLEDeviceServices(deviceId) {
  209. wx.getBLEDeviceServices({
  210. deviceId,
  211. success: (res) => {
  212. console.log('getBLEDeviceServices:', res)
  213. for (let i = 0; i < res.services.length; i++) {
  214. if (res.services[i].isPrimary && res.services[i].uuid.indexOf('0000FFB0') != -1) {
  215. this.getBLEDeviceCharacteristics(deviceId, res.services[i].uuid)
  216. return
  217. }
  218. }
  219. }
  220. })
  221. },
  222. getBLEDeviceCharacteristics(deviceId, serviceId) {
  223. this._deviceId = deviceId
  224. this._serviceId = serviceId
  225. this._device.serviceId = serviceId
  226. wx.getBLEDeviceCharacteristics({
  227. deviceId,
  228. serviceId,
  229. success: (res) => {
  230. console.log('getBLEDeviceCharacteristics success', res.characteristics)
  231. for (let i = 0; i < res.characteristics.length; i++) {
  232. let item = res.characteristics[i]
  233. if (item.uuid.indexOf('0000FFB1') != -1) {
  234. this.uuid1 = item.uuid //下发数据
  235. } else if (item.uuid.indexOf('0000FFB2') != -1) {
  236. this.uuid2 = item.uuid //监听数据
  237. }
  238. }
  239. // 打开监听
  240. wx.notifyBLECharacteristicValueChange({
  241. deviceId,
  242. serviceId,
  243. characteristicId: this.uuid2,
  244. state: true,
  245. })
  246. plugin.initPlugin(res.characteristics, this._device)
  247. plugin.cmdGetAllUnits()
  248. wx.onBLECharacteristicValueChange((characteristic) => {
  249. let bleData = plugin.parseBleData(characteristic.value)
  250. if (bleData.status == 1) {
  251. this.setData({
  252. text: bleData.data.text,
  253. weight: bleData.data.weight,
  254. unit: bleData.data.unit,
  255. unitIndex: bleData.data.unitIndex
  256. })
  257. } else if (bleData.status == 2) {
  258. this.setData({
  259. min: bleData.data.min,
  260. second: bleData.data.second,
  261. })
  262. } else if (bleData.status == 3) {//设备支持的单位
  263. // 使用返回的单位索引,从本地的所有单位列表中取出支持的单位名
  264. let unitList = bleData.data.unitIndexs.map(i=>{
  265. return this.data.unitList_all[i]
  266. })
  267. this.setData({
  268. // unitList: bleData.data.unitList,
  269. unitList,
  270. })
  271. } else {
  272. console.log(bleData)
  273. }
  274. })
  275. },
  276. fail(res) {
  277. console.error('getBLEDeviceCharacteristics', res)
  278. }
  279. })
  280. },
  281. writeBLECharacteristicValue(buffer, uuid, deviceId, serviceId) {
  282. // 向蓝牙设备发送一个二进制流数据
  283. wx.writeBLECharacteristicValue({
  284. deviceId,
  285. serviceId,
  286. characteristicId: uuid,
  287. value: buffer,
  288. success(res) {
  289. console.log('writeBLECharacteristicValue success', res)
  290. console.log(plugin.ab2hex(buffer))
  291. }
  292. })
  293. },
  294. closeBluetoothAdapter() {
  295. wx.closeBluetoothAdapter()
  296. this._discoveryStarted = false
  297. },
  298. });