- 当前 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,
success(res) {
console.log('notify 开启成功', res.errMsg)
setTimeout(() => {
_this.writeDevice(deviceId, serviceId, _this.data.characteristicId)
}, 2000)
},
fail(err) {
console.log('openNotify err: ', err)
}
})
},
/**
* @description 向设备写入数据
*/
writeDevice(deviceId, serviceId, characteristicId) {
let _this = this
// 向蓝牙设备发送一个0x00的16进制数据
let senddata = 'test'
let buffer = new ArrayBuffer(senddata.length)
let dataView = new DataView(buffer)
for (var i = 0; i < senddata.length; i++) {
dataView.setUint8(i, senddata.charAt(i).charCodeAt())
}
// let buffer = new ArrayBuffer(2)
// let dataView = new DataView(buffer)
// dataView.setUint16(0, 3)
wx.writeBLECharacteristicValue({
deviceId,
serviceId,
characteristicId,
value: buffer,
success(res) {
console.log('写入数据成功', res.errMsg)
_this.readDevice(deviceId, serviceId, _this.data.characteristicId)
},
fail(err) {
console.log('err', err)
},
complete() {
_this.closeConnection(deviceId)
_this.closeBlue()
}
})
},
readDevice(deviceId, serviceId, characteristicId) {
let _this = this
// 必须在这里的回调才能获取
wx.onBLECharacteristicValueChange(function (characteristic) {
console.log('characteristic value comed:', characteristic)
})
wx.readBLECharacteristicValue({
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
deviceId,
// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
serviceId,
// 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
characteristicId,
success(res) {
console.log('readBLECharacteristicValue:', res.errCode)
}
})
},
closeConnection(deviceId) {
wx.closeBLEConnection({
deviceId,
success(res) {
console.log('关闭设备连接:', res)
}
})
}
})
- 提供一个最简复现 Demo
你换的哪个打印机呀,我现在买的用你这个都成功就是不打印,麻烦给个打印机型号?谢谢
你解决了吗
解决了,我换了一个打印机, 要 write,read,还有notify 都可以才行
https://developers.weixin.qq.com/community/develop/doc/00020a86428670e0d9d75ae5051c06?jumpto=reply&parent_commentid=000804e49a0768ef81f7b2530568&commentid=00020cdca340a82f82f7d4240564
官方的人呢??