?藍(lán)牙列表期待效果
?代碼
<template>
<view class="bluetooth-list">
<view class="align-items option" style="justify-content: space-between;" v-for="item in bluetoothList" :key="item.deviceId">
<view class="">
<view class="title">{{item.name || item.localName}}</view>
<view class="desc">{{item.deviceId}}</view>
</view>
<view class="bind-btn" @click="onBind(item)">
綁定設(shè)備
</view>
</view>
</view>
</template>
?js里面注意getBLEDeviceCharacteristics獲取特征值的時候,極個別設(shè)備參數(shù)write,read,notify是亂來的,需要自己打單獨處理,通過對應(yīng)write,read,notify 為true的時候拿到對應(yīng)的uuid,文章來源:http://www.zghlxwxcb.cn/news/detail-766000.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-766000.html
<script>
export default {
data() {
return {
bluetoothObj:{},
bluetoothList:[],
services: [],
serviceId: 0,
writeCharacter: false,
readCharacter: false,
notifyCharacter: false
};
},
onLoad() {
this.init()
},
onUnload() {
//停止搜索藍(lán)牙設(shè)備
if (this.isSearching) {
uni.stopBluetoothDevicesDiscovery();
}
},
methods: {
// 初始化
init(){
let that = this;
uni.openBluetoothAdapter({
success(res) {
uni.getBluetoothAdapterState({
success(res2) {
if (res2.available) {
if (res2.discovering) {
uni.showToast({
title: '正在搜索附近打印機設(shè)備',
icon: "none"
})
return;
}
//獲取藍(lán)牙設(shè)備信息
that.getBluetoothDevices()
} else {
uni.showModal({
title: '提示',
content: '本機藍(lán)牙不可用',
})
}
}
});
},
fail() {
uni.showModal({
title: '提示',
content: '藍(lán)牙初始化失敗,請打開藍(lán)牙',
})
}
})
},
//獲取藍(lán)牙設(shè)備信息
getBluetoothDevices() {
let that = this
that.bluetoothList = [];
uni.startBluetoothDevicesDiscovery({
success(res) {
//藍(lán)牙設(shè)備監(jiān)聽 uni.onBluetoothDeviceFound
uni.onBluetoothDeviceFound((result) => {
let arr = that.bluetoothList;
let devices = [];
let list = result.devices;
for (let i = 0; i < list.length; ++i) {
if (list[i].name && list[i].name != "未知設(shè)備") {
let arrNew = arr.filter((item) => {
return item.deviceId == list[i].deviceId;
});
// console.log('arrNew:',arrNew.length)
if (arrNew.length == 0) {
devices.push(list[i]);
}
}
}
that.bluetoothList = arr.concat(devices);
console.log("bluetoothList",that.bluetoothList)
});
that.time = setTimeout(() => {
// uni.getBluetoothDevices
uni.getBluetoothDevices({
success(res2) {
let devices = [];
let list = res2.devices;
for (let i = 0; i < list.length; ++i) {
if (list[i].name && list[i].name != "未知設(shè)備") {
devices.push(list[i]);
}
}
that.bluetoothList = devices;
},
})
clearTimeout(that.time);
}, 3000);
}
});
},
// 綁定藍(lán)牙
onBind(item){
uni.stopBluetoothDevicesDiscovery();
let that = this;
let { deviceId } = item;
console.log('item',item)
that.bluetoothObj.deviceId = deviceId;
that.serviceId = 0;
that.writeCharacter = false;
that.readCharacter = false;
that.notifyCharacter = false;
// uni.showLoading({
// title: '正在連接',
// })
uni.openBluetoothAdapter({
success: function () {
uni.createBLEConnection({
deviceId,
success(res) {
console.log('createBLEConnection success', res)
uni.hideLoading()
that.getSeviceId()
},
fail(e) {
console.log('createBLEConnection fail', e)
uni.hideLoading()
}
})
},
fail: function (error) {
console.log("openBluetoothAdapter")
}
})
},
//獲取藍(lán)牙設(shè)備所有服務(wù)(service)。
getSeviceId() {
let that = this;
let t=setTimeout(()=>{
uni.getBLEDeviceServices({
deviceId: that.bluetoothObj.deviceId,
success(res) {
console.log('getBLEDeviceServices success', res)
that.services = res.services;
that.getCharacteristics()
},
fail: function(e) {
}
})
clearTimeout(t);
},1500)
},
getCharacteristics() {
var that = this
let {
services: list,
serviceId: num,
writeCharacter: write,
readCharacter: read,
notifyCharacter: notify
} = that;
// uni.getBLEDeviceCharacteristics
uni.getBLEDeviceCharacteristics({
deviceId: that.bluetoothObj.deviceId,
serviceId: list[num].uuid,
success(res) {
console.log('getBLEDeviceCharacteristics success', res)
// console.log(res)
for (var i = 0; i < res.characteristics.length; ++i) {
var properties = res.characteristics[i].properties
var item = res.characteristics[i].uuid
if (!notify) {
if (properties.notify) {
that.bluetoothObj.notifyCharaterId = item;
that.bluetoothObj.notifyServiceId = list[num].uuid;
notify = true
}
}
if (!write) {
if (properties.write) {
that.bluetoothObj.writeCharaterId = item;
that.bluetoothObj.writeServiceId = list[num].uuid;
write = true
}
}
if (!read) {
if (properties.read) {
that.bluetoothObj.readCharaterId = item;
that.bluetoothObj.readServiceId = list[num].uuid;
read = true
}
}
}
if (!write || !notify || !read) {
num++
that.writeCharacter = write;
that.readCharacter = read;
that.notifyCharacter = notify;
that.serviceId = num;
if (num == list.length) {
uni.showModal({
title: '提示',
content: '找不到該讀寫的特征值',
})
} else {
that.getCharacteristics()
}
} else {
// ok
// wx.writeBLECharacteristicValue
uni.notifyBLECharacteristicValueChange({
state: true, // 啟用 notify 功能
// 這里的 deviceId 需要已經(jīng)通過 createBLEConnection 與對應(yīng)設(shè)備建立鏈接
deviceId:that.bluetoothObj.deviceId,
// 這里的 serviceId 需要在 getBLEDeviceServices 接口中獲取
serviceId:that.bluetoothObj.serviceId,
// 這里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中獲取
characteristicId:that.bluetoothObj.notifyServiceId,
success (res) {
console.log('notifyBLECharacteristicValueChange success', res.errMsg)
uni.onBLECharacteristicValueChange(function (e) {
/**對設(shè)備發(fā)送過來的參數(shù)進(jìn)行解密 */
let str = that.ab2hex(e.value);
console.log("解密str",str)
})
}
})
console.log("that.bluetoothObj",that.bluetoothObj)
uni.setStorageSync("bluetoothObj", that.bluetoothObj)
uni.showToast({
icon:"none",
title:"綁定成功"
})
setTimeout(()=>{
uni.navigateBack({
delta:1
})
},1000)
}
},
fail: function(e) {
console.log("getBLEDeviceCharacteristics fail:",e);
}
})
},
ab2hex: (buffer) => {
const hexArr = Array.prototype.map.call(
new Uint8Array(buffer),
function (bit) {
return ('00' + bit.toString(16)).slice(-2)
}
)
return hexArr.join('')
},
str2ab:(str) => {
var buf = new ArrayBuffer(str.length / 2);
var bufView = new Uint8Array(buf);
for (var i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = parseInt(str.slice(i * 2, i * 2 + 2), 16);
}
return buf;
}
}
}
</script>
<style lang="less" scoped>
.bluetooth-list{
background-color: #F3F3F3;
.option{
margin: 20rpx;
padding: 20rpx 32rpx;
background-color: #fff;
border-radius: 20rpx;
.title{
font-weight: 600;
font-size: 32rpx;
}
.desc{
font-size: 28rpx;
color: #999;
margin-top: 12rpx;
}
.bind-btn{
background-color: #3F96DB;
color: #fff;
width: 200rpx;
height: 70rpx;
line-height: 70rpx;
border-radius: 35rpx;
text-align: center;
font-size: 30rpx;
}
}
}
</style>
到了這里,關(guān)于微信小程序藍(lán)牙連接 uniApp藍(lán)牙連接設(shè)備的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!