首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >得到“用户取消了requestDevice()选择器”。电子17.x + Web蓝牙的错误

得到“用户取消了requestDevice()选择器”。电子17.x + Web蓝牙的错误
EN

Stack Overflow用户
提问于 2022-06-05 14:48:19
回答 1查看 90关注 0票数 0

看一看下面的资源:

  1. Web Bluetooth & Chrome Extension: User cancelled the requestDevice() chooser
  2. Electron Web Bluetooth API requestDevice() Error
  3. Can you manipulate web bluetooth chooser that shows after calling requestDevice()?

但他们的解决方案似乎行不通。

这是我的main文件:

代码语言:javascript
复制
import { app, BrowserWindow } from "electron";

/**
 * Ref: https://www.electronjs.org/docs/tutorial/quick-start#create-the-main-script-file
 */
async function createWindow(): Promise<BrowserWindow> {
  // Creating a browser window
  const win = new BrowserWindow({
    webPreferences: {
      nodeIntegration: true,
    },
  });

  /**
   * Handle bluetooth connection
   * Ref: https://www.electronjs.org/docs/latest/tutorial/devices#web-bluetooth-api
   */
  win.webContents.on("select-bluetooth-device", (event, devices, callback) => {
    event.preventDefault();
    if (devices.length > 0) {
      callback(devices[0].deviceId);
    }
  });

  // Load index.html
  win.maximize();
  await win.loadFile("./dist/renderer/index.html");
  return win;
}

function setUpElectronApp(): void {
  // Browser window
  let win: BrowserWindow | undefined;

  // Enable webBluetooth
  app.commandLine.appendSwitch("enable-experimental-web-platform-features", "true");
  app.commandLine.appendSwitch("enable-web-bluetooth", "true");

  // Create bowser window once the electron app is initialized
  app
    .whenReady()
    .then(() => {
      createWindow()
        .then((response) => {
          win = response;
          console.log(win);
        })
        .catch((err) => {
          throw err as Error;
        });
    })
    .catch((err) => {
      throw err as Error;
    });

  // Quit the application when it no longer has any open windows
  app.on("window-all-closed", () => {
    if (process.platform !== "darwin") {
      // The action is no-op on macOS due to the OS' behavior (https://support.apple.com/en-ca/guide/mac-help/mchlp2469/mac)
      // On macOS it is common for applications and their menu bar to stay active until the user quits explicitly with Cmd + Q
      app.quit();
      win = undefined;
    }
  });

  // Create a new browser window only when the application has no visible windows being activated
  app.on("activate", () => {
    if (BrowserWindow.getAllWindows().length === 0) {
      // On macOS it's common to re-create a window in the app when the dock icon is clicked and there are no other windows open.
      createWindow()
        .then((response) => {
          win = response;
          console.log(win);
        })
        .catch((err) => {
          throw err;
        });
    }
  });
}

setUpElectronApp();

下面是renderer文件中的一些代码:

代码语言:javascript
复制
function onDisconnected(event: Event) {
  const device = event.target as BluetoothDevice;
  console.log(`Device ${device.name} is disconnected.`);
}

export async function deviceConnect(): Promise<DeviceInfo> {

  const device = await navigator.bluetooth.requestDevice({
    filters: [
      {
        namePrefix: "Polar Sense",
        manufacturerData: [{ companyIdentifier: 0x006b }], // For 'Polar Electro OY' company.
      },
    ],
    acceptAllDevices: false,
    optionalServices: [0x180d, 0x180f],
  });

  device.addEventListener("gattserverdisconnected", onDisconnected);


  const server = await device.gatt?.connect();

  const heartRateService = await server?.getPrimaryService(0x180d);
  const heartRate = await heartRateService?.getCharacteristic(0x2a37);

  const batteryLevelService = await server?.getPrimaryService(0x180f);
  const batteryLevel = await batteryLevelService?.getCharacteristic(0x2a19);

  return { heartRate, batteryLevel, deviceID: device.name };
}

在我的前面有一个按钮,当点击时触发上面的函数。不断地得到Uncaught (in promise) DOMException: User cancelled the requestDevice() chooser.错误。也尝试过不同版本的电子。还在挠我的头。

请让我知道是否有可能访问网络蓝牙API的电子。在"electron": "^17.4.7",上使用macOS蒙特利(12.4)。

如果你能发现任何问题,请建议与上述方法。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-07 10:05:50

必须启用蓝牙接入电子和VSCode通过系统首选项

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72508328

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档