[英]Bluetooth pop-up
所以我試圖用一些代碼來測試藍牙連接。 每當我嘗試掃描藍牙設備時,我都不會彈出允許掃描的窗口,因此它一直在等待。 我知道該代碼有效,因為我們在團隊中工作,它可以在我伙伴擁有的 MacOS 計算機上運行,但它不能在我的 windows 上運行。我主要希望它使用 BLE 掃描 ESP。 我已經啟用了“實驗性 Web 平台功能”和“使用 Web 藍牙的新權限后端”標志,並確保我的 Chrome 已更新,並確保藍牙掃描和設備的權限已啟用。
console.log("Init");
document.getElementById("scan").onclick = scan;
navigator.bluetooth.addEventListener("advertisementreceived", (event) => {
console.log("Advertisement", event);
});
async function scanDevices() {
const devices = await navigator.bluetooth.getDevices();
console.log(devices);
}
async function scan() {
console.log("Scanning...");
let options = {
acceptAllAdvertisements: true,
};
try {
log("Requesting Bluetooth Scan with options: " + JSON.stringify(options));
const scan = await navigator.bluetooth.requestLEScan(options);
log("Scan started with:");
log(" acceptAllAdvertisements: " + scan.acceptAllAdvertisements);
log(" active: " + scan.active);
log(" keepRepeatedDevices: " + scan.keepRepeatedDevices);
log(" filters: " + JSON.stringify(scan.filters));
navigator.bluetooth.addEventListener("advertisementreceived", (event) => {
log("Advertisement received.");
log(" Device Name: " + event.device.name);
log(" Device ID: " + event.device.id);
log(" RSSI: " + event.rssi);
log(" TX Power: " + event.txPower);
log(" UUIDs: " + event.uuids);
event.manufacturerData.forEach((valueDataView, key) => {
logDataView("Manufacturer", key, valueDataView);
});
event.serviceData.forEach((valueDataView, key) => {
logDataView("Service", key, valueDataView);
});
});
setTimeout(stopScan, 10000);
function stopScan() {
console.log("Scan result", scan);
log("Stopping scan...");
scan.stop();
log("Stopped. scan.active = " + scan.active);
}
} catch (error) {
log("Argh! " + error);
}
}
function log(c) {
console.log(c);
}
使用requestDevice彈出選擇器 UI。
const device = await navigator.bluetooth.requestDevice(options)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.