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

Example how to setup #28

Open
focussing opened this issue Sep 8, 2017 · 2 comments
Open

Example how to setup #28

focussing opened this issue Sep 8, 2017 · 2 comments

Comments

@focussing
Copy link

I am struggling to implement the following

  • central manager finds peripherals
  • I connect to the peripheral I want
[INFO] :   didDiscoverServices
[INFO] :   {
[INFO] :       bubbles = 1;
[INFO] :       cancelBubble = 0;
[INFO] :       error = "<null>";
[INFO] :       peripheral = "[object TiBluetoothPeripheral]";
[INFO] :       source = "[object TiBluetoothPeripheral]";
[INFO] :       type = didDiscoverServices;
[INFO] :   }
[INFO] :   [object TiBluetoothPeripheral]
[INFO] :   (
[INFO] :       "[object TiBluetoothService]",
[INFO] :       "[object TiBluetoothService]"
[INFO] :   )
[INFO] :   peripheral has  2 service(s)
[INFO] :   service characteristics  <null>
[INFO] :   service characteristics  <null>

Next I want to get the UUIDs from the services, and subscribe to let's say one of them
My code so far:

	centralManager.addEventListener('didConnectPeripheral', function(e) {
		console.log('\ndidConnectPeripheral');
		console.log(e);
		console.log(e.peripheral);

		centralManager.stopScan();
		connectedPeripheral = e.peripheral;

		connectedPeripheral.addEventListener('didDiscoverServices', function(e) {
			console.log('\ndidDiscoverServices');
			console.log(e);
			console.log(e.peripheral);
			console.log(e.peripheral.services);

			console.log('peripheral has ', e.peripheral.services.length, 'service(s)');
			var services = e.peripheral.services;

			for (var i = 0; i < services.length; i++)
				console.log('service characteristics ', services[i].characteristics.uuid);
			// if (service.UUID.toLowerCase() == CUSTOM_SERVICE_UUID.toLowerCase()) {
			// e.peripheral.discoverCharacteristicsForService(service);
			// }
		});

		connectedPeripheral.addEventListener('didDiscoverCharacteristicsForService', function(e) {
			console.log('didDiscoverCharacteristicsForService');
			console.log(e);
		});

		connectedPeripheral.addEventListener('didUpdateValueForCharacteristic', function(e) {
			console.log('didUpdateValueForCharacteristic');
			console.log(e);
		});

		connectedPeripheral.discoverServices();
	});
@wsliaw
Copy link
Contributor

wsliaw commented Sep 8, 2017

You have to add the event listener on the latest peripheral you get

centralManager.addEventListener('didConnectPeripheral', function(e) {
	console.log('\ndidConnectPeripheral');
	console.log(e);
	console.log(e.peripheral);

	centralManager.stopScan();
	connectedPeripheral = e.peripheral;

	connectedPeripheral.addEventListener('didDiscoverServices', function(e) {
		console.log('\ndidDiscoverServices');
		console.log(e);
		console.log(e.peripheral);
		console.log(e.peripheral.services);

		var peripheral = e.peripheral;

		peripheral.addEventListener('didDiscoverCharacteristicsForService', function(e) {
			console.log('didDiscoverCharacteristicsForService');
			console.log(e);
		});

		peripheral.services.forEach(function(service) {
			peripheral.discoverCharacteristicsForService({ service: service });
		});
	});

	connectedPeripheral.discoverServices();
});

@focussing
Copy link
Author

I am doing that, please take a look at #23

I updated that issue today.

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

No branches or pull requests

2 participants