search.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. const app = getApp()
  2. app.globalData.pageid=1;
  3. Page({
  4. data: {
  5. searching: false,
  6. connectedDeviceId: '',
  7. services: {},
  8. characteristics: {},
  9. devicesList: [],
  10. PageAddr: ["../device/device", "../dropfoot/dropfoot"],
  11. },
  12. checkUserHavePermission()
  13. {
  14. getUserPermission('scope.bluetooth').then(() => {
  15. // 用户已授权获取地理位置
  16. this.getUserLocation(()=>{
  17. // 获取位置 进行其他操作
  18. })
  19. }).catch(() => {
  20. // 用户未授权/拒绝获取地理位置
  21. // 需要提示用户手动打开设置去授权
  22. this.openSettingPop(); // 打开提示弹窗
  23. })
  24. },
  25. Search:function getUserPermission(permissionName) {
  26. return new Promise((resolve, reject) => {
  27. wx.getSetting({
  28. success(res) {
  29. if (res.authSetting[permissionName]) {
  30. resolve(true);
  31. } else if (res.authSetting[permissionName] === false) {
  32. reject(false);
  33. } else {
  34. wx.authorize({
  35. scope: permissionName,
  36. success() {
  37. resolve(true);
  38. },
  39. fail() {
  40. reject(false);
  41. }
  42. });
  43. }
  44. },
  45. fail() {
  46. reject(false);
  47. }
  48. });
  49. });
  50. },
  51. search2: function () {
  52. },
  53. back: function () {
  54. wx.navigateBack()
  55. },
  56. home: function () {
  57. },
  58. Search: function () {
  59. var that = this
  60. if (!that.data.searching) {
  61. // 关闭蓝牙模块
  62. wx.closeBluetoothAdapter({
  63. // 接口调用结束的回调函数(调用成功、失败都会执行)
  64. complete: function (res) {
  65. console.log(res)
  66. // 初始化蓝牙模块。iOS 上开启主机/从机(外围设备)模式时需分别调用一次,并指定对应的 mode。
  67. wx.openBluetoothAdapter({
  68. success: function (res) {
  69. console.log(res)
  70. // 获取本机蓝牙适配器状态
  71. wx.getBluetoothAdapterState({
  72. success: function (res) {
  73. console.log(res)
  74. }
  75. })
  76. // 开始搜寻附近的蓝牙外围设备。
  77. //此操作比较耗费系统资源,请在搜索到需要的设备后及时调用 wx.stopBluetoothDevicesDiscovery 停止搜索
  78. wx.startBluetoothDevicesDiscovery({
  79. allowDuplicatesKey: false,
  80. success: function (res) {
  81. console.log(res)
  82. that.setData({
  83. searching: true,
  84. devicesList: []
  85. })
  86. }
  87. })
  88. },
  89. fail: function (res) {
  90. console.log(res)
  91. wx.showModal({
  92. title: '提示',
  93. content: '请检查手机蓝牙是否打开',
  94. showCancel: false,
  95. success: function (res) {
  96. that.setData({
  97. searching: false
  98. })
  99. }
  100. })
  101. }
  102. })
  103. }
  104. })
  105. } else {
  106. wx.stopBluetoothDevicesDiscovery({
  107. success: function (res) {
  108. console.log(res)
  109. that.setData({
  110. searching: false
  111. })
  112. }
  113. })
  114. }
  115. },
  116. Connect: function (e) {
  117. var that = this
  118. var advertisData, name
  119. console.log(e.currentTarget.id)
  120. for (var i = 0; i < that.data.devicesList.length; i++) {
  121. if (e.currentTarget.id == that.data.devicesList[i].deviceId) {
  122. name = that.data.devicesList[i].localName
  123. advertisData = that.data.devicesList[i].advertisData
  124. }
  125. }
  126. wx.stopBluetoothDevicesDiscovery({
  127. success: function (res) {
  128. console.log(res)
  129. that.setData({
  130. searching: false
  131. })
  132. }
  133. })
  134. if (name != 'WH-BLE 103') {
  135. wx.showModal({
  136. title: '提示',
  137. content: '请选择正确设备连接',
  138. showCancel: false,
  139. success: function (res) {
  140. that.setData({
  141. searching: false
  142. })
  143. }
  144. })
  145. console.log('设备名称错误')
  146. } else {
  147. //显示loading提示框
  148. wx.showLoading({
  149. title: '连接蓝牙设备中...',
  150. })
  151. wx.createBLEConnection({
  152. deviceId: e.currentTarget.id,
  153. success: function (res) {
  154. console.log("createBLEConnection success!")
  155. // //ble获取蓝牙服务{获取特征值列表{开启端口监听{定时心跳发送{允许发送心跳{发送成功consloe显示}}}}}
  156. wx.getBLEDeviceServices({
  157. deviceId: e.currentTarget.id,
  158. /*蓝牙设备id */
  159. success: function (res) {
  160. /**接口调用成功的回调函数 */
  161. console.log("getBLEDeviceServices success!")
  162. that.setData({
  163. services: res.services
  164. })
  165. wx.getBLEDeviceCharacteristics({
  166. /**获取蓝牙低功耗设备某个服务中所有特征 */
  167. deviceId: e.currentTarget.id,
  168. /**蓝牙设备id */
  169. serviceId: res.services[0].uuid,
  170. /**蓝牙服务uuid */
  171. success: function (res) {
  172. /**接口调用成功的回调函数 */
  173. console.log("getBLEDeviceCharacteristics success!")
  174. that.setData({
  175. characteristics: res.characteristics
  176. })
  177. /**启用蓝牙低功耗设备特征值变化时的notify功能 */
  178. wx.notifyBLECharacteristicValueChange({
  179. state: true, //是否启用notify
  180. deviceId: e.currentTarget.id, //蓝牙设备id
  181. serviceId: that.data.services[0].uuid, //蓝牙特征对应服务的uuid
  182. characteristicId: that.data.characteristics[0].uuid, //蓝牙特征的uuid
  183. success: function (res) //接口调用成功的回调函数
  184. {
  185. console.log('启用notify成功')
  186. //*****在这跳转到device页面 url:路径?参数 参数键与参数值用 = 相连,不同参数用 & 分隔************
  187. wx.navigateTo({
  188. url: that.data.PageAddr[app.globalData.pageid] + '?' + 'connectedDeviceId=' + e.currentTarget.id + '&name=' + name +'&serviceId='+that.data.services[0].uuid,
  189. })
  190. //隐藏loading提示框
  191. wx.hideLoading()
  192. }
  193. })
  194. }
  195. })
  196. }
  197. })
  198. },
  199. fail: function (res) {
  200. console.log(res)
  201. wx.hideLoading()
  202. wx.showModal({
  203. title: '提示',
  204. content: '连接失败',
  205. showCancel: false
  206. })
  207. }
  208. })
  209. }
  210. },
  211. onLoad: function (options) {
  212. var that = this
  213. var list_height = ((app.globalData.SystemInfo.windowHeight - 50) * (750 / app.globalData.SystemInfo.windowWidth)) - 60
  214. that.setData({
  215. list_height: list_height
  216. })
  217. wx.onBluetoothAdapterStateChange(function (res) {
  218. console.log(res)
  219. that.setData({
  220. searching: res.discovering
  221. })
  222. if (!res.available) {
  223. that.setData({
  224. searching: false
  225. })
  226. }
  227. })
  228. wx.onBluetoothDeviceFound(function (devices) {
  229. //剔除重复设备,兼容不同设备API的不同返回值
  230. var isnotexist = true
  231. if (devices.devices[0].name != 'WH-BLE 103'){
  232. isnotexist = false
  233. }
  234. else if (devices.deviceId) {
  235. if (devices.advertisData) {
  236. devices.advertisData = app.buf2hex(devices.advertisData)
  237. } else {
  238. devices.advertisData = ''
  239. }
  240. // console.log(devices)
  241. for (var i = 0; i < that.data.devicesList.length; i++) {
  242. if (devices.deviceId == that.data.devicesList[i].deviceId) {
  243. isnotexist = false
  244. }
  245. }
  246. if (isnotexist) {
  247. that.data.devicesList.push(devices)
  248. }
  249. } else if (devices.devices) {
  250. if (devices.devices[0].advertisData) {
  251. devices.devices[0].advertisData = app.buf2hex(devices.devices[0].advertisData)
  252. } else {
  253. devices.devices[0].advertisData = ''
  254. }
  255. console.log(devices.devices[0])
  256. for (var i = 0; i < that.data.devicesList.length; i++) {
  257. if (devices.devices[0].deviceId == that.data.devicesList[i].deviceId) {
  258. isnotexist = false
  259. }
  260. }
  261. if (isnotexist) {
  262. that.data.devicesList.push(devices.devices[0])
  263. }
  264. } else if (devices[0]) {
  265. if (devices[0].advertisData) {
  266. devices[0].advertisData = app.buf2hex(devices[0].advertisData)
  267. } else {
  268. devices[0].advertisData = ''
  269. }
  270. console.log(devices[0])
  271. for (var i = 0; i < devices_list.length; i++) {
  272. if (devices[0].deviceId == that.data.devicesList[i].deviceId) {
  273. isnotexist = false
  274. }
  275. }
  276. if (isnotexist) {
  277. that.data.devicesList.push(devices[0])
  278. }
  279. }
  280. that.setData({
  281. devicesList: that.data.devicesList
  282. })
  283. })
  284. },
  285. onReady: function () {
  286. },
  287. onShow: function () {
  288. },
  289. onHide: function () {
  290. var that = this
  291. that.setData({
  292. devicesList: []
  293. })
  294. if (this.data.searching) {
  295. wx.stopBluetoothDevicesDiscovery({
  296. success: function (res) {
  297. console.log(res)
  298. that.setData({
  299. searching: false
  300. })
  301. }
  302. })
  303. }
  304. },
  305. })
  306. wx.getSetting({
  307. success(res) {
  308. console.log(res.authSetting)
  309. //判断是否有'scope.bluetooth'属性
  310. if (res.authSetting.hasOwnProperty('scope.bluetooth')) {
  311. //'scope.bluetooth'属性存在,且为false
  312. if (!res.authSetting['scope.bluetooth']) {
  313. //弹窗授权
  314. wx.openSetting({
  315. success(res) {
  316. console.log(res.authSetting)
  317. }
  318. })
  319. }
  320. }
  321. else
  322. //'scope.bluetooth'属性不存在,需要授权
  323. wx.authorize({
  324. scope: 'scope.bluetooth',
  325. success() {
  326. // 用户已经同意小程序使用手机蓝牙功能,后续调用 蓝牙 接口不会弹窗询问
  327. console.log(res.authSetting)
  328. }
  329. })
  330. }
  331. })