const app = getApp() const request_api = require('../../utils/api.js') const websocket_api = require('../../utils/socket_api.js') Page({ data: { inputText: '', qrcodeMac: "", searching: false, devicesList: [] }, MacInput: function (e) { this.setData({ inputText: e.detail.value }) if(e.detail.value.length == 12){ console.log("mac addr:", e.detail.value) } }, ConnectByID: function(targetID){ var that=this console.log("ConnectByID:",targetID) var name,advertisData //获取当前的name和ad for (var i = 0; i < that.data.devicesList.length; i++) { if (targetID == that.data.devicesList[i].deviceId) { name=that.data.devicesList[i].name advertisData = that.data.devicesList[i].advertisData.substring(2) break } } console.log("connect:"+targetID+"|"+name+"|"+advertisData) wx.stopBluetoothDevicesDiscovery({ success: function (res) { console.log(res) } }) wx.showLoading({ title: '连接蓝牙设备中...', }) wx.createBLEConnection({ deviceId: targetID, success: function (res) { console.log(res) wx.hideLoading() wx.showToast({ title: '连接成功', icon: 'success', duration: 1000 }) console.log("getplatform") var platform = wx.getSystemInfoSync().platform console.log(platform) if(platform == "android"){ wx.navigateTo({ url: '../device/device?connectedDeviceId=' + targetID +'&name='+name+'&advertisData='+advertisData }) } else{ console.log('ios device') wx.getBLEDeviceServices({ // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接 deviceId: targetID, success: function (res) { console.log("getBLEDeviceServices success") console.log(JSON.stringify(res)) //获取设备特征对象 wx.getBLEDeviceCharacteristics({ deviceId: targetID, serviceId: '0000FEE7-0000-1000-8000-00805F9B34FB', success: function(res) { console.log('../device/device?connectedDeviceId=' + targetID +'&name='+name+'&advertisData='+advertisData) wx.navigateTo({ url: '../device/device?connectedDeviceId=' + targetID +'&name='+name+'&advertisData='+advertisData }) }, fail:function(){ wx.showModal({ title: '温馨提示', content: '获取特征对象失败!', showCancel: false }); quit(obj); } }) }, fail:function(){ wx.showModal({ title: '温馨提示', content: '获取服务失败!', showCancel:false }); quit(obj); } }) } }, fail: function (res) { console.log(res) wx.hideLoading() wx.showModal({ title: '提示', content: '连接失败', showCancel: false }) } }) }, request_success_callback(res){ //http request成功的回调 console.log("http request success callback, data:", res.data) }, request_fail_callback(fail_code){ //http request失败的回调 console.log("http request fail callback,fail_code:", fail_code) }, request_api_test(){ //使用api进行http request测试 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) }, request_test() { //使用wx自带的sdk进行http request测试 wx.request({ url: 'http://www.yykvz.cn',//服务器接口地址 method:'GET',//请求类型 header: { 'content-type': 'application/json' }, success:function(res) { console.log(res) }, fail:function(err) { console.log(err.errno) } }) }, RequestTest: function(){ // 点击网络请求测试按钮事件 console.log("http request测试") // http request测试. this.request_api_test() }, WebsocketConnSuccessEvent:function (res) { console.log("socket连接成功,res:",res) }, WebsocketConnFailEvent:function (res) { console.log("socket连接失败,res:", res) }, WebsocketCloseSuccessEvent:function (res) { console.log("socket连接关闭成功,res:",res) }, WebsocketCloseFailEvent:function (res) { console.log("socket连接关闭失败,res:", res) }, WebsocketConnStart:function () { console.log("启动socket连接") const Host = 'ws://id27149.com:2222/codec' websocket_api.connectSocket( Host, this.WebsocketConnSuccessEvent, this.WebsocketConnFailEvent ) }, WebsocketConnButton:function () { // 点击websocket测试按钮事件 console.log("websocket建立连接按钮事件") this.WebsocketConnStart() }, // onSocketMessageCallback:function(res){ // console.log("收到服务器消息:res", res); // //字符串转为字典. // if(res == "heartbeat"){ // console.log("rx server heartbear") // }else{ // const dict = JSON.parse(res) // if(dict['method'] == "send_data"){ // console.log("将数据通过蓝牙发送至设备,", dict['args']) // } // } // }, WebsocketSendSuccessEvent:function (res) { console.log("socket发送成功,res:",res) }, WebsocketSendFailEvent:function (res) { console.log("socket发送失败,res:", res) }, WebsocketSendButton:function () { // 点击websocket发送按钮事件 console.log("websocket发送按钮事件") websocket_api.SocketTxMsg( JSON.stringify({ "method": "read_did", "args": {'N_TA':18,'did_list':['UDS_MANUFACTURING_DATA']} }), this.WebsocketSendSuccessEvent, this.WebsocketSendFailEvent ) }, SearchClick: function(){ var that=this that.setData({qrcodeMac:""}) that.Search() }, Search: function () { var that = this console.log("search:",that.data.searching) if (!that.data.searching) { //关闭现有的蓝牙连接 wx.closeBluetoothAdapter({ complete: function (res) { console.log(res) //打开蓝牙适配 wx.openBluetoothAdapter({ success: function (res) { console.log(res) wx.getBluetoothAdapterState({ success: function (res) { console.log(res) } }) //开始搜索蓝牙设备 wx.startBluetoothDevicesDiscovery({ allowDuplicatesKey: false, success: function (res) { console.log(res) that.setData({ searching: true, devicesList: [] }) } }) }, fail: function (res) { console.log(res) wx.showModal({ title: '提示', content: '请检查手机蓝牙是否打开', showCancel: false, success: function (res) { that.setData({ searching: false }) } }) } }) } }) } else { wx.stopBluetoothDevicesDiscovery({ success: function (res) { console.log(res) that.setData({ searching: false }) } }) } }, //连接上述mac地址蓝牙按钮事件 ConnectMacBle:function(){ var that = this console.log("开始连接蓝牙,mac地址:", that.data.inputText) if(that.data.inputText.length == 12){ that.Search() setTimeout(function() { //连接文本框中输入的mac地址的蓝牙设备 that.ConnectByMac(that.data.inputText) }, 1500) }else{ console.log("mac addr len error") } }, //扫描二维码按钮事件 ScanQR: function(){ var that=this that.setData({ searching: false, qrcodeMac:"" }) wx.scanCode({ success(res) { that.setData({ qrcodeMac:res.result, }) //扫码成功后,搜索查找指定的蓝牙设备并连接 that.Search() setTimeout(function() { that.ConnectByMac(that.data.qrcodeMac) }, 1500) } }) }, ConnectClick: function (e) { var that = this console.log(e.currentTarget.id) that.ConnectByID(e.currentTarget.id) }, ConnectByMac: function(mac_addr){ var that=this console.log("qrcode:",mac_addr," devicelen:",that.data.devicesList.length) var target_advertis_data = "1300"+mac_addr for (var i = 0; i < that.data.devicesList.length; i++) { if (that.data.devicesList[i].advertisData.length>12){ if (target_advertis_data == that.data.devicesList[i].advertisData) { var targetID=that.data.devicesList[i].deviceId //避免短时间内重复连接 if(that.data.searching == false) return that.setData({ searching: false, qrcodeMac:"" }) that.ConnectByID(targetID) break } } } that.setData({ searching: false, qrcodeMac:"" }) }, onLoad: function (options) { var that = this // 打开小程序后即创建socket连接 this.WebsocketConnStart(); // 设置接收消息回调 // websocket_api.onSocketMessageCallback = this.onSocketMessageCallback; var list_height = ((app.globalData.SystemInfo.windowHeight - 50) * (750 / app.globalData.SystemInfo.windowWidth)) - 110 that.setData({ list_height: list_height }) wx.onBluetoothAdapterStateChange(function (res) { console.log(res) that.setData({ searching: res.discovering }) if (!res.available) { that.setData({ searching: false }) } }) wx.onBluetoothDeviceFound(function (devices) { //剔除重复设备,兼容不同设备API的不同返回值 var isnotexist = true if (devices.deviceId) { if (devices.advertisData) { devices.advertisData = app.buf2hex(devices.advertisData) } else { devices.advertisData = '' } console.log(devices) for (var i = 0; i < that.data.devicesList.length; i++) { if (devices.deviceId == that.data.devicesList[i].deviceId) { isnotexist = false } } if (isnotexist) { that.data.devicesList.push(devices) } } else if (devices.devices) { if (devices.devices[0].advertisData) { devices.devices[0].advertisData = app.buf2hex(devices.devices[0].advertisData) } else { devices.devices[0].advertisData = '' } console.log(devices.devices[0]) for (var i = 0; i < that.data.devicesList.length; i++) { if (devices.devices[0].deviceId == that.data.devicesList[i].deviceId) { isnotexist = false } } if (isnotexist) { that.data.devicesList.push(devices.devices[0]) } } else if (devices[0]) { if (devices[0].advertisData) { devices[0].advertisData = app.buf2hex(devices[0].advertisData) } else { devices[0].advertisData = '' } console.log(devices[0]) for (var i = 0; i < devices_list.length; i++) { if (devices[0].deviceId == that.data.devicesList[i].deviceId) { isnotexist = false } } if (isnotexist) { that.data.devicesList.push(devices[0]) } } that.setData({ devicesList: that.data.devicesList }) }) }, onUnload: function(){ console.log("页面退出时,关闭socket连接") websocket_api.closeSocket(this.WebsocketCloseSuccessEvent, this.WebsocketCloseFailEvent); }, onReady: function () { }, onShow: function () { if(this.data.qrcodeMac=="") { wx.closeBluetoothAdapter({ complete: function (res) { } }) } }, onHide: function () { var that = this that.setData({ devicesList: [] }) if (this.data.searching) { wx.stopBluetoothDevicesDiscovery({ success: function (res) { console.log(res) that.setData({ searching: false }) } }) } } })