看一看下面的资源:
但他们的解决方案似乎行不通。
这是我的main
文件:
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
文件中的一些代码:
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)。
如果你能发现任何问题,请建议与上述方法。
发布于 2022-06-07 10:05:50
必须启用蓝牙接入电子和VSCode通过系统首选项。
https://stackoverflow.com/questions/72508328
复制相似问题