| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401 |
- const app = getApp()
- const request_api = require('../../utils/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()
- },
- 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();
- 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(){
- },
- 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
- })
- }
- })
- }
- }
- })
|