More noob BLE Questions, how do you setup a BLE Notification?

Alright, so thank you so far for getting this far! I have my nordic dongle flashed and it seems to work... I think. I'm trying to read a Kinsa thermometer with the simulator.

Here's the problem: In nordic connect, I can just push "Notify" and Nordic connect gets a notification every time I take a temperature. How do I do that in code?

Here's the report from Nordic Connect:

Here's my BLE Delegate:

using Toybox.Application;
using Toybox.WatchUi;
using Toybox.BluetoothLowEnergy;

class BleDelegate extends BluetoothLowEnergy.BleDelegate {
	private var blueEarView;
	private var kinsaUuid = BluetoothLowEnergy.stringToUuid("00000000-0068-746C-6165-4861736E694B");
	private var temperatureServiceUuid = BluetoothLowEnergy.stringToUuid("00000002-0068-746C-6165-4861736E694B");
	private var kinsaProfileDef = {
		:uuid => kinsaUuid,
		:characteristics => [{
			:uuid => temperatureServiceUuid,
			:descriptors => [BluetoothLowEnergy.cccdUuid()]
		}]
	};
	private var device;
	

 	function initialize(view){
 		BleDelegate.initialize();
 		blueEarView = view;
 		registerProfiles();
 		BluetoothLowEnergy.setScanState(BluetoothLowEnergy.SCAN_STATE_SCANNING);
		System.println("BleDelegate - initialize()");
 	}
 
	function onScanResults(scanResults) {
		System.println("BleDelegate - onScanResults() scanResults" + scanResults);
		for (var result = scanResults.next(); result != null; result = scanResults.next()) {
			var iter = result.getServiceUuids();
			for (var uuid = iter.next(); uuid != null; uuid = iter.next()) {
				System.println("found uuid:" + uuid);
				if (kinsaUuid.equals(uuid)) {
					System.println("attemping connection....");
					BluetoothLowEnergy.setScanState(BluetoothLowEnergy.SCAN_STATE_OFF);
					BluetoothLowEnergy.pairDevice(result);
				}
			}
		}
	}
	
	function onConnectedStateChanged(device, state) {
		System.println("BleDelegate - onConnectedStateChanged() device:" + device.getName());
		var services = device.getServices();
		for (var service = services.next(); service != null; service = services.next()) {
			System.println("service:" + service.getUuid());
			var characteristic = service.getCharacteristic(temperatureServiceUuid);
			characteristic.requestRead();
			characteristic.
		}
	}
	
	function onCharacteristicRead(characteristic, status, value) {
		System.println("BleDelegate - onCharacteristicRead() value:" + value);
		characteristic.requestRead();
	}
 

	function registerProfiles() {
		BluetoothLowEnergy.registerProfile(kinsaProfileDef);
		System.println("KinsaPM - registerProfiles()");
	}
}

Here's the output

KinsaPM - registerProfiles()
BleDelegate - initialize()
BleDelegate - onScanResults() scanResultsObj: 167
BleDelegate - onScanResults() scanResultsObj: 167
BleDelegate - onScanResults() scanResultsObj: 167
found uuid:00000000-0068-746C-6165-4861736E694B
attemping connection....
BleDelegate - onConnectedStateChanged() device:null
service:00000000-0068-746C-6165-4861736E694B
BleDelegate - onCharacteristicRead() value:[7, 1]

First, it takes forever to read the characteristic, whereas Nordic connect gets notified immediately. Second, 7,1 is a magic number that means no temperature, whereas nordic connect receives a value. Argh, what am I doing wrong?

  • Here's the basics to turn on notify (you can find something similar in the NordicThingy52 sample)

    char is the characteristic;

    var cccd = char.getDescriptor(BluetoothLowEnergy.cccdUuid());
    cccd.requestWrite([0x01,0x00]b);

    You'll then get the data by way of

    onCharacteristicChanged()

    Not all characteristics support notify so you may want to check on that..

  • AH YES IT VWERXXXX!!! It's still not instantaneous but it's much quick AND the reading is now bringing back the encoded temperature

  • What I typically do is that I issue a notify for a characteristic, and then do a read of the same characteristic.

    Whichever happens first, I see first.

    In the case of the Thing52, one of the characteristics only notifies every 10 seconds, so this way I see the result of the read before notifies start and have data sooner.

    And remember to queue requests!  Don't do two reads until you've heard back on the first