我正在使用 Electron 列出周围所有蓝牙设备(活动的),我想选择一个并连接到它。
我的问题: 现在,当我运行 npm start 并按下窗口中的按钮时,控制台开始吐出一些蓝牙设备,但不幸的是,这不是我正在寻找的设备。有人对我为什么在这里找不到该设备有任何建议吗?另外,电子中没有弹出窗口显示这些设备,对吗?
代码/设置:
渲染器.js:
async function testIt() {
const device = await navigator.bluetooth.requestDevice({
acceptAllDevices: true
})
document.getElementById('device-name').innerHTML = device.name || `ID: ${device.id}`
}
document.getElementById('clickme').addEventListener('click',testIt)
main.js:
const {app, BrowserWindow} = require('electron')
const path = require('path')
function createWindow () {
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
enableBlinkFeatures: 'Bluetooth', // Enable Bluetooth features
contextIsolation: false, // Set to false to enable direct access for demo purposes
nodeIntegration: true,
},
})
mainWindow.webContents.on('select-bluetooth-device', (event, deviceList, callback) => {
event.preventDefault()
if (deviceList && deviceList.length > 0) {
console.log(deviceList);
}
})
mainWindow.loadFile('index.html')
}
app.whenReady().then(() => {
createWindow()
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
package.json:
{
"name": "test_bt",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"start": "electron ."
},
"author": "",
"license": "ISC",
"devDependencies": {
"electron": "^31.2.1"
}
}
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'">
<title>Web Bluetooth API</title>
</head>
<body>
<h1>Web Bluetooth API</h1>
<button id="clickme">Test Bluetooth</button>
<p>Currently selected bluetooth device: <strong id="device-name""></strong></p>
<script src="./renderer.js"></script>
</body>
</html>
预加载.js:
const { contextBridge, ipcRenderer } = require('electron/renderer')
contextBridge.exposeInMainWorld('electronAPI', {
cancelBluetoothRequest: () => ipcRenderer.send('cancel-bluetooth-request'),
bluetoothPairingRequest: (callback) => ipcRenderer.on('bluetooth-pairing-request', () => callback()),
bluetoothPairingResponse: (response) => ipcRenderer.send('bluetooth-pairing-response', response)
})
额外信息:
当我使用笔记本电脑连接到设备时,我得到以下信息:
Driver Name: bth.inf
Class Guid: {e0cbf06c-cd8b-4647-bb8a-263b43f0f974}
Driver Date: 06/21/2006
Driver Version: 10.0.19041.4412
Driver Provider: Microsoft
Driver Section: BthGenericDevice.NT
Driver Rank: 0xFF2000
Matching Device Id: BTHENUM\GENERIC_DEVICE
Outranked Drivers:
Device Updated: false
Parent Device: BTH\MS_BTHBRB\6&13dbeec2&0&1
在 qT 中,我还可以列出设备,并且可以像这样连接到设备:
_btSocket = new bluetoothSocket();
_btSocket->connectToService(_btDeviceInfo->address(), QBluetoothUuid::ServiceClassUuid::SerialPort);
node -v: v20.12.2
electron: 31.2.1
npm -v: 10.5.0
从您的示例来看,您似乎正在尝试连接到经典蓝牙设备(“蓝牙套接字”是经典蓝牙概念),而 Web 蓝牙 API 不支持该设备。经典设备被过滤掉,这就是为什么您在传递给“选择蓝牙设备”处理程序的事件中看不到它们的原因。
您应该能够通过最近添加的连接蓝牙 RFCOMM 服务的支持来使用 Web Serial API:https://developer.chrome.com/blog/serial-over-bluetooth
这应该在基于 Chrome 117 或更高版本的任何 Electron 版本中可用,根据 https://releases. Electronjs.org/,这似乎是任何当前的稳定版本(29、30 或 31)。