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

didUpdateValueForCharacteristic event failing #275

Open
shanith32 opened this issue May 24, 2022 · 3 comments
Open

didUpdateValueForCharacteristic event failing #275

shanith32 opened this issue May 24, 2022 · 3 comments

Comments

@shanith32
Copy link

We are working on a BLE app that acts as a Central and connects to a chip(peripheral). The peripheral contains a service and a characteristic and the characteristic contains a notify property. After calling peripheral.subscribeToCharacteristic() and peripheral.readValueForCharacteristic() methods, it subscribes successfully with the didUpdateNotificationStateForCharacteristics event, however errors out on the didUpdateValueForCharacteristic event. The error message is: 257: failed to initiate reading characteristic for peripheral name

@m1ga
Copy link
Contributor

m1ga commented May 24, 2022

searching for the error message I guess you are trying to implement it on Android, right?
Run your app with --log-level debug to see some more logs.

It should show you:

Log.d(LCAT, "readValueForCharacteristic(): characteristic- " + characteristicProxy.uuid()
						+ " read initiation status- ." + isReadInitiated);

(source)

with false as isReadInitiated. According to https://stackoverflow.com/a/30720262/5193915 it could be a timing issue. Did you have a look at the examples in https://github.com/tidev/appcelerator.ble/tree/master/example how they do the flow?

@shanith32
Copy link
Author

@m1ga Thank you for the above information. I ran a build with --log-level debug and found this,
[DEBUG] TiBleCentralOperationManager: readValueForCharacteristic(): characteristic- 4aa10003-d92d-48d3-b522-509b8524ac31 read initiation status- .false

We cloned the centralManager.js and peripheralChannel.js files from the example and nothing much is changed on them.

Here's where the error occurs on the peripheralChannel.js

alert('Error while didUpdateValueForCharacteristic' + e.errorCode + '/' + e.errorDomain + '/' + e.errorDescription);

The BLE chip(peripheral) that we are connecting to only has a notify property on its characteristic, so maybe calling peripheral.readValueForCharacteristic() on line

peripheral.readValueForCharacteristic({
is the problem. If that is the case would you be able to let me know which method to call/event to listen to access data streamed with a characteristic's notify property? Appreciate your help!

@m1ga
Copy link
Contributor

m1ga commented May 24, 2022

have a look at this code:

cmgr.addEventListener('didDiscoverPeripheral', function(e) {
	cmgr.connectPeripheral({
		peripheral: e.peripheral,
		options: {
			[BLE.CONNECT_PERIPHERAL_OPTIONS_KEY_NOTIFY_ON_CONNECTION]: true,
			[BLE.CONNECT_PERIPHERAL_OPTIONS_KEY_NOTIFY_ON_DISCONNECTION]: true
		}
	});
});

cmgr.addEventListener('didConnectPeripheral', function(e) {
	peripheral = e.peripheral;

	peripheral.addEventListener('didDiscoverServices', function(e) {
		var services = e.source.services;

		services.forEach(function(service) {
			if (service.uuid.toUpperCase() == 'DEVICE_UUID') {
				e.source.discoverCharacteristics({
					characteristics: [],
					service: service
				});
			}
		});

		peripheral.addEventListener('didDiscoverCharacteristics', function(e) {
			var peripheral = e.peripheral;
			var characteristics = e.service.characteristics;

			characteristics.forEach(function(characteristic) {

				function _didUpdateValueForCharacteristic(e) {}
				function _didReadValue(e) {}

				if (characteristic.uuid == 'CHAR_UUID') {
					global.charactersticObject = characteristic;

					peripheral.subscribeToCharacteristic({
						characteristic: characteristic,
						descriptorUUID: BLE.CBUUID_CLIENT_CHARACTERISTIC_CONFIGURATION_STRING,
						descriptorValue: BLE.ENABLE_NOTIFICATION_VALUE
					});

					peripheral.readValueForCharacteristic(characteristic);
					peripheral.addEventListener('didReadValueForCharacteristic', _didReadValue);
					peripheral.addEventListener('didUpdateValueForCharacteristic', _didUpdateValueForCharacteristic);
				}
			});
		});
		peripheral.addEventListener('didUpdateValueForCharacteristic', function(e) {});

		peripheral.discoverServices();
	});
});

cmgr.startScan();

I had that in an example file. That should be the correct flow

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