search.js 13 KB

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