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

First 1.1.0 test results (iPhone)

Open focussing opened this issue 8 years ago • 22 comments

Tested the new 1.1.0 release. Main issue is that no reporting of UUIDs is done when reporting discovered peripherals, services and characteristics. It discovers my Bluetooth device (VB-1913...), and connects to this device. When the device is connected it finds it's 1 service, and finds that this service has characteristic(s). However it doesn't report which characteristics.

In order to use this module in an application, the control by specific UUIDs is needed.

[INFO] :   didDiscoverPeripheral
[INFO] :   VB-19133423006
[INFO] :   connecting
[INFO] :   
[INFO] :   didConnectPeripheral
[INFO] :   {
[INFO] :       bubbles = 1;
[INFO] :       cancelBubble = 0;
[INFO] :       peripheral = "[object TiBluetoothPeripheral]";
[INFO] :       source = "[object TiBluetoothCentralManager]";
[INFO] :       type = didConnectPeripheral;
[INFO] :   }
[INFO] :   [object TiBluetoothPeripheral]
[INFO] :   
[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] :   )
[INFO] :   peripheral has  1 service(s)
[INFO] :   service characteristics  (
[INFO] :   )
        /**
	 * Central Manager Events
	 */

	centralManager.addEventListener('didUpdateState', function(e) {
		console.log('didUpdateState');

		switch (e.state) {
		case BLE.MANAGER_STATE_RESETTING:
			console.log('Resetting');
			break;

		case BLE.MANAGER_STATE_UNSUPPORTED:
			console.log('Unsupported');
			break;

		case BLE.MANAGER_STATE_UNAUTHORIZED:
			console.log('Unauthorized');
			break;

		case BLE.MANAGER_STATE_POWERED_OFF:
			console.log('Powered Off');
			break;

		case BLE.MANAGER_STATE_POWERED_ON:
			console.log('Powered On');
			centralManager.startScan();
			break;

		case BLE.MANAGER_STATE_UNKNOWN:
		default:
			console.log('Unknown');
			break;
		}
	});

	centralManager.addEventListener('didDiscoverPeripheral', function(e) {
		console.log('didDiscoverPeripheral');
		console.log(e.peripheral.name);
		if ((e.peripheral.name != null) && e.peripheral.name.indexOf('VB-') != -1) {
			console.log('connecting');
			var options = {
				notifyOnConnection : true,
				notifyOnDisconnection : true,
				notifyOnNotification : true
			};
			centralManager.connectPeripheral(e.peripheral);
		}
	});

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

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

	centralManager.addEventListener('didDisconnectPeripheral', function(e) {
		console.log('didDisconnectPeripheral');
		console.log(e);
	});

	centralManager.addEventListener('willRestoreState', function(e) {
		console.log('willRestoreState');
		console.log(e);
	});

	centralManager.addEventListener('didFailToConnectPeripheral', function(e) {
		console.log('didFailToConnectPeripheral');
		console.log(e);
	});

focussing avatar Jun 16 '17 13:06 focussing

Hey there, thanks for the demo-code and logs. I am curious if this a module problem, here is why: We basically wrap all native API's, but especially on events, we don't do any other business-logic. See some details here:

  • We fire the event here
  • We wrap the native peripheral here
  • We return the services here
  • We return the service-characteristics here

And that's basically it. If there are characteristics, there are wrapped here, otherwise, an empty array is returned. Not sure how to debug this, since I do not have a proper Bluetooth-device with me right now.

hansemannn avatar Jun 16 '17 15:06 hansemannn

Thanks Hans, I am not "at home" in iOS source code, but I can see what is going on. Sorry for that. I have tested again with the new release 1.1.1 released this morning. Still I can't get any UUID information in the console.

Also firing discoverCharacteristicsForService does not result in a didDiscoverCharacteristicsForService event being fired. Maybe the call is not correct; I can't derive that from the source code nor the Wiki.

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

		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);
				e.peripheral.discoverCharacteristicsForService({
					characteristics: [],
					service : services[i]
				});
			}
		});

		// if (service.UUID.toLowerCase() == CUSTOM_SERVICE_UUID.toLowerCase()) {

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

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

		connectedPeripheral.discoverServices();
	});

The log:

[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>

focussing avatar Jun 20 '17 13:06 focussing

I know @wsliaw recently did a lot with the module, maybe he can help?

hansemannn avatar Jun 20 '17 13:06 hansemannn

  1. For now the module hasn't expose uuid for service & characteristic
  2. Try move didDiscoverCharacteristicsForService & didUpdateValueForCharacteristic listeners inside didDiscoverServices callback
centralManager.addEventListener('didConnectPeripheral', function(e) {
	connectedPeripheral = e.peripheral;

	connectedPeripheral.addEventListener('didDiscoverServices', function(e) {

		var p = e.peripheral;

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

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

		p.services.forEach(function(service) {
			p.discoverCharacteristicsForService({
				characteristics: [],
				service: service
			});
		});

	});

	connectedPeripheral.discoverServices();

});

wsliaw avatar Jun 20 '17 13:06 wsliaw

Thanks! didDiscoverCharacteristicsForService is now fired. Maybe the example should be updated with this.

Could the module be updated so the UUID's are exposed for service & characteristic and also for peripheral? This is needed when building an app where multipe UUID's are present.

Another question; how can I send a UINT64 value (byte array length 8) over Bluetooth? Javascript does not support > 53bits accuracy. If I create a Ti.Buffer, should I use Ti.Codec.TYPE_BYTE and fill the buffer bytes with the byteArray bytes?

Thanks!!

focussing avatar Jun 20 '17 13:06 focussing

Yea, I think I'll make a PR for that

This is how I do it:

e.service.characteristics.forEach(function(characteristic) {
	p.writeValueForCharacteristicWithType(toBlob('hello world, I\'m ' + Ti.Platform.username), characteristic, BLE.CHARACTERISTIC_PROPERTY_WRITE_WITH_RESPONSE);
});

function toBlob(str) {
	var buf = Ti.createBuffer({ value: str });
	return buf.toBlob();
}

wsliaw avatar Jun 20 '17 13:06 wsliaw

PR ready for the UUID properties, 1.1.2 on the way.

hansemannn avatar Jun 20 '17 14:06 hansemannn

I just see that the didDiscoverCharacteristicsForService is only fired once, yet there are 2 services having 5 characteristics each.

This is the log when I use the BLE module from Logicallabs

[INFO] :   16:02:13 (208) peripheral has 2 services
[INFO] :   16:02:13 (209) service UUID: 180A
[INFO] :   16:02:13 (211) service UUID: 500AAE11-CDEC-C0AE-5FD5-B10E85910100
[INFO] :   16:02:13 (217) entering digestCharacteristics for service 180A
[INFO] :   16:02:13 (218) service has 5 characteristic(s).
[INFO] :   16:02:13 (220) characteristic: 2A29
[INFO] :   16:02:13 (221) characteristic: 2A24
[INFO] :   16:02:13 (221) characteristic: 2A28
[INFO] :   16:02:13 (222) characteristic: 2A26
[INFO] :   16:02:13 (223) characteristic: 2A25
[INFO] :   16:02:13 (224) entering digestCharacteristics for service 500AAE11-CDEC-C0AE-5FD5-B10E85910100
[INFO] :   16:02:13 (224) service has 5 characteristic(s).
[INFO] :   16:02:13 (225) characteristic: 500AAE11-CDEC-C0AE-5FD5-B10E8591010A
[INFO] :   16:02:13 (226) characteristic: 500AAE11-CDEC-C0AE-5FD5-B10E8591010B
[INFO] :   16:02:13 (227) characteristic: 500AAE11-CDEC-C0AE-5FD5-B10E85910110
[INFO] :   16:02:13 (228) subscribing to characteristic 500AAE11-CDEC-C0AE-5FD5-B10E85910110
[INFO] :   16:02:13 (229) characteristic: 500AAE11-CDEC-C0AE-5FD5-B10E85910120
[INFO] :   16:02:13 (230) characteristic: 500AAE11-CDEC-C0AE-5FD5-B10E85910130

focussing avatar Jun 20 '17 14:06 focussing

Release 1.1.2: https://github.com/hansemannn/titanium-bluetooth/releases/tag/ios-v1.1.2

hansemannn avatar Jun 20 '17 14:06 hansemannn

@focussing so what did you get from ti.bluetooth ? I never advertise 2 services from a peripheral before

wsliaw avatar Jun 20 '17 14:06 wsliaw

The Bluetooth device I am using advertises 2 services. Ti.Bluetooth sees that, so per service a "didDiscoverCharacteristicsForService" has to be done.

I am testing with 1.1.2 version now, but I don't get uuid reported, for example from didDiscoverPeripheral.

	centralManager.addEventListener('didDiscoverPeripheral', function(e) {
		console.log('didDiscoverPeripheral');
		console.log(e.peripheral.name);
		console.log(e.peripheral.uuid);
		if ((e.peripheral.name != null) && e.peripheral.name.indexOf('SQ__') != -1) {
			console.log('connecting');
			var options = {
				notifyOnConnection : true,
				notifyOnDisconnection : true,
				notifyOnNotification : true
			};
			centralManager.connectPeripheral(e.peripheral);
		}
	});

Also didDiscoverCharacteristicsForService is not fired.

focussing avatar Jun 20 '17 14:06 focussing

@focussing Peripherals do not have a UUID, they are identified via their name and services. The connectPeripheral looks correctly.

hansemannn avatar Jun 21 '17 06:06 hansemannn

True! Of course you are right. Sorry!

This is the code and log. The only thing right now is that only the first of 2 service's characteristics are discovered:

	centralManager.addEventListener('didDiscoverPeripheral', function(e) {
		console.log('didDiscoverPeripheral');
		console.log(e.peripheral.name);

		// if ((e.peripheral.name != null) && e.peripheral.name.indexOf('SQ') == 0) {
		console.log('connecting');
		var options = {
			notifyOnConnection : true,
			notifyOnDisconnection : true,
			notifyOnNotification : true
		};
		centralManager.connectPeripheral(e.peripheral);
		// }
	});

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

		var connectedPeripheral = e.peripheral;

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

			var p = e.peripheral;

			console.log('peripheral has', p.services.length, 'service(s)');

			p.addEventListener('didDiscoverCharacteristicsForService', function(e) {
				console.log('\ndidDiscoverCharacteristicsForService');
				console.log('service:', e.service.uuid);

				var characteristics = e.service.characteristics;
				characteristics.forEach(function(characteristic) {
					console.log('characteristic:', characteristic.uuid);
				});
			});

			p.addEventListener('didUpdateValueForCharacteristic', function(e) {
				console.log('\ndidUpdateValueForCharacteristic');
				console.log(e);
			});

			p.services.forEach(function(service) {
				console.log('service:', service.uuid);
				console.log('calling discoverCharacteristicsForService');
				p.discoverCharacteristicsForService({
					characteristics : [],
					service : service
				});
			});

		});

		connectedPeripheral.discoverServices();
	});

[INFO] :   ti.bluetooth/0.1.0 (6.1.0.6eb8990)
[INFO] :   didUpdateState
[INFO] :   Powered On
[INFO] :   didDiscoverPeripheral
[INFO] :   SQ__859501025
[INFO] :   connecting
[INFO] :   
[INFO] :   didConnectPeripheral
[INFO] :   connected to SQ__859501025
[INFO] :   
[INFO] :   didDiscoverServices
[INFO] :   peripheral has 2 service(s)
[INFO] :   service: 180A
[INFO] :   calling discoverCharacteristicsForService
[INFO] :   service: 500AAE11-CDEC-C0AE-5FD5-B10E85910100
[INFO] :   calling discoverCharacteristicsForService
[INFO] :   
[INFO] :   didDiscoverCharacteristicsForService
[INFO] :   service: 180A
[INFO] :   characteristic: 2A29
[INFO] :   characteristic: 2A24
[INFO] :   characteristic: 2A28
[INFO] :   characteristic: 2A26
[INFO] :   characteristic: 2A25

focussing avatar Jun 21 '17 07:06 focussing

Maybe because you are using discoverCharacteristicsForService directly after the first service is found, so iOS might guess you are not waiting for others to be discovered? Maybe one of the CoreBluetooth example projects will help you comparing it to the native world, the API naming was pretty much 1:1 aligned to the native one.

hansemannn avatar Jun 21 '17 07:06 hansemannn

@focussing consider discoverServices with UUIDs

connectedPeripheral.discoverServices([serviceUUID_1, serviceUUID_2]);

Please tell me what's the result, thank you

wsliaw avatar Jun 21 '17 09:06 wsliaw

Closing due to inactivity. Feel free to reopen if the issue persists.

hansemannn avatar Aug 16 '17 08:08 hansemannn

@wsliaw your suggestion gives an error message.

connectedPeripheral.discoverServices(['49535343-FE7D-4AE5-8FA9-9FAFD205E455']);

Normally do don't know the service UUID on forehand. For a new Bluetooth module, first you list the services and UUID's in the console. And then you'll want to get the Characteristics per Service, and start listening or sending data to specific Characteristics.

focussing avatar Sep 08 '17 11:09 focussing

By the way, off course I switched to the latest release of this module 1.2.1

Getting the same result as earlier:

[INFO] :   didConnectPeripheral
[INFO] :   connected to RN4678-1CD8
[INFO] :   
[INFO] :   didDiscoverServices
[INFO] :   peripheral has 2 service(s)
[INFO] :   service: 180A
[INFO] :   calling discoverCharacteristicsForService
[INFO] :   service: 49535343-FE7D-4AE5-8FA9-9FAFD205E455
[INFO] :   calling discoverCharacteristicsForService
[INFO] :   
[INFO] :   didDiscoverCharacteristicsForService
[INFO] :   service: 180A
[INFO] :   characteristic: 2A29
[INFO] :   characteristic: 2A24
[INFO] :   characteristic: 2A25
[INFO] :   characteristic: 2A27
[INFO] :   characteristic: 2A26
[INFO] :   characteristic: 2A28
[INFO] :   characteristic: 2A23
[INFO] :   characteristic: 2A2A

focussing avatar Sep 08 '17 12:09 focussing

These characteristics are common ones, giving information about manufacturer etc. The characteristics of the second service is much more interesting :)

focussing avatar Sep 08 '17 12:09 focussing

@focussing So what exact event / method is not working? If you compare it to some Swift examples, it should be nearly 1:1 the name API naming, so that might lead us to the related issue here. Thanks for keeping this active!

hansemannn avatar Sep 08 '17 13:09 hansemannn

Reopening to keep track about the process. ping @focussing

hansemannn avatar Sep 19 '17 21:09 hansemannn

Thanks to @wsliaw, I just fixed an issue regarding peripherals in 1.2.2 (Download), please check it out!

hansemannn avatar Sep 21 '17 12:09 hansemannn