我在使用網路藍牙 API 時遇到了一些問題。問題是我有一個溫濕度記錄儀藍牙設備,它通過傳感器記錄了溫濕度資料。我想通過在 javascript 中使用網路藍牙在我的網路應用程式中獲取該資料。那么我該如何執行此任務?
我嘗試過網路藍牙,但我只能獲取設備名稱和電池電量,但無法獲取溫度和濕度資料。
uj5u.com熱心網友回復:
您需要知道什么藍牙 GATT 服務提供溫度和濕度資料。“nRF Connect”和瀏覽器內置的 about://bluetooth-internals 頁面等應用程式可以幫助您探索設備提供的服務。
Web 藍牙要求您將要訪問的服務的 UUID 傳遞給navigator.bluetooth.requestDevice()函式,因此您需要確定要訪問的 GATT 服務,然后才能BluetoothRemoteGATTServer在 API 提供的物件上看到它們。
如果您將設備的制造商和型號資訊作為問題的一部分發布,那么某人可能會指導您訪問制造商提供的有關設備提供的 GATT 服務的檔案,或者已經對如何訪問您正在查找的資料進行逆向工程的人為了。
uj5u.com熱心網友回復:
如果藍牙設備使用 GATT 環境感應服務(GATT 分配編號0x181A),您應該能夠讀取并獲得 GATT 溫度 ( 0x2A6E) 和濕度 ( 0x2A6F) 特性的通知。
這是一些應該*作業的代碼。
const serviceUuid = 0x181A;
// Requesting Bluetooth Device assuming it advertises
// the GATT Environmental Sensing Service...
const device = await navigator.bluetooth.requestDevice({
filters: [{services: [serviceUuid]}]});
// Connecting to GATT Server...
const server = await device.gatt.connect();
// Getting Environmental Sensing Service...
const service = await server.getPrimaryService(serviceUuid);
// Getting Humidity Characteristic...
const characteristic = await service.getCharacteristic(0x2A6F);
// Reading Humidity...
const value = await characteristic.readValue();
console.log(value);
https://googlechrome.github.io/samples/web-bluetooth/notifications.html?characteristic=humidity&service=environmental_sensing上的示例也可以幫助您入門。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/415108.html
標籤:
上一篇:img-tag中src的JS邏輯
