search.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. const app = getApp()
  2. const request_api = require('../../utils/api.js')
  3. Page({
  4. data: {
  5. inputText: '',
  6. qrcodeMac: "",
  7. searching: false,
  8. devicesList: []
  9. },
  10. MacInput: function (e) {
  11. this.setData({
  12. inputText: e.detail.value
  13. })
  14. if(e.detail.value.length == 12){
  15. console.log("mac addr:", e.detail.value)
  16. }
  17. },
  18. ConnectByID: function(targetID){
  19. var that=this
  20. console.log("ConnectByID:",targetID)
  21. var name,advertisData
  22. //获取当前的name和ad
  23. for (var i = 0; i < that.data.devicesList.length; i++) {
  24. if (targetID == that.data.devicesList[i].deviceId) {
  25. name=that.data.devicesList[i].name
  26. advertisData = that.data.devicesList[i].advertisData.substring(2)
  27. break
  28. }
  29. }
  30. console.log("connect:"+targetID+"|"+name+"|"+advertisData)
  31. wx.stopBluetoothDevicesDiscovery({
  32. success: function (res) {
  33. console.log(res)
  34. }
  35. })
  36. wx.showLoading({
  37. title: '连接蓝牙设备中...',
  38. })
  39. wx.createBLEConnection({
  40. deviceId: targetID,
  41. success: function (res) {
  42. console.log(res)
  43. wx.hideLoading()
  44. wx.showToast({
  45. title: '连接成功',
  46. icon: 'success',
  47. duration: 1000
  48. })
  49. console.log("getplatform")
  50. var platform = wx.getSystemInfoSync().platform
  51. console.log(platform)
  52. if(platform == "android"){
  53. wx.navigateTo({
  54. url: '../device/device?connectedDeviceId=' + targetID +'&name='+name+'&advertisData='+advertisData
  55. })
  56. }
  57. else{
  58. console.log('ios device')
  59. wx.getBLEDeviceServices({
  60. // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
  61. deviceId: targetID,
  62. success: function (res) {
  63. console.log("getBLEDeviceServices success")
  64. console.log(JSON.stringify(res))
  65. //获取设备特征对象
  66. wx.getBLEDeviceCharacteristics({
  67. deviceId: targetID,
  68. serviceId: '0000FEE7-0000-1000-8000-00805F9B34FB',
  69. success: function(res) {
  70. console.log('../device/device?connectedDeviceId=' + targetID +'&name='+name+'&advertisData='+advertisData)
  71. wx.navigateTo({
  72. url: '../device/device?connectedDeviceId=' + targetID +'&name='+name+'&advertisData='+advertisData
  73. })
  74. },
  75. fail:function(){
  76. wx.showModal({
  77. title: '温馨提示',
  78. content: '获取特征对象失败!',
  79. showCancel: false
  80. });
  81. quit(obj);
  82. }
  83. })
  84. },
  85. fail:function(){
  86. wx.showModal({
  87. title: '温馨提示',
  88. content: '获取服务失败!',
  89. showCancel:false
  90. });
  91. quit(obj);
  92. }
  93. })
  94. }
  95. },
  96. fail: function (res) {
  97. console.log(res)
  98. wx.hideLoading()
  99. wx.showModal({
  100. title: '提示',
  101. content: '连接失败',
  102. showCancel: false
  103. })
  104. }
  105. })
  106. },
  107. request_success_callback(res){
  108. //http request成功的回调
  109. console.log("http request success callback, data:", res.data)
  110. },
  111. request_fail_callback(fail_code){
  112. //http request失败的回调
  113. console.log("http request fail callback,fail_code:", fail_code)
  114. },
  115. request_api_test(){
  116. //使用api进行http request测试
  117. request_api.request('http://www.id27149.com:2222/codecInfo', 'POST', {'method':'get_device_phy_value_list','args':["BMW_790"]}, this.request_success_callback, this.request_fail_callback)
  118. },
  119. request_test() {
  120. //使用wx自带的sdk进行http request测试
  121. wx.request({
  122. url: 'http://www.yykvz.cn',//服务器接口地址
  123. method:'GET',//请求类型
  124. header: {
  125. 'content-type': 'application/json'
  126. },
  127. success:function(res) {
  128. console.log(res)
  129. },
  130. fail:function(err) {
  131. console.log(err.errno)
  132. }
  133. })
  134. },
  135. RequestTest: function(){
  136. // 点击网络请求测试按钮事件
  137. console.log("http request测试")
  138. // http request测试.
  139. this.request_api_test()
  140. },
  141. SearchClick: function(){
  142. var that=this
  143. that.setData({qrcodeMac:""})
  144. that.Search()
  145. },
  146. Search: function () {
  147. var that = this
  148. console.log("search:",that.data.searching)
  149. if (!that.data.searching) {
  150. //关闭现有的蓝牙连接
  151. wx.closeBluetoothAdapter({
  152. complete: function (res) {
  153. console.log(res)
  154. //打开蓝牙适配
  155. wx.openBluetoothAdapter({
  156. success: function (res) {
  157. console.log(res)
  158. wx.getBluetoothAdapterState({
  159. success: function (res) {
  160. console.log(res)
  161. }
  162. })
  163. //开始搜索蓝牙设备
  164. wx.startBluetoothDevicesDiscovery({
  165. allowDuplicatesKey: false,
  166. success: function (res) {
  167. console.log(res)
  168. that.setData({
  169. searching: true,
  170. devicesList: []
  171. })
  172. }
  173. })
  174. },
  175. fail: function (res) {
  176. console.log(res)
  177. wx.showModal({
  178. title: '提示',
  179. content: '请检查手机蓝牙是否打开',
  180. showCancel: false,
  181. success: function (res) {
  182. that.setData({
  183. searching: false
  184. })
  185. }
  186. })
  187. }
  188. })
  189. }
  190. })
  191. }
  192. else {
  193. wx.stopBluetoothDevicesDiscovery({
  194. success: function (res) {
  195. console.log(res)
  196. that.setData({
  197. searching: false
  198. })
  199. }
  200. })
  201. }
  202. },
  203. //连接上述mac地址蓝牙按钮事件
  204. ConnectMacBle:function(){
  205. var that = this
  206. console.log("开始连接蓝牙,mac地址:", that.data.inputText)
  207. if(that.data.inputText.length == 12){
  208. that.Search()
  209. setTimeout(function() {
  210. //连接文本框中输入的mac地址的蓝牙设备
  211. that.ConnectByMac(that.data.inputText)
  212. }, 1500)
  213. }else{
  214. console.log("mac addr len error")
  215. }
  216. },
  217. //扫描二维码按钮事件
  218. ScanQR: function(){
  219. var that=this
  220. that.setData({
  221. searching: false,
  222. qrcodeMac:""
  223. })
  224. wx.scanCode({
  225. success(res) {
  226. that.setData({
  227. qrcodeMac:res.result,
  228. })
  229. //扫码成功后,搜索查找指定的蓝牙设备并连接
  230. that.Search()
  231. setTimeout(function() {
  232. that.ConnectByMac(that.data.qrcodeMac)
  233. }, 1500)
  234. }
  235. })
  236. },
  237. ConnectClick: function (e) {
  238. var that = this
  239. console.log(e.currentTarget.id)
  240. that.ConnectByID(e.currentTarget.id)
  241. },
  242. ConnectByMac: function(mac_addr){
  243. var that=this
  244. console.log("qrcode:",mac_addr," devicelen:",that.data.devicesList.length)
  245. var target_advertis_data = "1300"+mac_addr
  246. for (var i = 0; i < that.data.devicesList.length; i++) {
  247. if (that.data.devicesList[i].advertisData.length>12){
  248. if (target_advertis_data == that.data.devicesList[i].advertisData) {
  249. var targetID=that.data.devicesList[i].deviceId
  250. //避免短时间内重复连接
  251. if(that.data.searching == false)
  252. return
  253. that.setData({
  254. searching: false,
  255. qrcodeMac:""
  256. })
  257. that.ConnectByID(targetID)
  258. break
  259. }
  260. }
  261. }
  262. that.setData({
  263. searching: false,
  264. qrcodeMac:""
  265. })
  266. },
  267. onLoad: function (options) {
  268. var that = this
  269. // 打开小程序后即创建socket连接
  270. // this.WebsocketConnStart();
  271. var list_height = ((app.globalData.SystemInfo.windowHeight - 50) * (750 / app.globalData.SystemInfo.windowWidth)) - 110
  272. that.setData({
  273. list_height: list_height
  274. })
  275. wx.onBluetoothAdapterStateChange(function (res) {
  276. console.log(res)
  277. that.setData({
  278. searching: res.discovering
  279. })
  280. if (!res.available) {
  281. that.setData({
  282. searching: false
  283. })
  284. }
  285. })
  286. wx.onBluetoothDeviceFound(function (devices) {
  287. //剔除重复设备,兼容不同设备API的不同返回值
  288. var isnotexist = true
  289. if (devices.deviceId) {
  290. if (devices.advertisData)
  291. {
  292. devices.advertisData = app.buf2hex(devices.advertisData)
  293. }
  294. else
  295. {
  296. devices.advertisData = ''
  297. }
  298. console.log(devices)
  299. for (var i = 0; i < that.data.devicesList.length; i++) {
  300. if (devices.deviceId == that.data.devicesList[i].deviceId) {
  301. isnotexist = false
  302. }
  303. }
  304. if (isnotexist) {
  305. that.data.devicesList.push(devices)
  306. }
  307. }
  308. else if (devices.devices) {
  309. if (devices.devices[0].advertisData)
  310. {
  311. devices.devices[0].advertisData = app.buf2hex(devices.devices[0].advertisData)
  312. }
  313. else
  314. {
  315. devices.devices[0].advertisData = ''
  316. }
  317. console.log(devices.devices[0])
  318. for (var i = 0; i < that.data.devicesList.length; i++) {
  319. if (devices.devices[0].deviceId == that.data.devicesList[i].deviceId) {
  320. isnotexist = false
  321. }
  322. }
  323. if (isnotexist) {
  324. that.data.devicesList.push(devices.devices[0])
  325. }
  326. }
  327. else if (devices[0]) {
  328. if (devices[0].advertisData)
  329. {
  330. devices[0].advertisData = app.buf2hex(devices[0].advertisData)
  331. }
  332. else
  333. {
  334. devices[0].advertisData = ''
  335. }
  336. console.log(devices[0])
  337. for (var i = 0; i < devices_list.length; i++) {
  338. if (devices[0].deviceId == that.data.devicesList[i].deviceId) {
  339. isnotexist = false
  340. }
  341. }
  342. if (isnotexist) {
  343. that.data.devicesList.push(devices[0])
  344. }
  345. }
  346. that.setData({
  347. devicesList: that.data.devicesList
  348. })
  349. })
  350. },
  351. onUnload: function(){
  352. },
  353. onReady: function () {
  354. },
  355. onShow: function () {
  356. if(this.data.qrcodeMac=="")
  357. {
  358. wx.closeBluetoothAdapter({
  359. complete: function (res) {
  360. }
  361. })
  362. }
  363. },
  364. onHide: function () {
  365. var that = this
  366. that.setData({
  367. devicesList: []
  368. })
  369. if (this.data.searching) {
  370. wx.stopBluetoothDevicesDiscovery({
  371. success: function (res) {
  372. console.log(res)
  373. that.setData({
  374. searching: false
  375. })
  376. }
  377. })
  378. }
  379. }
  380. })