開源
wx聯(lián)系本人獲取源碼(開源):
MJ682517
html
<view>
<view class="p_l_36 p_r_36">
<input class="w_100_ h_80 lh_80 ta_c b_2s_eee radius_20" value="{{instructVal}}" type="text" placeholder="請輸入開門指令" bindinput="bindinput" />
</view>
<view class="m_t_36 w_50_ h_90 lh_90 m_l_a m_r_a bc_409eff radius_10 color_fff ta_c" catchtap="openBluetoothAdapter">藍(lán)牙開鎖</view>
</view>
JavaScript
需要從下往上閱讀,使用函數(shù)自調(diào)的方式解決
API
不能及時獲取數(shù)據(jù)的問題,替換方案是使用定時器,但是個人覺得定時器不好,所以使用了函數(shù)自調(diào)的方式實(shí)現(xiàn)獲取不到數(shù)據(jù)的問題。
getBluetoothDevices
方法中獲取deviceId
;getBLEDeviceServices
方法中獲取到serviceId
,獲取到的是數(shù)組,取數(shù)組中的uuid
作為serviceId
值,具體用哪個值,需要與藍(lán)牙設(shè)備文檔比對;getBLEDeviceCharacteristics
方法中獲取characteristicId
,獲取到的是數(shù)組,取數(shù)組中的uuid
作為characteristicId
值。
一般情況的對應(yīng)的值如下
serviceId: 0000FFB0-0000-1000-8000-00805F9B34FB
notifyId: 0000FFB2-0000-1000-8000-00805F9B34FB
writeId: 0000FFB1-0000-1000-8000-00805F9B34FB
注意觀察第一個橫杠前面最后兩位的值,不難發(fā)現(xiàn)規(guī)律。
寫入的指令一般是以十六進(jìn)制(EE03E30100
)字符串的方式,最后轉(zhuǎn)為二進(jìn)制發(fā)送給藍(lán)牙。
坑: 在不確定藍(lán)牙文檔是否正確的情況下,寫入成功,但是藍(lán)牙設(shè)備毫無反應(yīng),那么大概率就是寫入的指令有問題,所以需要校對一下指令是否正確。
藍(lán)牙文檔需要與廠商獲取。
獲取藍(lán)牙服務(wù)值方法
getBLEDeviceServices
。
獲取藍(lán)牙特征值方法getBLEDeviceCharacteristics
。特征值一般包括兩個,一個代表寫入值,一個代表讀取值,意思是說寫入的是需要把寫入的特征值帶上,讀取的時候把讀取的特征值帶上。文章來源:http://www.zghlxwxcb.cn/news/detail-611592.html
notifyBLECharacteristicValueChange
方法啟用藍(lán)牙低功耗設(shè)備特征值變化時的notify
功能,訂閱特征。此時傳入的是讀取的特征值。文章來源地址http://www.zghlxwxcb.cn/news/detail-611592.html
// index.js
let timeout = undefined;
Page({
data: {
devices: [],
deviceId: '',
services: [],
serviceId: '',
characteristics: [],
characteristicId: '',
instructVal: 'EE03E30100'
},
// 指令輸入
bindinput({
detail: {
value
}
}) {
this.setData({
instructVal: value
});
},
// 失敗時的統(tǒng)一提示
failShowToast(title = '開鎖失敗') {
clearTimeout(timeout);
timeout = undefined;
wx.hideLoading();
wx.showToast({
icon: 'none',
title
});
},
/**
* 根據(jù)不同方法名調(diào)用方法,統(tǒng)一定時器的觸發(fā)判斷,
* 當(dāng)定時器觸發(fā)時,無法確定當(dāng)前調(diào)用的方法是哪個
* @param {String} fnName
*/
methodExecution(fnName = '') {
if (timeout) this[fnName]();
},
// 關(guān)閉藍(lán)牙模塊
closeBluetoothAdapter() {
let that = this;
// 關(guān)閉藍(lán)牙模塊
wx.closeBluetoothAdapter({
success() {},
fail() {
that.methodExecution('closeBluetoothAdapter');
}
});
},
// 斷開與藍(lán)牙低功耗設(shè)備的連接
closeBLEConnection() {
let that = this;
// 斷開與藍(lán)牙低功耗設(shè)備的連接
wx.closeBLEConnection({
deviceId: that.data.deviceId,
success() {
that.closeBluetoothAdapter();
},
fail() {
that.methodExecution('closeBLEConnection');
}
});
},
// 向藍(lán)牙低功耗設(shè)備特征值中寫入二進(jìn)制數(shù)據(jù)
writeBLECharacteristicValue() {
let that = this;
let str = that.data.instructVal;
if (!str) return wx.showToast({
title: '請輸入指令',
icon: 'none'
});
/* 將數(shù)值轉(zhuǎn)為ArrayBuffer類型數(shù)據(jù) */
let typedArray = new Uint8Array(str.match(/[\da-f]{2}/gi).map((h) => parseInt(h, 16))),
buffer = typedArray.buffer;
// 向藍(lán)牙低功耗設(shè)備特征值中寫入二進(jìn)制數(shù)據(jù)
wx.writeBLECharacteristicValue({
deviceId: that.data.deviceId,
serviceId: that.data.serviceId,
characteristicId: '0000FFB1-0000-1000-8000-00805F9B34FB',
value: buffer,
success() {
clearTimeout(timeout);
timeout = undefined;
wx.hideLoading();
wx.showToast({
icon: 'none',
title: '開鎖成功'
});
// that.closeBLEConnection();
},
fail() {
that.methodExecution('writeBLECharacteristicValue');
}
});
},
// 監(jiān)聽藍(lán)牙低功耗設(shè)備的特征值變化事件
onBLECharacteristicValueChange() {
let that = this;
// 監(jiān)聽藍(lán)牙低功耗設(shè)備的特征值變化事件
wx.onBLECharacteristicValueChange((res) => {
if (res.value) {
that.writeBLECharacteristicValue();
} else {
that.methodExecution('onBLECharacteristicValueChange');
}
});
},
// 啟用藍(lán)牙低功耗設(shè)備特征值變化時的 notify 功能,訂閱特征
notifyBLECharacteristicValueChange() {
let that = this;
// 啟用藍(lán)牙低功耗設(shè)備特征值變化時的 notify 功能,訂閱特征
wx.notifyBLECharacteristicValueChange({
state: true,
deviceId: that.data.deviceId,
serviceId: that.data.serviceId,
characteristicId: that.data.characteristicId,
success() {
that.onBLECharacteristicValueChange();
},
fail() {
that.methodExecution('notifyBLECharacteristicValueChange');
}
});
},
// 獲取藍(lán)牙低功耗設(shè)備某個服務(wù)中所有特征 (characteristic)
getBLEDeviceCharacteristics() {
let that = this;
// 獲取藍(lán)牙低功耗設(shè)備某個服務(wù)中所有特征 (characteristic)
wx.getBLEDeviceCharacteristics({
deviceId: that.data.deviceId,
serviceId: that.data.serviceId,
success({
characteristics
}) {
that.setData({
characteristics,
characteristicId: '0000FFB2-0000-1000-8000-00805F9B34FB'
}, () => that.notifyBLECharacteristicValueChange());
},
fail() {
that.methodExecution('getBLEDeviceCharacteristics');
}
});
},
// 獲取藍(lán)牙低功耗設(shè)備所有服務(wù) (service)
getBLEDeviceServices() {
let that = this;
// 獲取藍(lán)牙低功耗設(shè)備所有服務(wù) (service)
wx.getBLEDeviceServices({
deviceId: that.data.deviceId,
success({
services
}) {
that.setData({
services,
serviceId: '0000FFB0-0000-1000-8000-00805F9B34FB'
}, () => that.getBLEDeviceCharacteristics());
},
fail() {
that.methodExecution('getBLEDeviceServices');
}
});
},
// 停止搜尋附近的藍(lán)牙外圍設(shè)備
stopBluetoothDevicesDiscovery() {
let that = this;
// 停止搜尋附近的藍(lán)牙外圍設(shè)備
wx.stopBluetoothDevicesDiscovery({
success() {
that.getBLEDeviceServices();
},
fail() {
that.methodExecution('stopBluetoothDevicesDiscovery');
}
});
},
// 連接藍(lán)牙低功耗設(shè)備
createBLEConnection() {
let that = this;
// 連接藍(lán)牙低功耗設(shè)備
wx.createBLEConnection({
deviceId: that.data.deviceId,
success() {
that.stopBluetoothDevicesDiscovery();
},
fail() {
that.methodExecution('createBLEConnection');
}
});
},
// 獲取在藍(lán)牙模塊生效期間所有搜索到的藍(lán)牙設(shè)備
getBluetoothDevices() {
let that = this;
// 獲取在藍(lán)牙模塊生效期間所有搜索到的藍(lán)牙設(shè)備
wx.getBluetoothDevices({
success({
devices
}) {
for (let i = 0; i < devices.length; i++) {
const item = devices[i];
if (item.name === 'YX_0A45320C78C6') {
item.ASUUID = item.advertisServiceUUIDs[0];
that.setData({
devices: item,
deviceId: item.deviceId
}, () => that.createBLEConnection());
break;
}
}
that.methodExecution('getBluetoothDevices');
},
fail() {
that.methodExecution('getBluetoothDevices');
}
});
},
// 開始搜尋附近的藍(lán)牙外圍設(shè)備
startBluetoothDevicesDiscovery() {
let that = this;
// 開始搜尋附近的藍(lán)牙外圍設(shè)備
wx.startBluetoothDevicesDiscovery({
// 此字段會導(dǎo)致startBluetoothDevicesDiscovery不執(zhí)行
// services: ['YX'],
success() {
that.getBluetoothDevices();
},
fail() {
that.methodExecution('startBluetoothDevicesDiscovery');
}
});
},
// 藍(lán)牙開鎖
openBluetoothAdapter() {
let that = this,
thatData = that.data;
if (!that.data.instructVal) return wx.showToast({
title: '請輸入指令',
icon: 'none'
});
if (timeout) return wx.showToast({
title: '加載中',
icon: 'none'
});
wx.showLoading({
title: '加載中',
mask: true
});
timeout = setTimeout(() => {
that.failShowToast('開鎖失敗');
}, 1000 * 26);
if (thatData.deviceId && thatData.serviceId && thatData.characteristicId) return that.writeBLECharacteristicValue();
// 初始化藍(lán)牙模塊
wx.openBluetoothAdapter({
success() {
that.setData({
devices: [],
deviceId: '',
services: [],
serviceId: '',
characteristics: [],
characteristicId: ''
}, () => that.startBluetoothDevicesDiscovery());
},
fail() {
that.failShowToast('查看手機(jī)藍(lán)牙是否打開');
}
});
},
onLoad() {
}
})
到了這里,關(guān)于微信小程序?qū)崿F(xiàn)藍(lán)牙開鎖、開門、開關(guān)、指令發(fā)送成功,但藍(lán)牙設(shè)備毫無反應(yīng)、坑的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!