bleConnect.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. // pages/blueconn/blueconn.js
  2. var app = getApp()
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. list: [],
  9. services: [],
  10. serviceId: 0,
  11. writeCharacter: false,
  12. readCharacter: false,
  13. notifyCharacter: false,
  14. isScanning:false
  15. },
  16. //搜索设备
  17. startSearch: function () {
  18. var that = this
  19. wx.openBluetoothAdapter({
  20. success: function (res) {
  21. wx.getBluetoothAdapterState({
  22. success: function (res) {
  23. console.log('openBluetoothAdapter success', res)
  24. if (res.available) {
  25. if (res.discovering) {
  26. wx.stopBluetoothDevicesDiscovery({
  27. success: function (res) {
  28. console.log(res)
  29. }
  30. })
  31. }else{
  32. // that.startBluetoothDevicesDiscovery()
  33. that.getBluetoothDevices()
  34. }
  35. // that.checkPemission()
  36. } else {
  37. wx.showModal({
  38. title: '提示',
  39. content: '本机蓝牙不可用',
  40. showCancel: false
  41. })
  42. }
  43. },
  44. })
  45. }, fail: function () {
  46. // if (res.errCode === 10001) {
  47. // wx.onBluetoothAdapterStateChange(function (res) {
  48. // console.log('onBluetoothAdapterStateChange', res)
  49. // if (res.available) {
  50. // this.startBluetoothDevicesDiscovery()
  51. // }
  52. // })
  53. // }
  54. wx.showModal({
  55. title: '提示',
  56. content: '蓝牙初始化失败,请到设置打开蓝牙',
  57. showCancel: false
  58. })
  59. }
  60. })
  61. },
  62. checkPemission: function () { //android 6.0以上需授权地理位置权限
  63. var that = this
  64. var platform = app.BLEInformation.platform
  65. if (platform == "ios") {
  66. app.globalData.platform = "ios"
  67. that.getBluetoothDevices()
  68. } else if (platform == "android") {
  69. app.globalData.platform = "android"
  70. console.log(app.getSystem().substring(app.getSystem().length - (app.getSystem().length - 8), app.getSystem().length - (app.getSystem().length - 8) + 1))
  71. if (app.getSystem().substring(app.getSystem().length - (app.getSystem().length - 8), app.getSystem().length - (app.getSystem().length - 8) + 1) > 5) {
  72. wx.getSetting({
  73. success: function (res) {
  74. console.log(res)
  75. if (!res.authSetting['scope.userLocation']) {
  76. wx.authorize({
  77. scope: 'scope.userLocation',
  78. complete: function (res) {
  79. that.getBluetoothDevices()
  80. }
  81. })
  82. } else {
  83. that.getBluetoothDevices()
  84. }
  85. }
  86. })
  87. }
  88. }
  89. },
  90. getBluetoothDevices: function () { //获取蓝牙设备信息
  91. var that = this
  92. console.log("start search")
  93. wx.showLoading({
  94. title: '正在加载',
  95. icon: 'loading',
  96. })
  97. that.setData({
  98. isScanning:true
  99. })
  100. wx.startBluetoothDevicesDiscovery({
  101. success: function (res) {
  102. console.log(res)
  103. setTimeout(function () {
  104. wx.getBluetoothDevices({
  105. success: function (res) {
  106. var devices = []
  107. var num = 0
  108. for (var i = 0; i < res.devices.length; ++i) {
  109. if (res.devices[i].name != "未知设备") {
  110. devices[num] = res.devices[i]
  111. num++
  112. }
  113. }
  114. that.setData({
  115. list: devices,
  116. isScanning:false
  117. })
  118. wx.hideLoading()
  119. wx.stopPullDownRefresh()
  120. wx.stopBluetoothDevicesDiscovery({
  121. success: function (res) {
  122. console.log("停止搜索蓝牙")
  123. }
  124. })
  125. },
  126. })
  127. }, 5000)
  128. },
  129. })
  130. },
  131. bindViewTap: function (e) {
  132. var that = this
  133. wx.stopBluetoothDevicesDiscovery({
  134. success: function (res) { console.log(res) },
  135. })
  136. that.setData({
  137. serviceId: 0,
  138. writeCharacter: false,
  139. readCharacter: false,
  140. notifyCharacter: false
  141. })
  142. console.log(e.currentTarget.dataset.title)
  143. wx.showLoading({
  144. title: '正在连接',
  145. })
  146. wx.createBLEConnection({
  147. deviceId: e.currentTarget.dataset.title,
  148. success: function (res) {
  149. console.log(res)
  150. app.BLEInformation.deviceId = e.currentTarget.dataset.title
  151. that.getSeviceId()
  152. }, fail: function (e) {
  153. wx.showModal({
  154. title: '提示',
  155. content: '连接失败',
  156. showCancel: false
  157. })
  158. console.log(e)
  159. wx.hideLoading()
  160. }, complete: function (e) {
  161. console.log(e)
  162. }
  163. })
  164. },
  165. getSeviceId: function () {
  166. var that = this
  167. var platform = app.BLEInformation.platform
  168. console.log(app.BLEInformation.deviceId)
  169. wx.getBLEDeviceServices({
  170. deviceId: app.BLEInformation.deviceId,
  171. success: function (res) {
  172. console.log(res)
  173. // var realId = ''
  174. // if (platform == 'android') {
  175. // // for(var i=0;i<res.services.length;++i){
  176. // // var item = res.services[i].uuid
  177. // // if (item == "0000FEE7-0000-1000-8000-00805F9B34FB"){
  178. // realId = "0000FEE7-0000-1000-8000-00805F9B34FB"
  179. // // break;
  180. // // }
  181. // // }
  182. // } else if (platform == 'ios') {
  183. // // for(var i=0;i<res.services.length;++i){
  184. // // var item = res.services[i].uuid
  185. // // if (item == "49535343-FE7D-4AE5-8FA9-9FAFD205E455"){
  186. // realId = "49535343-FE7D-4AE5-8FA9-9FAFD205E455"
  187. // // break
  188. // // }
  189. // // }
  190. // }
  191. // app.BLEInformation.serviceId = realId
  192. that.setData({
  193. services: res.services
  194. })
  195. that.getCharacteristics()
  196. }, fail: function (e) {
  197. console.log(e)
  198. }, complete: function (e) {
  199. console.log(e)
  200. }
  201. })
  202. },
  203. getCharacteristics: function () {
  204. var that = this
  205. var list = that.data.services
  206. var num = that.data.serviceId
  207. var write = that.data.writeCharacter
  208. var read = that.data.readCharacter
  209. var notify = that.data.notifyCharacter
  210. wx.getBLEDeviceCharacteristics({
  211. deviceId: app.BLEInformation.deviceId,
  212. serviceId: list[num].uuid,
  213. success: function (res) {
  214. console.log(res)
  215. for (var i = 0; i < res.characteristics.length; ++i) {
  216. var properties = res.characteristics[i].properties
  217. var item = res.characteristics[i].uuid
  218. if (!notify) {
  219. if (properties.notify) {
  220. app.BLEInformation.notifyCharaterId = item
  221. app.BLEInformation.notifyServiceId = list[num].uuid
  222. notify = true
  223. }
  224. }
  225. if (!write) {
  226. if (properties.write) {
  227. app.BLEInformation.writeCharaterId = item
  228. app.BLEInformation.writeServiceId = list[num].uuid
  229. write = true
  230. }
  231. }
  232. if (!read) {
  233. if (properties.read) {
  234. app.BLEInformation.readCharaterId = item
  235. app.BLEInformation.readServiceId = list[num].uuid
  236. read = true
  237. }
  238. }
  239. }
  240. if (!write || !notify || !read) {
  241. num++
  242. that.setData({
  243. writeCharacter: write,
  244. readCharacter: read,
  245. notifyCharacter: notify,
  246. serviceId: num
  247. })
  248. if (num == list.length) {
  249. wx.showModal({
  250. title: '提示',
  251. content: '找不到该读写的特征值',
  252. showCancel: false
  253. })
  254. } else {
  255. that.getCharacteristics()
  256. }
  257. } else {
  258. wx.showToast({
  259. title: '连接成功',
  260. })
  261. that.openControl()
  262. }
  263. }, fail: function (e) {
  264. console.log(e)
  265. }, complete: function (e) {
  266. console.log("write:" + app.BLEInformation.writeCharaterId)
  267. console.log("read:" + app.BLEInformation.readCharaterId)
  268. console.log("notify:" + app.BLEInformation.notifyCharaterId)
  269. }
  270. })
  271. },
  272. openControl: function () {//连接成功返回主页
  273. wx.navigateTo({
  274. url: '../home/home',
  275. })
  276. },
  277. /**
  278. * 生命周期函数--监听页面加载
  279. */
  280. onLoad: function (options) {
  281. app.BLEInformation.platform = app.getPlatform()
  282. },
  283. /**
  284. * 生命周期函数--监听页面初次渲染完成
  285. */
  286. onReady: function () {
  287. },
  288. /**
  289. * 生命周期函数--监听页面显示
  290. */
  291. onShow: function () {
  292. },
  293. /**
  294. * 生命周期函数--监听页面隐藏
  295. */
  296. onHide: function () {
  297. },
  298. /**
  299. * 生命周期函数--监听页面卸载
  300. */
  301. onUnload: function () {
  302. },
  303. // /**
  304. // * 页面相关事件处理函数--监听用户下拉动作
  305. // */
  306. // onPullDownRefresh: function () {
  307. // // var that = this
  308. // // wx.startPullDownRefresh({})
  309. // // that.startSearch()
  310. // },
  311. /**
  312. * 页面上拉触底事件的处理函数
  313. */
  314. onReachBottom: function () {
  315. },
  316. /**
  317. * 用户点击右上角分享
  318. */
  319. onShareAppMessage: function () {
  320. }
  321. })