-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Allow to acces and choose the Hardware Adapter to use #189
base: main
Are you sure you want to change the base?
Changes from 7 commits
ce2f0bc
08e683f
67f766b
6663611
17bd00b
fda4fbe
dfcdc5c
b02aaac
05ac545
46079d3
990dea4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Node Web Bluetooth | ||
* Copyright (c) 2022 Rob Moran | ||
* | ||
* The MIT License (MIT) | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
const webbluetooth = require("../"); | ||
|
||
console.log(webbluetooth.getSimpleBleHardwareAdapters()); |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -53,6 +53,24 @@ export class SimplebleAdapter extends EventEmitter implements BluetoothAdapter { | |||||
private descriptors = new Map<string, string[]>(); | ||||||
private charEvents = new Map<string, (value: DataView) => void>(); | ||||||
|
||||||
getHardwareAdapters() { | ||||||
return getAdapters().map( | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume this is to reduce the exposed fields? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, because we do not want to expose the whole "adapter instance", but just the informative fields. I can add a comment |
||||||
({ identifier, address, active }) => | ||||||
({ identifier, address, active }) | ||||||
); | ||||||
} | ||||||
|
||||||
useHardwareAdapter(adapterIndex: number) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Please make this explicitly public |
||||||
if (this.adapter !== undefined) { | ||||||
throw new Error('Can not change adapter after already used.'); | ||||||
} | ||||||
const adapter = getAdapters()[adapterIndex]; | ||||||
if (adapter === undefined) { | ||||||
throw new Error(`Adapter ${adapterIndex} not found.`); | ||||||
} | ||||||
this.adapter = getAdapters()[adapterIndex]; | ||||||
} | ||||||
|
||||||
private validDevice(device: Partial<BluetoothDeviceImpl>, serviceUUIDs: Array<string>): boolean { | ||||||
if (serviceUUIDs.length === 0) { | ||||||
// Match any device | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,8 @@ | |
* SOFTWARE. | ||
*/ | ||
|
||
import { HardwareAdapterDetails } from './adapter'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const simpleble = require('bindings')('simpleble.node'); | ||
module.exports = simpleble; | ||
|
@@ -85,10 +87,7 @@ export interface Peripheral { | |
} | ||
|
||
/** SimpleBLE Adapter. */ | ||
export interface Adapter { | ||
identifier: string; | ||
address: string; | ||
active: boolean; | ||
export interface Adapter extends HardwareAdapterDetails { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it worth merging these types? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To have a clearly typed separateion between whats exposed as "information" and whats a full adapte rinstance I would leave it separated. Or what do you mean? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we change names I would rather rename HardwareAdapterDetails to just AdapterDetails ... But yes this naming overlapping that we have "native BLE adapters" aka "Hardware level" and "BLE providers like Noble OR SimpleBLE" (at least structurewise) that are also called "Adapters is difficult. I could imagine renaing Adapter -> "Provider" (or maybe anopther better word to remove this multi-use of "Adapter" for the SimpleBle/Noble level and keep Adapter for the "native level". WDYT? |
||
peripherals: Peripheral[]; | ||
pairedPeripherals: Peripheral[]; | ||
scanFor(ms: number): boolean; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please make this explicitly public