const app = getApp() app.globalData.pageid=1; Page({ data: { searching: false, connectedDeviceId: '', services: {}, characteristics: {}, devicesList: [], PageAddr: ["../device/device", "../dropfoot/dropfoot"], }, checkUserHavePermission() { getUserPermission('scope.bluetooth').then(() => { // 用户已授权获取地理位置 this.getUserLocation(()=>{ // 获取位置 进行其他操作 }) }).catch(() => { // 用户未授权/拒绝获取地理位置 // 需要提示用户手动打开设置去授权 this.openSettingPop(); // 打开提示弹窗 }) }, Search:function getUserPermission(permissionName) { return new Promise((resolve, reject) => { wx.getSetting({ success(res) { if (res.authSetting[permissionName]) { resolve(true); } else if (res.authSetting[permissionName] === false) { reject(false); } else { wx.authorize({ scope: permissionName, success() { resolve(true); }, fail() { reject(false); } }); } }, fail() { reject(false); } }); }); }, search2: function () { }, back: function () { wx.navigateBack() }, home: function () { }, Search: function () { var that = this if (!that.data.searching) { // 关闭蓝牙模块 wx.closeBluetoothAdapter({ // 接口调用结束的回调函数(调用成功、失败都会执行) complete: function (res) { console.log(res) // 初始化蓝牙模块。iOS 上开启主机/从机(外围设备)模式时需分别调用一次,并指定对应的 mode。 wx.openBluetoothAdapter({ success: function (res) { console.log(res) // 获取本机蓝牙适配器状态 wx.getBluetoothAdapterState({ success: function (res) { console.log(res) } }) // 开始搜寻附近的蓝牙外围设备。 //此操作比较耗费系统资源,请在搜索到需要的设备后及时调用 wx.stopBluetoothDevicesDiscovery 停止搜索 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 }) } }) } }, Connect: function (e) { var that = this var advertisData, name console.log(e.currentTarget.id) for (var i = 0; i < that.data.devicesList.length; i++) { if (e.currentTarget.id == that.data.devicesList[i].deviceId) { name = that.data.devicesList[i].localName advertisData = that.data.devicesList[i].advertisData } } wx.stopBluetoothDevicesDiscovery({ success: function (res) { console.log(res) that.setData({ searching: false }) } }) if (name != 'WH-BLE 103') { wx.showModal({ title: '提示', content: '请选择正确设备连接', showCancel: false, success: function (res) { that.setData({ searching: false }) } }) console.log('设备名称错误') } else { //显示loading提示框 wx.showLoading({ title: '连接蓝牙设备中...', }) wx.createBLEConnection({ deviceId: e.currentTarget.id, success: function (res) { console.log("createBLEConnection success!") // //ble获取蓝牙服务{获取特征值列表{开启端口监听{定时心跳发送{允许发送心跳{发送成功consloe显示}}}}} wx.getBLEDeviceServices({ deviceId: e.currentTarget.id, /*蓝牙设备id */ success: function (res) { /**接口调用成功的回调函数 */ console.log("getBLEDeviceServices success!") that.setData({ services: res.services }) wx.getBLEDeviceCharacteristics({ /**获取蓝牙低功耗设备某个服务中所有特征 */ deviceId: e.currentTarget.id, /**蓝牙设备id */ serviceId: res.services[0].uuid, /**蓝牙服务uuid */ success: function (res) { /**接口调用成功的回调函数 */ console.log("getBLEDeviceCharacteristics success!") that.setData({ characteristics: res.characteristics }) /**启用蓝牙低功耗设备特征值变化时的notify功能 */ wx.notifyBLECharacteristicValueChange({ state: true, //是否启用notify deviceId: e.currentTarget.id, //蓝牙设备id serviceId: that.data.services[0].uuid, //蓝牙特征对应服务的uuid characteristicId: that.data.characteristics[0].uuid, //蓝牙特征的uuid success: function (res) //接口调用成功的回调函数 { console.log('启用notify成功') //*****在这跳转到device页面 url:路径?参数 参数键与参数值用 = 相连,不同参数用 & 分隔************ wx.navigateTo({ url: that.data.PageAddr[app.globalData.pageid] + '?' + 'connectedDeviceId=' + e.currentTarget.id + '&name=' + name +'&serviceId='+that.data.services[0].uuid, }) //隐藏loading提示框 wx.hideLoading() } }) } }) } }) }, fail: function (res) { console.log(res) wx.hideLoading() wx.showModal({ title: '提示', content: '连接失败', showCancel: false }) } }) } }, onLoad: function (options) { var that = this var list_height = ((app.globalData.SystemInfo.windowHeight - 50) * (750 / app.globalData.SystemInfo.windowWidth)) - 60 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.devices[0].name != 'WH-BLE 103'){ isnotexist = false } else 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 }) }) }, onReady: function () { }, onShow: function () { }, onHide: function () { var that = this that.setData({ devicesList: [] }) if (this.data.searching) { wx.stopBluetoothDevicesDiscovery({ success: function (res) { console.log(res) that.setData({ searching: false }) } }) } }, }) wx.getSetting({ success(res) { console.log(res.authSetting) //判断是否有'scope.bluetooth'属性 if (res.authSetting.hasOwnProperty('scope.bluetooth')) { //'scope.bluetooth'属性存在,且为false if (!res.authSetting['scope.bluetooth']) { //弹窗授权 wx.openSetting({ success(res) { console.log(res.authSetting) } }) } } else //'scope.bluetooth'属性不存在,需要授权 wx.authorize({ scope: 'scope.bluetooth', success() { // 用户已经同意小程序使用手机蓝牙功能,后续调用 蓝牙 接口不会弹窗询问 console.log(res.authSetting) } }) } })