How to get different characteristic UUIDs (distance, calories, heart rate, etc) from the connected BLE device

37 views Asked by At

I am trying to display the characteristic values like distance, heart rate, calories from the connected BLE device. Here is the code:

 const connectToDevice = async (device: Device) => {
        try {
            if (device.name?.includes('EW')) {
                const deviceConnection = await bleManager.connectToDevice(device.id);
                console.log("Connected...Discovering services and characteristics");
                await deviceConnection.discoverAllServicesAndCharacteristics(device.id);
                console.log('Services and characteristics discovered');
                const services = await deviceConnection.services();
                console.log('Discovered services:', services.values);

                for (const service of services) {
                    console.log('Service UUID:', service.uuid);
                    const heartRateCharacteristic = await deviceConnection.characteristicsForService(
                        service.uuid,
                    );
                    console.log(`heart rate characteristics for service ${service.id}:`, heartRateCharacteristic)
                    const characteristics = await service.characteristics();
                    console.log(`Characteristics for service ${service.id}:`, characteristics);

                    for (const characteristic of heartRateCharacteristic) {
                        console.log(`Characteristic UUID: ${characteristic.uuid}`);
                    }
                    console.log('Read Characteristic:', heartRateCharacteristic);
                }

                for (const service of services) {

                    const characteristics = await service.characteristics();
                    console.log(`Characteristics for service ${service.id}:`, characteristics);

                    const heartRateCharacteristic = await deviceConnection.characteristicsForService(
                        service.uuid,  // Use the UUID of the service
                    );
                    const heartRateValueCharacteristic = heartRateCharacteristic.find(
                        characteristic => characteristic.uuid === HEART_RATE_UUID3
                    );
                    if (heartRateValueCharacteristic) {

                        const heartRateValue = await deviceConnection.readCharacteristicForService(
                            service.uuid,
                            heartRateValueCharacteristic.uuid
                        );

                        console.log('Heart Rate Value:', heartRateValue.value);
}
}
}

I get the below result and I couldn't figure out which one is for which and how to decode each of them. Here is the output for my service UUIDs

LOG Service UUID: 0000fff0-0000-1000-8000-00805f9b34fb LOG heart rate characteristics for service 9: [{"_manager": {"_activePromises": [Object], "_activeSubscriptions": [Object], "_errorCodesToMessagesMapping": [Object], "_eventEmitter": [NativeEventEmitter], "_scanEventSubscription": [Object], "_uniqueId": 11}, "deviceID": "D3:8F:7A:AC:6D:53", "id": 10, "isIndicatable": false, "isNotifiable": true, "isNotifying": false, "isReadable": false, "isWritableWithResponse": false, "isWritableWithoutResponse": false, "serviceID": 9, "serviceUUID": "0000fff0-0000-1000-8000-00805f9b34fb", "uuid": "0000fff1-0000-1000-8000-00805f9b34fb", "value": null}, {"_manager": {"_activePromises": [Object], "_activeSubscriptions": [Object], "_errorCodesToMessagesMapping": [Object], "_eventEmitter": [NativeEventEmitter], "_scanEventSubscription": [Object], "_uniqueId": 11}, "deviceID": "D3:8F:7A:AC:6D:53", "id": 12, "isIndicatable": false, "isNotifiable": false, "isNotifying": false, "isReadable": false, "isWritableWithResponse": true, "isWritableWithoutResponse": true, "serviceID": 9, "serviceUUID": "0000fff0-0000-1000-8000-00805f9b34fb", "uuid": "0000fff2-0000-1000-8000-00805f9b34fb", "value": null}] LOG Characteristics for service 9: [{"_manager": {"_activePromises": [Object], "_activeSubscriptions": [Object], "_errorCodesToMessagesMapping": [Object], "_eventEmitter": [NativeEventEmitter], "_scanEventSubscription": [Object], "_uniqueId": 12}, "deviceID": "D3:8F:7A:AC:6D:53", "id": 10, "isIndicatable": false, "isNotifiable": true, "isNotifying": false, "isReadable": false, "isWritableWithResponse": false, "isWritableWithoutResponse": false, "serviceID": 9, "serviceUUID": "0000fff0-0000-1000-8000-00805f9b34fb", "uuid": "0000fff1-0000-1000-8000-00805f9b34fb", "value": null}, {"_manager": {"_activePromises": [Object], "_activeSubscriptions": [Object], "_errorCodesToMessagesMapping": [Object], "_eventEmitter": [NativeEventEmitter], "_scanEventSubscription": [Object], "_uniqueId": 12}, "deviceID": "D3:8F:7A:AC:6D:53", "id": 12, "isIndicatable": false, "isNotifiable": false, "isNotifying": false, "isReadable": false, "isWritableWithResponse": true, "isWritableWithoutResponse": true, "serviceID": 9, "serviceUUID": "0000fff0-0000-1000-8000-00805f9b34fb", "uuid": "0000fff2-0000-1000-8000-00805f9b34fb", "value": null}] LOG Characteristic UUID: 0000fff1-0000-1000-8000-00805f9b34fb LOG Characteristic UUID: 0000fff2-0000-1000-8000-00805f9b34fb

Can anybody guide me through this?

0

There are 0 answers