Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

How to get list of devices?? #57

Open
DrCWO opened this issue Jun 20, 2023 · 1 comment
Open

How to get list of devices?? #57

DrCWO opened this issue Jun 20, 2023 · 1 comment

Comments

@DrCWO
Copy link

DrCWO commented Jun 20, 2023

Hi,
first thank you for making node-ble. Hope it will help me connecting to my devices 👍
I have a question but maybe it is also an issue, I don't know.

I like to start discovery and get the devices discovered so I can decide to which one I like to connect. In my example below I use myAdapter.devices() but is only returns an empty object. Any ideas?

I have bluetoothctl open at the same time and see that discovery starts. I also see the device announcing its UUID after some time but my code shows nothing.

Here my code:

'use strict';

async function main() {
	const { createBluetooth } = require('node-ble');
	const { bluetooth, destroy } = createBluetooth();
	var myAdapter = await bluetooth.defaultAdapter();

	console.log(myAdapter.adapter);

	if (! await myAdapter.isDiscovering()) {
		console.log ("startDiscovery");
		myAdapter.startDiscovery();
	}

	await myAdapter.isDiscovering();
	console.log ('isDiscovering');


	showDevices();

	function showDevices () {
		console.log (JSON.stringify(myAdapter.devices(), null,2));
		setTimeout (showDevices, 2000);
	}
}

main();

I can connect to one of my devices using its UUID (see working code blow). But before being able to do so I first have to find out the UUID which I expected to get with myAdapter.devices().

	const device = await adapter.waitDevice('70:BC:10:86:85:C5');
	await device.connect();

Support would be appreciated.
Best DrCWO

@DrCWO
Copy link
Author

DrCWO commented Jun 20, 2023

Hi again, found the solution by myself. Here the code that works...

'use strict';

async function main() {

	const { createBluetooth } = require('node-ble');
	const { bluetooth, destroy } = createBluetooth();
	var myAdapter = await bluetooth.defaultAdapter();

	console.log(myAdapter.adapter);

	if (! await myAdapter.isDiscovering()) {
		console.log ("startDiscovery");
		myAdapter.startDiscovery();
	}

	await myAdapter.isDiscovering();
	console.log ('isDiscovering');

	showDevices();

	async function showDevices () {
		console.log ('------------------------------');
		var myDevices = await myAdapter.devices();
		console.log (myDevices);
		setTimeout (showDevices, 2000);
	}
}

main();

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

No branches or pull requests

1 participant