Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

BluetoothLink.js 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. var plugin = requirePlugin("myPlugin")
  2. //var plugin = require("../../plugins/index.js");
  3. const app = getApp();
  4. function inArray(arr, key, val) {
  5. for (let i = 0; i < arr.length; i++) {
  6. if (arr[i][key] === val) {
  7. return i;
  8. }
  9. }
  10. return -1;
  11. }
  12. Page({
  13. data: {
  14. list: [],
  15. devices: [], //
  16. dev1: "已发现",
  17. dev2: "台设备",
  18. data: {},
  19. dainji: "绑定设备",
  20. shebei: "",
  21. headTip: '下拉刷新',
  22. indx: "0",
  23. // 显示隐藏
  24. showView: true,
  25. deviceInfo: ({
  26. macAddress: "",
  27. name: "",
  28. advertisData: "",
  29. })
  30. },
  31. onLoad: function() {
  32. var that = this;
  33. // that.setData({
  34. // list: [],
  35. // devices: [],
  36. // });
  37. // 获取设备列表
  38. // that.openBluetoothAdapter();
  39. // console.log("openBluetoothAdapter");
  40. },
  41. // 点击切换显示隐藏
  42. onShow: function() {
  43. var that = this;
  44. that.setData({
  45. list: [],
  46. devices: [],
  47. });
  48. // that.refreshInterface();
  49. that.openBluetoothAdapter();
  50. setTimeout(() => {
  51. var deviceInfo = wx.getStorageSync('deviceInfo')
  52. console.log(deviceInfo);
  53. if (deviceInfo == "1" || deviceInfo == null) {
  54. that.setData({
  55. dainji: "绑定设备",
  56. });
  57. } else {
  58. // if (deviceInfo.macAddress.length == 9) {
  59. // that.setData({
  60. // dainji: "绑定设备",
  61. // });
  62. // }else{
  63. // that.setData({
  64. // dainji: "绑定设备",
  65. // dev1: "已发现:",
  66. // dev2: "台设备,已连接",
  67. // shebei:wx.getStorageSync('deviceInfo').macAddress
  68. // });
  69. // }
  70. }
  71. }, 1000)
  72. },
  73. onHide: function() {
  74. var that = this;
  75. that.stopBluetoothDevicesDiscovery();
  76. },
  77. onPullDownRefresh: function() {
  78. console.log('下拉刷新')
  79. var that = this;
  80. that.setData({
  81. list: [],
  82. devices: [],
  83. });
  84. that.openBluetoothAdapter();
  85. wx.showNavigationBarLoading() //在标题栏中显示加载
  86. setTimeout(() => {
  87. wx.hideNavigationBarLoading() //完成停止加载
  88. wx.stopPullDownRefresh() //停止下拉刷新
  89. }, 3000)
  90. },
  91. // 官方广播称开始
  92. openBluetoothAdapter: function() {
  93. wx.openBluetoothAdapter({
  94. success: (res) => {
  95. console.log('openBluetoothAdapter success', res)
  96. this.startBluetoothDevicesDiscovery()
  97. this.setData({ // 我们不一样
  98. shebei: ",请稍后",
  99. dev1: "已发现",
  100. dev2: "台设备",
  101. })
  102. },
  103. fail: (res) => {
  104. if (res.errCode === 10001) {
  105. wx.onBluetoothAdapterStateChange(function(res) {
  106. console.log('onBluetoothAdapterStateChange', res)
  107. if (res.available) {
  108. this.startBluetoothDevicesDiscovery()
  109. }
  110. })
  111. }
  112. this.setData({ // 我们不一样
  113. shebei: "请开启手机蓝牙",
  114. dev1: "",
  115. dev2: "",
  116. })
  117. }
  118. })
  119. },
  120. getBluetoothAdapterState() {
  121. wx.getBluetoothAdapterState({
  122. success: (res) => {
  123. // console.log('getBluetoothAdapterState', res)
  124. if (res.discovering) {
  125. this.onBluetoothDeviceFound()
  126. } else if (res.available) {
  127. this.startBluetoothDevicesDiscovery()
  128. }
  129. }
  130. })
  131. },
  132. startBluetoothDevicesDiscovery() {
  133. wx.startBluetoothDevicesDiscovery({
  134. // services: [AICARE_SERVICE_UUID, aicare_service_uuid_android],
  135. allowDuplicatesKey: true,
  136. success: (res) => {
  137. console.log('startBluetoothDevicesDiscovery success', res)
  138. this.onBluetoothDeviceFound()
  139. },
  140. })
  141. },
  142. onBluetoothDeviceFound() {
  143. console.log("reresresresresress");
  144. wx.onBluetoothDeviceFound((res) => {
  145. console.log(res);
  146. res.devices.forEach(device => {
  147. if (device.advertisServiceUUIDs == null || device.advertisServiceUUIDs.length == 0) {
  148. var data15 = plugin.getBbroadcastData(device.advertisData);
  149. if (data15 != null) {
  150. console.log("广播秤");
  151. device.advertisData = data15.address;
  152. device.name = "el-deviceinfo";
  153. const foundDevices = this.data.devices
  154. const idx = inArray(foundDevices, 'deviceId', device.deviceId)
  155. const data = {}
  156. if (idx === -1) {
  157. data[`devices[${foundDevices.length}]`] = device
  158. } else {
  159. data[`devices[${idx}]`] = device
  160. }
  161. console.log(device);
  162. this.setData(data)
  163. }
  164. } else {
  165. console.log("不是我们的秤");
  166. }
  167. })
  168. })
  169. },
  170. stopBluetoothDevicesDiscovery() {
  171. wx.stopBluetoothDevicesDiscovery(
  172. console.log("蓝牙搜索已关闭")
  173. )
  174. },
  175. closeBluetoothAdapter() {
  176. wx.closeBluetoothAdapter()
  177. console.log("蓝牙模块已关闭")
  178. },
  179. //点击绑定设备处理
  180. bindViewTap: function(e) {
  181. var that = this;
  182. console.log('点击事件--绑定对应的秤');
  183. wx.closeBLEConnection({
  184. deviceId: wx.getStorageSync('deviceInfo').macAddress,
  185. success(res) {
  186. console.log(res)
  187. }
  188. })
  189. that.stopBluetoothDevicesDiscovery(); // 关闭蓝牙搜索
  190. console.log(e.currentTarget.dataset.title);
  191. console.log(e.currentTarget.dataset.name);
  192. console.log(e.currentTarget.dataset.macaddress);
  193. if (e.currentTarget.dataset.name != "el-deviceinfo") {
  194. console.log('点击连接--连接秤');
  195. wx.showLoading({
  196. title: '正在连接....',
  197. })
  198. // 初适化蓝牙模块
  199. wx.openBluetoothAdapter({
  200. success: function(res) {},
  201. fail: function() {
  202. wx.showToast({
  203. title: '启动蓝牙失败,请开启蓝牙',
  204. icon: 'noine',
  205. duration: 1500,
  206. })
  207. }
  208. })
  209. wx.createBLEConnection({
  210. deviceId: e.currentTarget.dataset.title,
  211. success: function(res) {
  212. console.log('连接成功');
  213. console.log(res);
  214. wx.hideLoading()
  215. wx.showToast({
  216. title: '绑定成功',
  217. icon: 'success',
  218. duration: 1000,
  219. })
  220. that.setData({ // 我们不一样
  221. deviceInfo: ({
  222. macAddress: e.currentTarget.dataset.title,
  223. name: e.currentTarget.dataset.name,
  224. advertisData: e.currentTarget.dataset.advertisData,
  225. macdidname: e.currentTarget.dataset.macdidname
  226. })
  227. })
  228. wx.setStorageSync("deviceInfo", that.data.deviceInfo);
  229. console.log("新绑定设备信息:")
  230. console.log(wx.getStorageSync('deviceInfo'))
  231. wx.switchTab({
  232. url: '../index/index'
  233. })
  234. },
  235. fail: function(res) {
  236. console.log('连接秤连接失败');
  237. console.log(res);
  238. wx.hideLoading()
  239. console.log(res);
  240. wx.showToast({
  241. title: '连接失败,再试试',
  242. icon: 'none',
  243. duration: 2000,
  244. })
  245. }
  246. })
  247. } else {
  248. // 广播秤BM15 连接
  249. // 初适化蓝牙模块
  250. wx.showLoading({
  251. title: '正在绑定广播....',
  252. })
  253. wx.closeBluetoothAdapter({
  254. success: function(res) {
  255. console.log(res)
  256. wx.hideLoading()
  257. wx.showToast({
  258. title: '广播秤绑定成功',
  259. icon: 'success',
  260. duration: 1000,
  261. })
  262. that.setData({ // 我们不一样
  263. deviceInfo: ({
  264. macAddress: e.currentTarget.dataset.title,
  265. name: e.currentTarget.dataset.name,
  266. advertisData: e.currentTarget.dataset.advertisData,
  267. macdidname: e.currentTarget.dataset.macdidname,
  268. })
  269. })
  270. wx.setStorageSync("deviceInfo", that.data.deviceInfo);
  271. wx.setStorageSync("devMacAddress", e.currentTarget.dataset.macaddress);
  272. console.log("新绑定设备信息:" + e.currentTarget.dataset.macaddress);
  273. // wx.switchTab({
  274. // url: '../index/index'
  275. // })
  276. wx.navigateTo({
  277. url: '../index/index'
  278. })
  279. }
  280. })
  281. }
  282. // 跳转回主界面
  283. // wx.switchTab({
  284. // url: '../measure/measure'
  285. // })
  286. },
  287. })