最近老板总想开发微信小程序,然后我们公司又是倾向于智能硬件,所以直接还是上微信小程序BLE的开发demo。其实如果有过Android或者iOSble开发经验的人,开发微信小程序ble还是相当简单的,蓝牙的初始化、开启蓝牙、判断Android开启定位、开始扫描、判断连接、获取服务、获取特征值、订阅通知、写入数据等,都包含在一个按钮事件中,从上到下一气呵成。
完成的方法代码如下:
startBle: function() {
wx.openBluetoothAdapter({ success: function(res) {
console.log('ble初始化成功' + res)
if (app.getPlatform() == 'android' && versionCompare(app.getsystem(), '10.0')) {
wx.showToast({
title: '安卓6.0以上需要开启GPS服务', duration: 2000
})
} else if (versionCompare('6.5.6', app.getVersion())) {
wx.showModal({
title: '提示',
content: '当前微信版本过低,请更新至最新版本', showCancel: false
}) }
//开始搜索蓝牙 wx.startBluetoothDevicesDiscovery({
success: function(res) { console.log('search', res)
},
fail: function(res) {
console.log(res) wx.showModal({
title: '提示',
content: '请检查手机蓝牙是否打开',
showCancel: false, success: function(res) {
} })
} })
//发现设备 wx.getBluetoothDevices({
success: function(res) {
console.log('发现设备', res)
} })
//监听发现设备 wx.onBluetoothDeviceFound(function(devices) {
console.log('发现设备1:', devices.devices) for (let i = 0; i < devices.devices.length; i++) {
//检索指定设备
if (devices.devices[i].name == 'qixiang_TS') {
let deviceID = devices.devices[i].deviceId; console.log("deviceID" + deviceID) //蓝牙连接成功 停止搜索蓝牙 wx.stopBluetoothDevicesDiscovery({
success: function(res) { console.log("停止蓝牙搜索") console.log(res)
} })
wx.createBLEConnection({ deviceId: deviceID, success: function(res) {
console.log('蓝牙设备连接成功')
wx.getBLEDeviceServices({
deviceId: devices.devices[i].deviceId, success: function(res) {
var service = res.services[1]
var serviceId = service.uuid.substring(4, 8) if (serviceId == 'FFF0') {
console.log('蓝牙设备Service = fff0') console.log(devices.devices[i].deviceId) console.log(service.uuid) wx.getBLEDeviceCharacteristics({
deviceId: devices.devices[i].deviceId, serviceId: service.uuid,
success: function(res) {
if (res.characteristics[0].uuid == "0000FFF1-0000-1000-8000-00805F9B34FB") {//通知通道的
uuid
console.log('deviceID:', deviceID); console.log('service.uuid:', service.uuid); console.log('res.characteristics[0].uuid:',
res.characteristics[0].uuid);
wx.notifyBLECharacteristicValueChange({//订阅通知 state: true, // 启用 notify 功能
deviceId: deviceID, serviceId: service.uuid, characteristicId:
res.characteristics[0].uuid,
success: function(res) {
console.log('订阅notify成功')
},
fail: function(res) {
console.log('订阅notify失败')
},
complete: function(res) {
console.log('订阅notify完成') }
})
/**
* 回调获取 设备发过来的数据 */
wx.onBLECharacteristicValueChange(function(characteristic) {
console.log('characteristic value comed:', characteristic.value)
/**
* 监听
000FFF1-0000-1000-8000-00805F9B34FB中的结果 */
}) }
if (res.characteristics[1].uuid == "0000FFF2-0000-1000-8000-00805F9B34FB") {//写通道
var hex = '063838D63B0000';
var typedArray = new Uint8Array(hex.match(/[\da-f]{2}/gi).map(function(h) {
return parseInt(h, 16) }))
console.log(typedArray)
console.log([0xAA, 0x55, 0x04, 0xB1, 0x00, 0x00, 0xB5])
var buffer1 = typedArray.buffer console.log(buffer1) wx.writeBLECharacteristicValue({//写方法
deviceId: deviceID, serviceId: service.uuid, characteristicId:
res.characteristics[1].uuid,
value: buffer1,
success: function(res) {//写成功 console.log(res)
console.log('writeBLECharacteristicValue success', res.errMsg);
},
fail: function (res) {//写成功
console.log(res) },
complete: function (res) {//写完成 console.log(res)
} })
} },
fail: function() { console.log('获取characteristic失败')
} })
}
},
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()
} })
} }
}) }
}) }
哇咔咔,就这么愉快的完成了,如遇到其他问题可以留言交流。蓝牙问题都可以交流。