IanHarvey / bluepy

Python interface to Bluetooth LE on Linux
Other
1.61k stars 490 forks source link

Multiple Instances of a single Service #490

Open thok-itx opened 2 years ago

thok-itx commented 2 years ago

Dear BluePy Team,

I have encountered problems while extracting services from my local device. The local device is using multiple instances of the same service (e.g. Temperature) as it could perform multiple measurements.

I think this is currently not supported within the bluepy library. I tried to dig a little bit into the code. I observed that internally the services are stored within a _serviceMap that is indexed directly by the UUID:

(from bluepy/btle.py)

    def discoverServices(self):
        self._writeCmd("svcs\n")
        rsp = self._getResp('find')
        starts = rsp['hstart']
        ends   = rsp['hend']
        uuids  = rsp['uuid']
        nSvcs = len(uuids)
        assert(len(starts)==nSvcs and len(ends)==nSvcs)
        self._serviceMap = {}
        for i in range(nSvcs):
            self._serviceMap[UUID(uuids[i])] = Service(self, uuids[i], starts[i], ends[i])
        return self._serviceMap

Hence, if multiple instances exists with the same UUID, only the last instance will be reported by the bluepy library. This is also what I observed locally.

Unfortunately I don't have experience to judge if this can be changed easily. I guess it would be required to index the serviceMap with the handle of that service instead of the UUID itself, as the handle will be unique. At a fist glance, two methods would be affected if the _serviceMap would be indexed differently: def discoverServices(self): and def getServiceByUUID(self, uuidVal):.

Best Regards, Thomas