• Hello, I'm trying to implement the Bluetooth SIG Battery Service but it's very complicated and there's absolutely no help in terms of example available online. I'm not trying to implement all of the characteristics in that service only battery_level & battery_level_status. Implementing is very confusing because the documentation provided by bluetooth doesn't match any of the implementations you can observe on github. Even in NRF's own implementation of the specification the battery service only has two characteristics as opposed to the 5 provided by the specification. Also apparently you're supposed to combine the unit for a value with the actual value somehow which I don't understand and I can't really find any references for it in the specification.

    Can someone please look over my implementation if it follows the specification correctly. For reference I am using Assigned Numbers Revision Date: 20230313 & GATT_Specification_Supplement_v8-1 as reference. I really appreciate the help, thank you in advance.

    let IS_CHARGING = false;
    // Globals
    let IS_CONNECTED;
    
    function onConnect() {
        IS_CONNECTED = true
    }
    
    function onDisconnect() {
        IS_CONNECTED = false
    }
    
    function onCharge(charging) {
        IS_CHARGING = charging
    }
    
    function build_battery_status(){
        const battery_level = E.getBattery()
        // 0 good, 1 bad
        let is_charge_good = battery_level > 80 ? true: false
        const battery_status_flags = 2;
        let power_state_flags = 1
        if(IS_CHARGING){
            // Charging
            power_state_flags = power_state_flags | 32
        }else{
            // Active Discharging
            power_state_flags = power_state_flags | 64
        }
        if(is_charge_good){
            // Good
            power_state_flags = power_state_flags | 128
        }else{
            // Bad
            power_state_flags = power_state_flags | 256
        }
        // I know this part is technically incorrect because battery_status_flags & 
        // battery_level should be 1 octet but since I don't know I don't understand 
        // the endianness(I know it's little endian) I have referained from encoding
        // it directly
        const encoded = new Uint16Array([battery_status_flags, power_state_flags, battery_level]).buffer
        return encoded
    }
    
    function update_battery_status(){
        NRF.updateServices({
            0x180F: {
                0x2A19: {
                    value: [E.getBattery()]
                },
                0x2BED: {
                    value:build_battery_status()
                },
            },
        })
    }
    
    function onInit() {
        Bangle.on('charging', onCharge);
        NRF.on('connect', onConnect);
        NRF.on('disconnect', onDisconnect);
        NRF.setServices({
            // Battery level service
            0x180F: {
                0x2A19: {
                    notify: true,
                    readable: true,
                    indicate: true,
                    description: "Battery Level",
                    value: [E.getBattery()]
                },
                0x2BED: {
                    notify: true,
                    readable: true,
                    indicate: true,
                    description: "Battery Status",
                    value:build_battery_status()
                },
            }
        })
    
        setInterval(update_battery_status, 40_000)
    }