titanium-bluetooth icon indicating copy to clipboard operation
titanium-bluetooth copied to clipboard

Example how to setup

Open focussing opened this issue 8 years ago • 2 comments

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();
	});

focussing avatar Sep 08 '17 11:09 focussing

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();
});

wsliaw avatar Sep 08 '17 13:09 wsliaw

I am doing that, please take a look at https://github.com/hansemannn/titanium-bluetooth/issues/23

I updated that issue today.

focussing avatar Sep 08 '17 13:09 focussing