// pages/blueconn/blueconn.js var app = getApp() Page({ /** * 页面的初始数据 */ data: { list: [], services: [], serviceId: 0, writeCharacter: false, readCharacter: false, notifyCharacter: false, isScanning:false }, //搜索设备 startSearch: function () { var that = this wx.openBluetoothAdapter({ success: function (res) { wx.getBluetoothAdapterState({ success: function (res) { console.log('openBluetoothAdapter success', res) if (res.available) { if (res.discovering) { wx.stopBluetoothDevicesDiscovery({ success: function (res) { console.log(res) } }) }else{ // that.startBluetoothDevicesDiscovery() that.getBluetoothDevices() } // that.checkPemission() } else { wx.showModal({ title: '提示', content: '本机蓝牙不可用', showCancel: false }) } }, }) }, fail: function () { // if (res.errCode === 10001) { // wx.onBluetoothAdapterStateChange(function (res) { // console.log('onBluetoothAdapterStateChange', res) // if (res.available) { // this.startBluetoothDevicesDiscovery() // } // }) // } wx.showModal({ title: '提示', content: '蓝牙初始化失败,请到设置打开蓝牙', showCancel: false }) } }) }, checkPemission: function () { //android 6.0以上需授权地理位置权限 var that = this var platform = app.BLEInformation.platform if (platform == "ios") { app.globalData.platform = "ios" that.getBluetoothDevices() } else if (platform == "android") { app.globalData.platform = "android" console.log(app.getSystem().substring(app.getSystem().length - (app.getSystem().length - 8), app.getSystem().length - (app.getSystem().length - 8) + 1)) if (app.getSystem().substring(app.getSystem().length - (app.getSystem().length - 8), app.getSystem().length - (app.getSystem().length - 8) + 1) > 5) { wx.getSetting({ success: function (res) { console.log(res) if (!res.authSetting['scope.userLocation']) { wx.authorize({ scope: 'scope.userLocation', complete: function (res) { that.getBluetoothDevices() } }) } else { that.getBluetoothDevices() } } }) } } }, getBluetoothDevices: function () { //获取蓝牙设备信息 var that = this console.log("start search") wx.showLoading({ title: '正在加载', icon: 'loading', }) that.setData({ isScanning:true }) wx.startBluetoothDevicesDiscovery({ success: function (res) { console.log(res) setTimeout(function () { wx.getBluetoothDevices({ success: function (res) { var devices = [] var num = 0 for (var i = 0; i < res.devices.length; ++i) { if (res.devices[i].name != "未知设备") { devices[num] = res.devices[i] num++ } } that.setData({ list: devices, isScanning:false }) wx.hideLoading() wx.stopPullDownRefresh() wx.stopBluetoothDevicesDiscovery({ success: function (res) { console.log("停止搜索蓝牙") } }) }, }) }, 5000) }, }) }, bindViewTap: function (e) { var that = this wx.stopBluetoothDevicesDiscovery({ success: function (res) { console.log(res) }, }) that.setData({ serviceId: 0, writeCharacter: false, readCharacter: false, notifyCharacter: false }) console.log(e.currentTarget.dataset.title) wx.showLoading({ title: '正在连接', }) wx.createBLEConnection({ deviceId: e.currentTarget.dataset.title, success: function (res) { console.log(res) app.BLEInformation.deviceId = e.currentTarget.dataset.title that.getSeviceId() }, fail: function (e) { wx.showModal({ title: '提示', content: '连接失败', showCancel: false }) console.log(e) wx.hideLoading() }, complete: function (e) { console.log(e) } }) }, getSeviceId: function () { var that = this var platform = app.BLEInformation.platform console.log(app.BLEInformation.deviceId) wx.getBLEDeviceServices({ deviceId: app.BLEInformation.deviceId, success: function (res) { console.log(res) // var realId = '' // if (platform == 'android') { // // for(var i=0;i