- 当前 Bug 的表现(可附上截图)
代码:
//index.js
//获取应用实例
const app = getApp()
Page({
data: {
userInfo: {},
hasUserInfo: false,
canIUse: wx.canIUse('button.open-type.getUserInfo'),
devices: [],
deviceId: '',
services: [],
serviceId: '',
characteristicId: '',
characteristicIdNotify: '',
characteristicIdRead: '',
},
onLoad() {
// wx.showModal({
// title: '提示',
// content: '请检查手机蓝牙是否打开',
// success: function (res) {
// that.setData({
// isbluetoothready: false,
// searchingstatus: false
// })
// }
// })
},
/**
* @description 开启蓝牙适配器
*/
openBlue() {
let _this = this
wx.openBluetoothAdapter({
success(res) {
console.log('开启适配器:', res)
// 搜索蓝牙设备
_this.discoverDevices()
}
})
},
closeBlue() {
wx.closeBluetoothAdapter({
success(res) {
console.log('关闭适配器: ', res)
}
})
},
/**
* @description 搜索蓝牙设备
*/
discoverDevices() {
let _this = this
wx.startBluetoothDevicesDiscovery({
success(res) {
console.log('搜索完成', res)
setTimeout(() => {
_this.getDevices()
setTimeout(() => {
_this.foundDevices()
}, 500)
}, 500)
}
})
},
/**
* @description 搜索新的蓝牙设备
*/
foundDevices() {
let _this = this
wx.onBluetoothDeviceFound(function (devices) {
console.log('检测到新设备:', devices)
// 检测到新设备后,更新设备列表,供选择
_this.data.devices.push(devices.devices[0])
_this.setData({
devices: _this.data.devices
})
})
},
/**
* @description 获取可连接的蓝牙设备
*/
getDevices() {
let _this = this
wx.getBluetoothDevices({
success: function (res) {
if (res.devices[0]) {
console.log('获取设备:', res)
_this.setData({
devices: res.devices
})
}
}
})
},
chooseDevice(e) {
let _this = this
// 储存 deviceId
_this.data.deviceId = e.currentTarget.dataset.deviceid
// 连接设备
_this.createConnect(_this.data.deviceId)
// console.log('_this.data.deviceId: ',_this.data.deviceId)
},
createConnect(deviceId) {
let _this = this
wx.createBLEConnection({
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
deviceId,
success(res) {
// 连接成功
console.log('连接设备成功:', res)
// 获取 serviceId
_this.getServices(deviceId)
// 监听连接状态
_this.onConnectionStatus()
// 停止搜索
_this.stopDiscoverDevices()
},
fail(err) {
console.warn('err :', err);
}
})
},
onConnectionStatus() {
wx.onBLEConnectionStateChange(function (res) {
// 该方法回调中可以用于处理连接意外断开等异常情况
console.log(`设备 ${res.deviceId} 连接状态变化, 连接状态: ${res.connected}`)
})
},
/**
* @description 获取已连接的蓝牙设备
*/
getConnectDevices(services) {
wx.getConnectedBluetoothDevices({
services,
success(res) {
console.log('已连接的设备:' + JSON.stringify(res))
},
fail(err) {
console.log('err: ', err.errMsg)
}
})
},
/**
* @description 停止搜索蓝牙设备
*/
stopDiscoverDevices() {
wx.stopBluetoothDevicesDiscovery({
success(res) {
console.log('成功链接设备,停止搜索。')
}
})
},
/**
* @description 获得 services & serviceId
*/
getServices(deviceId) {
let _this = this
wx.getBLEDeviceServices({
deviceId,
success(res) {
console.log('device services:', res.services)
_this.getConnectDevices([res.services[2].uuid])
// 获取 serviceId
_this.setData({
serviceId: res.services[2].uuid
})
console.log('_this.serviceId :', _this.data.serviceId);
_this.getCharacteristicsId(_this.data.deviceId, _this.data.serviceId)
}
})
},
/**
* @description 获得 CharacteristicsId
*/
getCharacteristicsId(deviceId, serviceId) {
let _this = this
wx.getBLEDeviceCharacteristics({
deviceId,
serviceId,
success(res) {
console.log('Characteristics:', res.characteristics)
// 设置 characteristicId、characteristicIdNotify
_this.setData({
characteristicIdNotify: res.characteristics[0].uuid,
characteristicId: res.characteristics[0].uuid,
})
_this.openNotify(deviceId, serviceId, _this.data.characteristicIdNotify)
console.log('CharacteristicsId:', res.characteristics[0].uuid)
}
})
},
/**
* @description 开启 notify
*/
openNotify(deviceId, serviceId, characteristicId) {
let _this = this
wx.notifyBLECharacteristicValueChange({
state: true,
deviceId,
serviceId,
characteristicId,