终于实现了蓝牙的功能,也找到了合适的硬件,记录一下自己需要注意和总结的地方
具体的搜索、连接、断开、消息传输都已经实现了,作为项目的一个页面完成了
蓝牙部分代码地址
wx.openBluetoothAdapter({
success: function (res) {
console.log('初始化蓝牙适配器成功' + JSON.stringify(res))
that.msg = '初始化蓝牙适配器成功'
wx.showModal({
title: '蓝牙适配情况',
content: '初始化蓝牙适配器成功'
})
},
fail: function () {
that.msg = '初始化蓝牙适配器失败'
wx.showModal({
title: '蓝牙适配情况',
content: '蓝牙适配失败,请检查手机蓝牙和定位功能是否打开'
})
},
complete: function () {
console.log('初始化蓝牙适配器完成')
}
})
wx.createBLEConnection({
deviceId: deviceId,
success: function (res) {
console.log('蓝牙设备连接成功')
wx.hideLoading()
wx.getBLEDeviceServices({
deviceId: deviceId,
success: function (res) {
that.deviceService = res.services
for (var t = 0; t < that.deviceService.length; t++) {
var service = that.deviceService[t]
var serviceId = service.uuid.substring(4, 8)
if (serviceId === 'FFE0') { //‘FFE0’为设备定义的读写UUID
that.serviceId = service.uuid
}
}
that.nowDevice = !that.nowDevice
that.nowService = !that.nowService
console.log('获取蓝牙设备Service' + res.errMsg)
},
fail: function (res) {
wx.showModal({
title: '设备Service信息',
content: '蓝牙设备连接成功' + '\n' + '设备信息获取错误' + res.errMsg
})
}
})
},
fail: function (res) {
console.log('蓝牙设备连接失败,请稍后重试')
wx.hideLoading()
wx.showModal({
title: '提示',
content: '蓝牙设备连接失败,请稍后重试',
duration: 2000
})
},
complete: function () {
console.log('蓝牙设备连接完成')
wx.hideLoading()
}
})
wx.getBLEDeviceCharacteristics({
deviceId: that.deviceId,
serviceId: that.serviceId,
success: function (res) {
console.log(res.characteristics)
that.deviceCharacteristics = res.characteristics
for (var i = 0; i < that.deviceCharacteristics.length; i++) {
that.characteristic = that.deviceCharacteristics[i]
that.characteristicProperties = that.characteristic.properties
if (that.characteristicProperties.notify === true) {
that.characteristicId = that.characteristic.uuid
wx.notifyBLECharacteristicValueChange({
state: true, // 启用 notify 功能
deviceId: that.deviceId,
serviceId: that.serviceId,
characteristicId: that.characteristicId,
success: function (res) {
console.log('开启notify成功' + that.characteristic.uuid)
for (let i = 0; i < dataView.byteLength; i++) {
var writeData = '0x' + dataView.getUint8(i).toString(16)
that.writeDatas = that.writeDatas + '\n' + writeData
}
wx.writeBLECharacteristicValue({
deviceId: that.deviceId,
serviceId: that.serviceId,
characteristicId: that.characteristicId,
value: dataBuffer,
success: function (res) {
console.log('发送的数据:' + that.writeDatas)
console.log('message发送成功')
wx.showModal({
title: '数据发送成功',
content: that.writeDatas
})
wx.readBLECharacteristicValue({
deviceId: that.deviceId,
serviceId: that.serviceId,
characteristicId: that.characteristicId,
success: function (res) {
console.log('读取数据成功')
}
})
},
fail: function (res) {
// fail
console.log('message发送失败' + that.characteristicIdw)
wx.showToast({
title: '数据发送失败,请稍后重试',
icon: 'none'
})
},
complete: function (res) {
// fail
console.log('message发送完成')
}
})
},
fail: function () {
console.log('开启notify失败' + that.characteristicId)
}
})
// that.writeMessage(that.deviceId, that.serviceId, that.characteristicIdw, that.characteristicIdr, that.characteristicIdn)
}
}
},
fail: function () {
console.log('获取characteristic失败')
}
})
蓝牙页面启动流程
let data = that.sendData.split(',')
let dataBuffer = new ArrayBuffer(data.length)
let dataView = new DataView(dataBuffer)
for (let j = 0; j < data.length; j++) {
dataView.setUint8(j, '0x' + data[j])
}
var writeData = '0x' + dataView.getUint8(i).toString(16)
ab2hex: function (buffer) {
var hexArr = Array.prototype.map.call(
new Uint8Array(buffer), function (bit) {
return ('00' + bit.toString(16)).slice(-2)
}
)
return hexArr.join('')
}