uni-app 蓝牙传输
<p>最近在做一个项目,要求app通过蓝牙连接设备并将报文传输至设备中,在这个过程踩过了几个坑,总结如下:</p><p>根据uni-app官网API主要涉及到“蓝牙”和“低功耗蓝牙”两个部分。</p>
<p>主要步骤:</p>
<h2>步骤1:初始化蓝牙模块 openBluetoothAdapter</h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">$openBluetoothAdapter(){
uni.openBluetoothAdapter({
success: (res) </span>=><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">初始化成功,搜索设备</span>
console.log(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">openBluetoothAdapter success</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">, res);
uni.showLoading({
title: </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">搜索设备中</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
});
setTimeout(()</span>=><span style="color: rgba(0, 0, 0, 1)">{
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.baseList=[];<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">每次扫码清空设备列表,不然会导致重复</span>
<span style="color: rgba(0, 0, 255, 1)">this</span>.$startBluetoothDevicesDiscovery();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">扫码蓝牙设备</span>
},<span style="color: rgba(128, 0, 128, 1)">100</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">定时关闭搜索设备</span>
setTimeout(()=><span style="color: rgba(0, 0, 0, 1)">{
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.$stopBluetoothDevicesDiscovery();
uni.hideLoading();
},</span><span style="color: rgba(128, 0, 128, 1)">10</span>*<span style="color: rgba(128, 0, 128, 1)">1000</span><span style="color: rgba(0, 0, 0, 1)">);
},
fail: (res) </span>=><span style="color: rgba(0, 0, 0, 1)"> {
uni.showToast({
title: </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">请打开蓝牙</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
duration: </span><span style="color: rgba(128, 0, 128, 1)">1000</span><span style="color: rgba(0, 0, 0, 1)">
});
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (res.errCode === <span style="color: rgba(128, 0, 128, 1)">10001</span><span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">监听蓝牙适配器状态变化事件</span>
<span style="color: rgba(0, 0, 0, 1)"> uni.onBluetoothAdapterStateChange(function(res){
console.log(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">onBluetoothAdapterStateChange</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">, res);
</span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (res.available) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">开始扫描</span>
<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.$startBluetoothDevicesDiscovery()
}
})
}
}
})
},</span></pre>
</div>
<h2>步骤2:搜索附近的蓝牙外围设备 startBluetoothDevicesDiscovery</h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2.搜索附近的蓝牙外围设备</span>
<span style="color: rgba(0, 0, 0, 1)"> $startBluetoothDevicesDiscovery(){
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">._discoveryStarted) {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">;
}
</span><span style="color: rgba(0, 0, 255, 1)">this</span>._discoveryStarted = <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">开始搜寻附近的蓝牙外围设备</span>
<span style="color: rgba(0, 0, 0, 1)"> uni.startBluetoothDevicesDiscovery({
allowDuplicatesKey: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
success: (res) </span>=><span style="color: rgba(0, 0, 0, 1)"> {
console.log(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">startBluetoothDevicesDiscovery success</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">, res);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">监听寻找到新设备的事件</span>
<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.$onBluetoothDeviceFound()
},
fail:err</span>=><span style="color: rgba(0, 0, 0, 1)">{
console.error(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">startBluetoothDevicesDiscoveryErr</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,err);
uni.showToast({
icon:</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">none</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
duration:</span><span style="color: rgba(128, 0, 128, 1)">2000</span><span style="color: rgba(0, 0, 0, 1)">,
title:</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">请检查蓝牙状态</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
});
}
})
},</span></pre>
</div>
<h2>步骤3:监听寻找到新设备 onBluetoothDeviceFound</h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">$onBluetoothDeviceFound(){
let that </span>=<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">监听寻找到新设备的事件</span>
<span style="color: rgba(0, 0, 0, 1)"> uni.onBluetoothDeviceFound(function(res){
res.devices.forEach(device </span>=><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (!device.name && !<span style="color: rgba(0, 0, 0, 1)">device.localName) {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">;
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">添加蓝牙设备列表</span>
let oneBluetooth=<span style="color: rgba(0, 0, 0, 1)">{
deviceId: device.deviceId,
name: device.name,
RSSI:device.RSSI,
localName:device.localName
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">判断是否存在</span>
<span style="color: rgba(0, 0, 255, 1)">if</span>(that.bluetoothIndex.indexOf(device.deviceId) ==-<span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">){
that.baseList.push(oneBluetooth);
that.bluetoothIndex.push(device.deviceId);
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果名字相同连接设备
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">if(device.name == devicename){
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">$createBLEConnection(device.deviceId);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">}</span>
<span style="color: rgba(0, 0, 0, 1)"> })
})
},</span></pre>
</div>
<h2>步骤4:创建连接蓝牙事件 createBLEConnection</h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">$createBLEConnection(deviceId){
let that</span>=<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1)判断设备是否处于连接状态</span>
<span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(that.Connecting){
uni.showToast({
icon:</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">none</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
duration:</span><span style="color: rgba(128, 0, 128, 1)">2000</span><span style="color: rgba(0, 0, 0, 1)">,
title:</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">设备已连接</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
});
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2)创建连接</span>
<span style="color: rgba(0, 0, 0, 1)"> uni.showLoading({
title: </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">设备连接中</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
mask:</span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
});
uni.createBLEConnection({
deviceId:deviceId,
success: (res) </span>=><span style="color: rgba(0, 0, 0, 1)"> {
that._deviceId </span>=<span style="color: rgba(0, 0, 0, 1)"> deviceId;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">不延迟造成获取不到服务!!!!,我走过的坑!!!</span>
<span style="color: rgba(0, 0, 0, 1)"> setTimeout(function() {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取设备的蓝牙服务</span>
<span style="color: rgba(0, 0, 0, 1)"> that.$getBLEDeviceServices(deviceId);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">关闭等待提示</span>
setTimeout(function () {uni.hideLoading();}, <span style="color: rgba(128, 0, 128, 1)">100</span><span style="color: rgba(0, 0, 0, 1)">);
}, </span><span style="color: rgba(128, 0, 128, 1)">7000</span><span style="color: rgba(0, 0, 0, 1)">);
},
fail: (err) </span>=><span style="color: rgba(0, 0, 0, 1)">{
console.log(err);
}
});
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">3)设置MTU,否则传输报文不全,默认是23bytes,但是蓝牙本身是需要3bytes,故而只能传输20bytes</span>
setTimeout(()=><span style="color: rgba(0, 0, 0, 1)">{
console.log(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">deviceId>>></span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,deviceId);
uni.setBLEMTU({
deviceId:deviceId,
mtu:</span><span style="color: rgba(128, 0, 128, 1)">255</span><span style="color: rgba(0, 0, 0, 1)">,
success:(res)</span>=><span style="color: rgba(0, 0, 0, 1)">{
console.log(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">设置MTU最大值成功</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,res);
},
fail:(res)</span>=><span style="color: rgba(0, 0, 0, 1)">{
console.log(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">设置MTU最大值失败</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,res);
}
});
},</span><span style="color: rgba(128, 0, 128, 1)">9000</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">4)关闭搜索</span>
<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.$stopBluetoothDevicesDiscovery();
},</span></pre>
</div>
<h2>步骤5:获取蓝牙设备的所有服务 getBLEDeviceServices</h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">$getBLEDeviceServices(deviceId){
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取蓝牙设备所有服务(service)</span>
<span style="color: rgba(0, 0, 0, 1)"> uni.getBLEDeviceServices({
deviceId,
success: (res) </span>=><span style="color: rgba(0, 0, 0, 1)"> {
console.log(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">250res.services>>></span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,res.services);
</span><span style="color: rgba(0, 0, 255, 1)">for</span> (let i = <span style="color: rgba(128, 0, 128, 1)">0</span>; i < res.services.length; i++<span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (res.services.isPrimary) {
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.$getBLEDeviceCharacteristics(deviceId, res.services.uuid);
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">;
}
}
}
});
},</span></pre>
</div>
<h2>步骤6:获取蓝牙设备某个服务中所有特征值(characteristic)getBLEDeviceCharacteristics</h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">$getBLEDeviceCharacteristics(deviceId,serviceId){
let that </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取蓝牙设备某个服务中所有特征值(characteristic)。</span>
<span style="color: rgba(0, 0, 0, 1)"> uni.getBLEDeviceCharacteristics({
deviceId,
serviceId,
success: (res) </span>=><span style="color: rgba(0, 0, 0, 1)"> {
console.log(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">288getBLEDeviceCharacteristics success</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">, res.characteristics);
</span><span style="color: rgba(0, 0, 255, 1)">for</span> (let i = <span style="color: rgba(128, 0, 128, 1)">0</span>; i < res.characteristics.length; i++<span style="color: rgba(0, 0, 0, 1)">) {
let item </span>=<span style="color: rgba(0, 0, 0, 1)"> res.characteristics
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">if (item.properties.read) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">读取低功耗蓝牙设备的特征值的二进制数据值。1</span>
<span style="color: rgba(0, 0, 0, 1)"> uni.readBLECharacteristicValue({
deviceId,
serviceId,
characteristicId: item.uuid,
})
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">}</span>
<span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (item.properties.write) {
</span><span style="color: rgba(0, 0, 255, 1)">this</span>._deviceId =<span style="color: rgba(0, 0, 0, 1)"> deviceId;
</span><span style="color: rgba(0, 0, 255, 1)">this</span>._serviceId =<span style="color: rgba(0, 0, 0, 1)"> serviceId;
</span><span style="color: rgba(0, 0, 255, 1)">this</span>._characteristicId =<span style="color: rgba(0, 0, 0, 1)"> item.uuid;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">写入请求数据</span>
<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.$writeBLECharacteristicValue();
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">if (item.properties.notify || item.properties.indicate) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">启用低功耗蓝牙设备特征值变化时的 notify 功能,订阅特征值。</span>
<span style="color: rgba(0, 0, 0, 1)"> uni.notifyBLECharacteristicValueChange({
deviceId,
serviceId,
characteristicId: item.uuid,
state: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
success(res)
{
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> console.log('notifyBLECharacteristicValueChange success:' + res.errMsg);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> console.log(JSON.stringify(res));</span>
uni.onBLECharacteristicValueChange(characteristic =><span style="color: rgba(0, 0, 0, 1)"> {
console.log(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">监听低功耗蓝牙设备的特征值变化事件成功>></span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将字节转换成16进制字符串</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> data =<span style="color: rgba(0, 0, 0, 1)"> ab2hex(characteristic.value)
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将16进制转成字符串</span>
let newdataStr = Buffer.<span style="color: rgba(0, 0, 255, 1)">from</span>(data,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">hex</span><span style="color: rgba(128, 0, 0, 1)">'</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">先把数据存在buf里面
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">that.btvalue=newdataStr.toString("utf-8");</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">使用toString函数就能转换成字符串
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设备返回SN</span>
that.SN=newdataStr.toString(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">utf-8</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
console.log(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">设备返回SN>>></span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,that.SN);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">根据返回的数据处理设备类型和显示的输入区域-----------------</span>
<span style="color: rgba(0, 0, 0, 1)"> that.getEquType(that.SN);
});
}
});
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">}</span>
<span style="color: rgba(0, 0, 0, 1)"> }
},
fail(res) {
console.error(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">getBLEDeviceCharacteristics</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">, res)
}
})
},</span></pre>
</div>
<h2>步骤7:向蓝牙设备发送一个16进制数据</h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)"> $writeBLECharacteristicValue(){
let buffer </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayBuffer(<span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">)
let dataView </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> DataView(buffer);
dataView.setUint8(</span><span style="color: rgba(128, 0, 128, 1)">0</span>, <span style="color: rgba(128, 0, 128, 1)">0</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">0x61 | 0</span>
<span style="color: rgba(0, 0, 0, 1)"> uni.writeBLECharacteristicValue({
deviceId: </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">._deviceId,
serviceId: </span><span style="color: rgba(0, 0, 255, 1)">this</span>._serviceId,<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">"0000FE61-0000-1000-8000-00805F9B34FB",</span>
characteristicId: <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">._characteristicId,
value: buffer,
success: function(res){
console.log(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">350</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,res);
},
fail: function(res){
console.log(res);
}
})
},</span></pre>
</div>
<h2>步骤8:向蓝牙设备发送字符串数据 writeBLECharacteristicValueString</h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">$writeBLECharacteristicValueString(str){
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 发送方式一</span>
let buffer = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ArrayBuffer(str.length);
let dataView </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> DataView(buffer);
</span><span style="color: rgba(0, 0, 255, 1)">for</span> (let i <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> str) {
dataView.setUint8(i, str.charCodeAt() </span>| <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">打印二进制字节
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">console.log('dataView.getUint8(i)>>',dataView.getUint8(i));</span>
<span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">延迟发送指令</span>
setTimeout(()=><span style="color: rgba(0, 0, 0, 1)">{
uni.writeBLECharacteristicValue({
deviceId: </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">._deviceId,
serviceId: </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">._serviceId,
characteristicId: </span><span style="color: rgba(0, 0, 255, 1)">this</span>._characteristicId,<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">"0000FE61-0000-1000-8000-00805F9B34FB",</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">,</span>
<span style="color: rgba(0, 0, 0, 1)"> value: buffer,
success: function(res){
console.log(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">命令写入成功</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,res);
},
fail: function(res){
console.error(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">命令写入失败</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,res);
}
});
},</span><span style="color: rgba(128, 0, 128, 1)">2000</span><span style="color: rgba(0, 0, 0, 1)">);
},</span></pre>
</div>
<h2>步骤9:关闭搜索 stopBluetoothDevicesDiscovery</h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)"> $stopBluetoothDevicesDiscovery(){
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">关闭搜索</span>
<span style="color: rgba(0, 0, 0, 1)"> uni.stopBluetoothDevicesDiscovery({
success(res) {
</span><span style="color: rgba(0, 0, 255, 1)">this</span>._discoveryStarted=<span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
}
})
},</span></pre>
</div>
<h2>步骤10:断开蓝牙设备连接 closeBLEConnect</h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">$closeBLEConnect(deviceId){
uni.closeBLEConnection({
deviceId,
success:res</span>=><span style="color: rgba(0, 0, 0, 1)">{
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.deviceId = <span style="color: rgba(128, 0, 0, 1)">""</span><span style="color: rgba(0, 0, 0, 1)">;
console.log(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">断开成功</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,res);
},
fail:err</span>=><span style="color: rgba(0, 0, 0, 1)">{
console.error(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">断开错误</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,err);
}
})
},</span></pre>
</div>
<p>需要注意的是:</p>
<p>1)和蓝牙设备通讯不能太频繁。</p>
<p>2)写入数据时候要注意特性值UUID。</p>
<p>3)写入数据超过20bytes时候需要设置MTU。否则传输的内容将被截取,造成传输信息不完整。</p>
<p>本文参考:https://blog.csdn.net/qq_33259323/article/details/105609275</p>
<p> https://www.jianshu.com/p/c627ddc7862d</p>
<p> https://uniapp.dcloud.io/api/system/bluetooth.html</p>
<p> https://uniapp.dcloud.io/api/system/ble.html</p><br><br>
来源:https://www.cnblogs.com/ckfuture/p/16450418.html
頁:
[1]