求推荐蓝牙低功耗BLE方案?

求推荐蓝牙低功耗BLE方案?,第1张

您这边是要做一个什么应用的,SKYLAB有基于Nordic52系列的BLE蓝牙模块及BLE蓝牙低功耗方案,可以满足物联网智能家居、智慧医疗、智慧工业、智能楼宇等领域范围内的数据传输、智能控制需求,希望能够帮助到您。

SKYLAB BLE蓝牙模块

低于SDK23以下,通过startScan(true)和startScan(false)来实现开启和停止扫描BLE设备
此时运用计时器/线程/Handler等其中一个来处理周期性扫描问题额,这周期性其实就看你怎么延时开/关 扫描

蓝牙适配器接口是基础库版本 110 开始支持。
iOS 微信客户端 656 版本开始支持,Android 客户端暂不支持
蓝牙总共增加了18个api接口。
Api分类
搜索类
连接类
通信类
案例实现
搜索蓝牙设备
/
搜索设备界面
/
Page({
data: {
logs: [],
list:[],
},
onLoad: function () {
consolelog('onLoad')
var that = this;
// const SDKVersion = wxgetSystemInfoSync()SDKVersion || '100'
// const [MAJOR, MINOR, PATCH] = SDKVersionsplit('')map(Number)
// consolelog(SDKVersion);
// consolelog(MAJOR);
// consolelog(MINOR);
// consolelog(PATCH);
// const canIUse = apiName => {
// if (apiName === 'showModalcancel') {
// return MAJOR >= 1 && MINOR >= 1
// }
// return true
// }
// wxshowModal({
// success: function(res) {
// if (canIUse('showModalcancel')) {
// consolelog(rescancel)
// }
// }
// })
//获取适配器
wxopenBluetoothAdapter({
success: function(res){
// success
consolelog("-----success----------");
consolelog(res);
//开始搜索
wxstartBluetoothDevicesDiscovery({
services: [],
success: function(res){
// success
consolelog("-----startBluetoothDevicesDiscovery--success----------");
consolelog(res);
},
fail: function(res) {
// fail
consolelog(res);
},
complete: function(res) {
// complete
consolelog(res);
}
})
},
fail: function(res) {
consolelog("-----fail----------");
// fail
consolelog(res);
},
complete: function(res) {
// complete
consolelog("-----complete----------");
consolelog(res);
}
})
wxgetBluetoothDevices({
success: function(res){
// success
//{devices: Array[11], errMsg: "getBluetoothDevices:ok"}
consolelog("getBluetoothDevices");
consolelog(res);
thatsetData({
list:resdevices
});
consolelog(thatdatalist);
},
fail: function(res) {
// fail
},
complete: function(res) {
// complete
}
})
},
onShow:function(){
},
//点击事件处理
bindViewTap: function(e) {
consolelog(ecurrentTargetdatasettitle);
consolelog(ecurrentTargetdatasetname);
consolelog(ecurrentTargetdatasetadvertisData);
var title = ecurrentTargetdatasettitle;
var name = ecurrentTargetdatasetname;
wxredirectTo({
url: '/conn/conndeviceId='+title+'&name='+name,
success: function(res){
// success
},
fail: function(res) {
// fail
},
complete: function(res) {
// complete
}
})
},
})
连接 获取数据
/
连接设备。获取数据
/
Page({
data: {
motto: 'Hello World',
userInfo: {},
deviceId: '',
name: '',
serviceId: '',
services: [],
cd20: '',
cd01: '',
cd02: '',
cd03: '',
cd04: '',
characteristics20: null,
characteristics01: null,
characteristics02: null,
characteristics03: null,
characteristics04: null,
result,
},
onLoad: function (opt) {
var that = this;
consolelog("onLoad");
consolelog('deviceId=' + optdeviceId);
consolelog('name=' + optname);
thatsetData({ deviceId: optdeviceId });
/
监听设备的连接状态
/
wxonBLEConnectionStateChanged(function (res) {
consolelog(`device ${resdeviceId} state has changed, connected: ${resconnected}`)
})
/
连接设备
/
wxcreateBLEConnection({
deviceId: thatdatadeviceId,
success: function (res) {
// success
consolelog(res);
/
连接成功,后开始获取设备的服务列表
/
wxgetBLEDeviceServices({
// 这里的 deviceId 需要在上面的 getBluetoothDevices中获取
deviceId: thatdatadeviceId,
success: function (res) {
consolelog('device services:', resservices)
thatsetData({ services: resservices });
consolelog('device services:', thatdataservices[1]uuid);
thatsetData({ serviceId: thatdataservices[1]uuid });
consolelog('--------------------------------------');
consolelog('device设备的id:', thatdatadeviceId);
consolelog('device设备的服务id:', thatdataserviceId);
/
延迟3秒,根据服务获取特征
/
setTimeout(function () {
wxgetBLEDeviceCharacteristics({
// 这里的 deviceId 需要在上面的 getBluetoothDevices
deviceId: thatdatadeviceId,
// 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取
serviceId: thatdataserviceId,
success: function (res) {
consolelog('000000000000' + thatdataserviceId);
consolelog('device getBLEDeviceCharacteristics:', rescharacteristics)
for (var i = 0; i < 5; i++) {
if (rescharacteristics[i]uuidindexOf("cd20") != -1) {
thatsetData({
cd20: rescharacteristics[i]uuid,
characteristics20: rescharacteristics[i]
});
}
if (rescharacteristics[i]uuidindexOf("cd01") != -1) {
thatsetData({
cd01: rescharacteristics[i]uuid,
characteristics01: rescharacteristics[i]
});
}
if (rescharacteristics[i]uuidindexOf("cd02") != -1) {
thatsetData({
cd02: rescharacteristics[i]uuid,
characteristics02: rescharacteristics[i]
});
} if (rescharacteristics[i]uuidindexOf("cd03") != -1) {
thatsetData({
cd03: rescharacteristics[i]uuid,
characteristics03: rescharacteristics[i]
});
}
if (rescharacteristics[i]uuidindexOf("cd04") != -1) {
thatsetData({
cd04: rescharacteristics[i]uuid,
characteristics04: rescharacteristics[i]
});
}
}
consolelog('cd01= ' + thatdatacd01 + 'cd02= ' + thatdatacd02 + 'cd03= ' + thatdatacd03 + 'cd04= ' + thatdatacd04 + 'cd20= ' + thatdatacd20);
/
回调获取 设备发过来的数据
/
wxonBLECharacteristicValueChange(function (characteristic) {
consolelog('characteristic value comed:', characteristicvalue)
//{value: ArrayBuffer, deviceId: "D8:00:D2:4F:24:17", serviceId: "ba11f08c-5f14-0b0d-1080-007cbe238851-0x600000460240", characteristicId: "0000cd04-0000-1000-8000-00805f9b34fb-0x60800069fb80"}
/
监听cd04cd04中的结果
/
if (characteristiccharacteristicIdindexOf("cd01") != -1) {
const result = characteristicvalue;
const hex = thatbuf2hex(result);
consolelog(hex);
}
if (characteristiccharacteristicIdindexOf("cd04") != -1) {
const result = characteristicvalue;
const hex = thatbuf2hex(result);
consolelog(hex);
thatsetData({ result: hex });
}
})
/
顺序开发设备特征notifiy
/
wxnotifyBLECharacteristicValueChanged({
deviceId: thatdatadeviceId,
serviceId: thatdataserviceId,
characteristicId: thatdatacd01,
state: true,
success: function (res) {
// success
consolelog('notifyBLECharacteristicValueChanged success', res);
},
fail: function (res) {
// fail
},
complete: function (res) {
// complete
}
})
wxnotifyBLECharacteristicValueChanged({
deviceId: thatdatadeviceId,
serviceId: thatdataserviceId,
characteristicId: thatdatacd02,
state: true,
success: function (res) {
// success
consolelog('notifyBLECharacteristicValueChanged success', res);
},
fail: function (res) {
// fail
},
complete: function (res) {
// complete
}
})
wxnotifyBLECharacteristicValueChanged({
deviceId: thatdatadeviceId,
serviceId: thatdataserviceId,
characteristicId: thatdatacd03,
state: true,
success: function (res) {
// success
consolelog('notifyBLECharacteristicValueChanged success', res);
},
fail: function (res) {
// fail
},
complete: function (res) {
// complete
}
})
wxnotifyBLECharacteristicValueChanged({
// 启用 notify 功能
// 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
deviceId: thatdatadeviceId,
serviceId: thatdataserviceId,
characteristicId: thatdatacd04,
state: true,
success: function (res) {
consolelog('notifyBLECharacteristicValueChanged success', res)
}
})
}, fail: function (res) {
consolelog(res);
}
})
}
, 1500);
}
})
},
fail: function (res) {
// fail
},
complete: function (res) {
// complete
}
})
},
/
发送 数据到设备中
/
bindViewTap: function () {
var that = this;
var hex = 'AA5504B10000B5'
var typedArray = new Uint8Array(hexmatch(/[\da-f]{2}/gi)map(function (h) {
return parseInt(h, 16)
}))
consolelog(typedArray)
consolelog([0xAA, 0x55, 0x04, 0xB1, 0x00, 0x00, 0xB5])
var buffer1 = typedArraybuffer
consolelog(buffer1)
wxwriteBLECharacteristicValue({
deviceId: thatdatadeviceId,
serviceId: thatdataserviceId,
characteristicId: thatdatacd20,
value: buffer1,
success: function (res) {
// success
consolelog("success 指令发送成功");
consolelog(res);
},
fail: function (res) {
// fail
consolelog(res);
},
complete: function (res) {
// complete
}
})
},
/
ArrayBuffer 转换为 Hex
/
buf2hex: function (buffer) { // buffer is an ArrayBuffer
return Arrayprototypemapcall(new Uint8Array(buffer), x => ('00' + xtoString(16))slice(-2))join('');
}
})
效果展示
发送校验指令。获取结果


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/dianzi/13107833.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-30
下一篇 2023-05-30

发表评论

登录后才能评论

评论列表(0条)

保存