Skip to content
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

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

Apollon77
Copy link
Contributor

@Apollon77 Apollon77 commented Apr 20, 2024

This PR adds the option to query the available "Hardware BLE Adapters" (I did not wanted to say USB sticks because it could also be UART, so if you have a better name that do not conflict with "Adapter" already used just say :-) ) and define which one to use.

The Constructor of the WebBluetooth class is used to allow to specify the hadware adapter to use as optional option. If the option is not defined it will initialize the adapter HW with id 0 as before too laziely on first real use.

Additionally the method "getSimpleBleHardwareAdapters()" is exported and can be used. I added an example to show the usage. It outputs the following on my raspi

[ { identifier: 'hci0', address: 'DC:A6:32:3E:E5:EF', active: false } ]

fixes #178

Define a Information only interface for a Hardware Adapter. These info should also be available if other adapter-libs are added.
Enhance the "Adapter" interface with methods to get and define he HW Adapter to use
Adds the HW Adapter Index as option to the constructor like other "non Webbluetooth spec" options and set this.
In order to allow getting list of adapter we expose a "adapter" specific method.
If you have a better idea just tell me, but for me this was the one that fits the "mostly compatible webbbluetooth interface" the best.
This was referenced Apr 20, 2024
@Symbitic
Copy link
Contributor

Is HardwareAdapter better than just Adapter? I know there's an internal adapter class, but since users don't see that, I'm wondering if the shorter getAdapters() might be better. It also might be a good idea to throw an error if the user tries to change the adapter while scanning is active.

Other than that, I like this idea.

@Apollon77
Copy link
Contributor Author

The naming is exactly one topic to discuss. You are right with "internal iuse of Adapter vs external use" - in fact I also thought that for library devs it should be clear and there Adapter is highly overlapping. But I'm very open to change. @thegecko WDYT?

For your secondcomment: In fact the code shoud throw on "useHardwareAdapter" call as soon as "this.adapter" is set already. This happens in

this.adapter = getAdapters()[0];
... so it should already be as you proposed.

Copy link
Owner

@thegecko thegecko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR @Apollon77, I like the approach.

I've made a few minor comments, but agree with @Symbitic that calling the interface Adapter may be better. This may need you to merge the adapter interfaces.

@@ -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() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
getHardwareAdapters() {
public getHardwareAdapters() {

Please make this explicitly public

@@ -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(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this is to reduce the exposed fields?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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: string;
address: string;
active: boolean;
export interface Adapter extends HardwareAdapterDetails {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth merging these types?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

@Apollon77 Apollon77 Apr 29, 2024

Choose a reason for hiding this comment

The 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?

);
}

useHardwareAdapter(adapterIndex: number) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
useHardwareAdapter(adapterIndex: number) {
public useHardwareAdapter(adapterIndex: number) {

Please make this explicitly public

@Apollon77
Copy link
Contributor Author

I will try to update here with all comments from above the next days.

@thegecko
Copy link
Owner

I will try to update here with all comments from above the next days.

Any progress @Apollon77 ?

@Apollon77
Copy link
Contributor Author

did not found time yet, but your comment pushed it up in the queue gg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow to choose/configure the used Bluetooth adapter
3 participants