初始化蓝牙 uni.openBluetoothAdapter(OBJECT)
开始搜索蓝牙设备 uni.startBluetoothDevicesDiscovery(OBJECT)
发现外围设备 uni.onBluetoothDeviceFound(CALLBACK)
停止搜寻附近的蓝牙外围设备 uni.stopBluetoothDevicesDiscovery(OBJECT)
连接低功耗蓝牙设备 uni.createBLEConnection(OBJECT)
获取蓝牙设备所有服务 uni.getBLEDeviceServices(OBJECT) //如果有uuid可以省略
获取蓝牙特征 uni.getBLEDeviceCharacteristics(OBJECT)//如果有uuid可以省略
启用蓝牙设备特征值变化时的 notify 功能 uni.notifyBLECharacteristicValueChange(OBJECT)
监听低功耗蓝牙设备的特征值变化 uni.onBLECharacteristicValueChange(CALLBACK)
一般需要三个UUID:
service UUID 服务
write UUID 写入
notify UUID 通知
如果开发商没有给可以使用BLE调试宝APP进行尝试获取
image.png
image.png
image.png
<template>
<view class="content">
<text class="text-style">连接状态</text>
<button @tap="openBle" class="margin-top">打开蓝牙</button>
<button @tap="searchDevice" class="margin-top">搜索设备</button>
<button @tap="getInfo" class="margin-top">获取设备信息</button>
</view>
</template>
<script>
export default {
data() {
return {
isOPenBle:false,
deviceId: '',
serviceId:'14839ac4-7d7e-111c-9a42-167340cf2339',//service UUID
notifycharacteristicId:'0732224a-a8e7-4b1a-a6b1-cd5243059a57', //notify UUID
writecharacteristicId:'8b1234e7-eb0b-49b0-bbe9-9aee0a26e1a3' //write UUID
}
},
onLoad() {
},
methods: {
openBle(){
uni.openBluetoothAdapter({
success(data) {
console.log("蓝牙打开了",data);
},
fail: (err) => {
console.log("蓝牙未打开",err);
}
})
},
searchDevice(){
let _this = this;
uni.startBluetoothDevicesDiscovery({
success(data) {
_this.onBluetoothDeviceFound();
},
fail(err) {
console.log("fail",err);
}
})
},
onBluetoothDeviceFound(){
let _this = this;
uni.onBluetoothDeviceFound(function(res){
console.log("```````",res);
let devices = res.devices;
for (var i = 0; i < devices.length; i++) {
console.log(devices[i]);
const device = devices[i];
const deviceName = device.name;
if(deviceName !== null && deviceName === 'DuoEK 0018'){
_this.deviceId = device.deviceId;
console.log(_this.deviceId + '----------停止搜索----------');
_this.stopBluetoothDevicesDiscovery()
break
}
}
})
},
stopBluetoothDevicesDiscovery(){
let _this = this;
uni.stopBluetoothDevicesDiscovery({
success() {
console.log("停止搜索ble成功");
_this.createConnectBle()
},
fail() {
console.log("停止搜索ble失败");
}
})
},
createConnectBle(){
let _this = this;
uni.createBLEConnection({
deviceId: this.deviceId,
success(res) {
console.log('连接设备成功',res);
_this.notifyBLECharacteristicValueChange();
},
fail(err) {
console.log('连接设备失败',err);
}
})
},
notifyBLECharacteristicValueChange(){
let _this = this;
uni.notifyBLECharacteristicValueChange({
deviceId:_this.deviceId,
serviceId:_this.serviceId,
characteristicId:_this.notifycharacteristicId,
state:true,
success(res) {
console.log('notify开启通知成功');
_this.onBLECharacteristicValueChange();
},
fail(err) {
console.log('notify开启通知失败');
}
})
},
onBLECharacteristicValueChange(){
let _this = this;
uni.onBLECharacteristicValueChange(function(res){
console.log("ble广播数据",res.value);
console.log(_this.ab2hex(res.value));
});
},
ab2hex(buffer) {
const hexArr = Array.prototype.map.call(
new Uint8Array(buffer),
function(bit) {
return ('00' + bit.toString(16)).slice(-2)
}
)
return hexArr.join('')
},
getInfo(){
const buffer = new ArrayBuffer(8)
const dataView = new DataView(buffer)
dataView.setUint8(0, 0xA5)
dataView.setUint8(1, 0x01)
dataView.setUint8(2, ~0x1)
dataView.setUint8(3, 0)
dataView.setUint8(4, 0)
dataView.setUint8(5, 0)
dataView.setUint8(6, 0)
dataView.setUint8(7, 0xF3)
for (let i = 0; i < dataView.byteLength; i++) {
console.log("0x" + dataView.getUint8(i).toString(16))
}
this.writeBLECharacteristicValue(buffer)
},
showToastMe(data){
uni.showToast({
icon:'none',
title:data
})
},
writeBLECharacteristicValue(cmd){
let _this = this;
uni.writeBLECharacteristicValue({
deviceId:_this.deviceId,
serviceId:_this.serviceId,
characteristicId:_this.writecharacteristicId,
value:cmd,
success(res) {
console.log('指令写入成功');
},
fail(err) {
console.log('指令写入失败',err);
}
})
}
}
}
</script>
<style>
.text-style{
margin-top: 20rpx;
font-size: 40rpx;
}
.margin-top{
margin-top: 20rpx;
}
.content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
</style>
网友评论