feat: add support for Web Bluetooth pin pairing (#35416)
* feat: add bluetooth pairing handler * Update docs/api/session.md Co-authored-by: Charles Kerr <charles@charleskerr.com> * Update docs/api/session.md Co-authored-by: Charles Kerr <charles@charleskerr.com> * docs: update based on review * Apply suggestions from code review Co-authored-by: Erick Zhao <erick@hotmail.ca> Co-authored-by: Charles Kerr <charles@charleskerr.com> * chore: update docs per review * chore: cleanup callback per review Co-authored-by: Charles Kerr <charles@charleskerr.com> Co-authored-by: Erick Zhao <erick@hotmail.ca>
This commit is contained in:
parent
f8077cc004
commit
697a219bcb
11 changed files with 234 additions and 6 deletions
|
@ -823,6 +823,71 @@ app.whenReady().then(() => {
|
|||
})
|
||||
```
|
||||
|
||||
#### `ses.setBluetoothPairingHandler(handler)` _Windows_ _Linux_
|
||||
|
||||
* `handler` Function | null
|
||||
* `details` Object
|
||||
* `deviceId` string
|
||||
* `pairingKind` string - The type of pairing prompt being requested.
|
||||
One of the following values:
|
||||
* `confirm`
|
||||
This prompt is requesting confirmation that the Bluetooth device should
|
||||
be paired.
|
||||
* `confirmPin`
|
||||
This prompt is requesting confirmation that the provided PIN matches the
|
||||
pin displayed on the device.
|
||||
* `providePin`
|
||||
This prompt is requesting that a pin be provided for the device.
|
||||
* `frame` [WebFrameMain](web-frame-main.md)
|
||||
* `pin` string (optional) - The pin value to verify if `pairingKind` is `confirmPin`.
|
||||
* `callback` Function
|
||||
* `response` Object
|
||||
* `confirmed` boolean - `false` should be passed in if the dialog is canceled.
|
||||
If the `pairingKind` is `confirm` or `confirmPin`, this value should indicate
|
||||
if the pairing is confirmed. If the `pairingKind` is `providePin` the value
|
||||
should be `true` when a value is provided.
|
||||
* `pin` string | null (optional) - When the `pairingKind` is `providePin`
|
||||
this value should be the required pin for the Bluetooth device.
|
||||
|
||||
Sets a handler to respond to Bluetooth pairing requests. This handler
|
||||
allows developers to handle devices that require additional validation
|
||||
before pairing. When a handler is not defined, any pairing on Linux or Windows
|
||||
that requires additional validation will be automatically cancelled.
|
||||
macOS does not require a handler because macOS handles the pairing
|
||||
automatically. To clear the handler, call `setBluetoothPairingHandler(null)`.
|
||||
|
||||
```javascript
|
||||
|
||||
const { app, BrowserWindow, ipcMain, session } = require('electron')
|
||||
|
||||
let bluetoothPinCallback = null
|
||||
|
||||
function createWindow () {
|
||||
const mainWindow = new BrowserWindow({
|
||||
webPreferences: {
|
||||
preload: path.join(__dirname, 'preload.js')
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Listen for an IPC message from the renderer to get the response for the Bluetooth pairing.
|
||||
ipcMain.on('bluetooth-pairing-response', (event, response) => {
|
||||
bluetoothPinCallback(response)
|
||||
})
|
||||
|
||||
mainWindow.webContents.session.setBluetoothPairingHandler((details, callback) => {
|
||||
bluetoothPinCallback = callback
|
||||
// Send a IPC message to the renderer to prompt the user to confirm the pairing.
|
||||
// Note that this will require logic in the renderer to handle this message and
|
||||
// display a prompt to the user.
|
||||
mainWindow.webContents.send('bluetooth-pairing-request', details)
|
||||
})
|
||||
|
||||
app.whenReady().then(() => {
|
||||
createWindow()
|
||||
})
|
||||
```
|
||||
|
||||
#### `ses.clearHostResolverCache()`
|
||||
|
||||
Returns `Promise<void>` - Resolves when the operation is complete.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue