随手精灵小程序,单独为了获取WIFI信息加强围栏告警功能能力
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // 示例demo
  2. const plugin = requirePlugin('ppScale-plugin');
  3. import { CommandButtonList } from '../../model/index'
  4. let app = getApp();
  5. Page({
  6. data: {
  7. // 选择的设备对象
  8. selmac: null,
  9. // 是否在测量中
  10. scaleing: false,
  11. // 蓝牙连接状态
  12. devLink: false,
  13. // 扫描筛选出的设备
  14. selectDev: null,
  15. // 设备模型
  16. deviceModel: null,
  17. // 通讯结果
  18. contnectResult: null,
  19. // 发送指令内容
  20. sendData: null,
  21. // 蓝牙设备返回的结果
  22. callBack: null,
  23. // 蓝牙设备下发指令按钮数据
  24. sendBtnList: CommandButtonList.send, //版本号
  25. // 手动输入文本域的值
  26. inputValue: '',
  27. },
  28. /**
  29. * 页面卸载时调用
  30. */
  31. onUnload() {
  32. // 业务结束后需要调用此方法
  33. },
  34. /**
  35. * 页面隐藏时调用
  36. */
  37. onHide() {
  38. },
  39. onLoad() {
  40. let that = this;
  41. //监听蓝牙连接状态
  42. wx.onBLEConnectionStateChange(function (res) {
  43. // 该方法回调中可以用于处理连接意外断开等异常情况
  44. console.log(`设备蓝牙连接状态`, res);
  45. // 如果蓝牙断开
  46. if (!res.connected) {
  47. // 关闭蓝牙连接事件,再重新初始化连接?
  48. that.setData({
  49. devLink: false,
  50. })
  51. wx.showToast({
  52. title: '蓝牙已断开',
  53. })
  54. /* setTimeout(() => {
  55. that.initBlue();
  56. }, 1500) */
  57. } else {
  58. that.setData({
  59. devLink: true,
  60. })
  61. }
  62. })
  63. },
  64. onShow() {
  65. let that = this;
  66. // 记录设备对象
  67. this.setData({
  68. selmac: app.globalData.selmac
  69. })
  70. console.log("selmac", that.data.selmac);
  71. // 连接设备
  72. wx.createBLEConnection({
  73. deviceId: app.globalData.selmac.deviceId,
  74. success: (res) => {
  75. console.log("连接成功", res);
  76. that.setData({
  77. devLink: true
  78. })
  79. },
  80. fail: (e) => {
  81. console.log("连接失败", e);
  82. }
  83. })
  84. setTimeout(() => {
  85. // 获取蓝牙低功耗设备所有服务 (service)
  86. wx.getBLEDeviceServices({
  87. deviceId: app.globalData.selmac.deviceId,
  88. success: (res) => {
  89. console.log("获取蓝牙低功耗设备所有服务成功", res);
  90. let serviceId = res.services[0].uuid;
  91. // 缓存数据 serviceId
  92. app.globalData.serviceId = serviceId;
  93. //获取蓝牙低功耗设备某个服务中所有特征 (characteristic)。
  94. wx.getBLEDeviceCharacteristics({
  95. deviceId: app.globalData.selmac.deviceId,
  96. serviceId: serviceId,
  97. success: (res) => {
  98. console.log("获取蓝牙低功耗设备某个服务中所有特征", res);
  99. let characteristicId = res.characteristics[0].uuid;
  100. // 缓存数据 characteristicId
  101. app.globalData.characteristicId = characteristicId;
  102. },
  103. fail: (e) => {
  104. console.log("获取蓝牙低功耗设备某个服务中所有特征", e);
  105. }
  106. })
  107. },
  108. fail: (e) => {
  109. console.log("获取蓝牙低功耗设备所有服务失败", e);
  110. }
  111. })
  112. }, 1500)
  113. },
  114. connect() {
  115. let that = this;
  116. if (this.data.selmac.deviceId) {
  117. console.log("可以初始化");
  118. wx.openBluetoothAdapter({
  119. mode: 'peripheral',
  120. })
  121. // 连接设备
  122. wx.createBLEConnection({
  123. deviceId: that.data.selmac.deviceId,
  124. success: (res) => {
  125. console.log("建立连接成功", res);
  126. },
  127. fail: (e) => {
  128. console.log("建立连接失败", e);
  129. wx.showToast({
  130. title: '重新连接失败',
  131. icon: 'error'
  132. })
  133. }
  134. })
  135. }
  136. },
  137. disconnect() {
  138. let that = this;
  139. if (this.data.selmac.deviceId) {
  140. // 连接设备
  141. wx.closeBLEConnection({
  142. deviceId: that.data.selmac.deviceId,
  143. success: (res) => {
  144. console.log("断开连接成功", res);
  145. },
  146. fail: (e) => {
  147. console.log("断开连接失败", e);
  148. }
  149. })
  150. }
  151. },
  152. onClearInput() {
  153. this.setData({
  154. callBack: '',
  155. sendData: '',
  156. });
  157. },
  158. startwrite() {
  159. // 下发指令
  160. let text = this.cleanHexString('hello');
  161. let buffer = this.stringToCmdBuffer(text);
  162. let hexData = this.ab2hex(buffer);
  163. let string = this.hexCharCodeToStr(hexData);
  164. console.log("十六进制字符串转buffer", buffer);
  165. console.log("buffer转hexData", hexData);
  166. console.log("hexData转string", string);
  167. this.sendMessage(text);
  168. },
  169. bindTextAreaBlur(e) {
  170. this.data.inputValue = e.detail.value;
  171. console.log(e.detail.value)
  172. },
  173. // 手动输入文本域的值
  174. onInputSend() {
  175. console.log("this.data.inputValue", this.data.inputValue);
  176. if(this.data.inputValue) {
  177. this.sendMessage(this.data.inputValue, true);
  178. } else {
  179. wx.showToast({
  180. title: '指令不能为空',
  181. icon: 'error'
  182. })
  183. }
  184. },
  185. // 发送数据
  186. sendMessage(event,type) {
  187. let that = this;
  188. let sendData = '';
  189. if(type) {
  190. console.log("手动输出方式");
  191. sendData = event
  192. } else {
  193. console.log("按钮点击输出方式");
  194. console.log("event", event.target.dataset);
  195. sendData = event.target.dataset.command.defaultSendData;
  196. }
  197. if(!that.data.devLink) {
  198. wx.showToast({
  199. title: '蓝牙已断开',
  200. icon: 'error'
  201. })
  202. return
  203. }
  204. if(!sendData) {
  205. wx.showToast({
  206. title: '指令不存在',
  207. icon: 'error'
  208. })
  209. return
  210. }
  211. this.setData({
  212. sendData: sendData
  213. })
  214. let buffer = this.stringToCmdBuffer(sendData);
  215. console.log("发送的指令",buffer);
  216. wx.writeBLECharacteristicValue({
  217. deviceId: app.globalData.selmac.deviceId, // 设备ID
  218. serviceId: app.globalData.selmac.serviceId, // 服务UUID
  219. characteristicId: app.globalData.selmac.characteristicId, // 特征值
  220. value: buffer,
  221. success(res) {
  222. console.log("write指令发送成功", res)
  223. that.notify();
  224. },
  225. fail(err) {
  226. console.log("write指令发送失败", err);
  227. that.setData({
  228. callBack: err.errMsg
  229. });
  230. wx.showToast({
  231. title: '指令发送失败',
  232. icon: 'error'
  233. })
  234. }
  235. })
  236. },
  237. cleanHexString(hexStr) {
  238. // 使用正则表达式匹配并移除多余的'0x',并拼接剩下的部分
  239. let cleanedHex = hexStr.replace(/0x/g, '').replace(/\b0+/g, ''); // 第二个正则表达式用于移除前导零
  240. return cleanedHex;
  241. },
  242. // 开启消息监听
  243. notify() {
  244. let that = this;
  245. wx.notifyBLECharacteristicValueChange({
  246. deviceId: app.globalData.selmac.deviceId, // 设备ID,
  247. serviceId: app.globalData.selmac.serviceId, // 服务UUID
  248. characteristicId: app.globalData.selmac.characteristicId, // 特征值
  249. success(res) {
  250. console.log(res)
  251. // 监听消息变化的方法
  252. that.listenValueChange()
  253. },
  254. fail(err) {
  255. console.error(err)
  256. }
  257. })
  258. },
  259. // 消息变化
  260. listenValueChange() {
  261. let that = this;
  262. wx.onBLECharacteristicValueChange(res => {
  263. console.log(res)
  264. let resHex = that.ab2hex(res.value)
  265. console.log("resHex", resHex)
  266. let result = that.hexCharCodeToStr(resHex)
  267. console.log("消息变化", String(result))
  268. that.setData({
  269. callBack: String(result)
  270. });
  271. })
  272. },
  273. // 将16进制的内容转成我们看得懂的字符串内容
  274. hexCharCodeToStr(hexCharCodeStr) {
  275. let trimedStr = hexCharCodeStr.trim();
  276. let rawStr = trimedStr.substr(0, 2).toLowerCase() === "0x" ? trimedStr.substr(2) : trimedStr;
  277. let len = rawStr.length;
  278. if (len % 2 !== 0) {
  279. alert("存在非法字符!");
  280. return "";
  281. }
  282. let curCharCode;
  283. let resultStr = [];
  284. for (let i = 0; i < len; i = i + 2) {
  285. curCharCode = parseInt(rawStr.substr(i, 2), 16);
  286. resultStr.push(String.fromCharCode(curCharCode));
  287. }
  288. return resultStr.join("");
  289. },
  290. /*字符串转换16进制buffer*/
  291. stringToCmdBuffer(msg) {
  292. const buffer = new ArrayBuffer(msg.length)
  293. const dataView = new DataView(buffer)
  294. //dataView.setUint8(0, 0)
  295. for (let i = 0; i < msg.length; i++) {
  296. dataView.setUint8(i, msg.charAt(i).charCodeAt())
  297. };
  298. return buffer
  299. },
  300. // ArrayBuffer转16进度字符串示例
  301. ab2hex(buffer) {
  302. const hexArr = Array.prototype.map.call(
  303. new Uint8Array(buffer),
  304. function (bit) {
  305. return ('00' + bit.toString(16)).slice(-2)
  306. }
  307. )
  308. return hexArr.join('')
  309. }
  310. })