简体   繁体   中英

BlueZ example gatt server

I'm learning about BlueZ and BLE. In the bluez/test directory there is a python 'example gatt server'. I've read the code and almost understood how it works, but something is not clear. For example there's this method which notifies when a characteristic's value changes (in this example is the battery level value):

    def notify_battery_level(self):
    if not self.notifying:
        return
    self.PropertiesChanged(
            GATT_CHRC_IFACE,
            { 'Value': [dbus.Byte(self.battery_lvl)] }, [])

The method PropertiesChanged is so defined:

@dbus.service.signal(DBUS_PROP_IFACE,
                     signature='sa{sv}as')
def PropertiesChanged(self, interface, changed, invalidated):
    pass

I'm starting advertising and running this example, then i'm using the BLE scanner app on my smartphone for reading the characteristic (the battery level in this case). Well, the self.battery_lvl is an integer number (24 for example), but on ble scanner as the value of characteristic i see "24 % battery level". The problem is that nowhere in the code is added '% battery level' to the self.battery_lvl (simply 24). So where are this additional informations added? Sorry for my english. If you need additional informations please tell me

The battery level example uses the Bluetooth service adopted UUID (0x180F) and attribute format (Please see this ). As such, when the mobile phone app finds the service and sees the UUID, it will know that this is the battery service, and add the % when reading the value. This is a major advantage when using adopted services/characteristics vs custom ones, you can add intelligence at the remote end because the local end has a predefined format. Please note that this also applies to adopted heart rate service/characteristic (the app will immediately add bpm), adopted temperature service/characteristic (the app will immediately add (°C), and so on.

For a full list of adopted services and characteristics, please see these links:-

For further reading about this subject:-

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM