gattlib-py/gattlib/device.py: Add support to retrieve advertisement data when device not connected

pull/169/head
Olivier Martin 2020-04-09 11:39:18 +02:00 committed by Olivier Martin
parent f18dd02530
commit a34a2d2a21
1 changed files with 20 additions and 12 deletions

View File

@ -27,7 +27,7 @@ class Device:
else:
self._addr = addr
self._name = name
self._connection = c_void_p(None)
self._connection = None
# Keep track if notification handler has been initialized
self._is_notification_init = False
@ -41,7 +41,10 @@ class Device:
@property
def connection(self):
return self._connection
if self._connection:
return self._connection
else:
return c_void_p(None)
def connect(self, options=CONNECTION_OPTIONS_LEGACY_DEFAULT):
if self._adapter:
@ -71,10 +74,10 @@ class Device:
self.disconnection_callback = callback
self.disconnection_user_data = user_data
gattlib_register_on_disconnect(self._connection, Device.on_disconnection, self)
gattlib_register_on_disconnect(self.connection, Device.on_disconnection, self)
def disconnect(self):
ret = gattlib_disconnect(self._connection)
ret = gattlib_disconnect(self.connection)
handle_return(ret)
def discover(self):
@ -83,7 +86,7 @@ class Device:
#
_services = POINTER(GattlibPrimaryService)()
_services_count = c_int(0)
ret = gattlib_discover_primary(self._connection, byref(_services), byref(_services_count))
ret = gattlib_discover_primary(self.connection, byref(_services), byref(_services_count))
handle_return(ret)
self._services = {}
@ -98,7 +101,7 @@ class Device:
#
_characteristics = POINTER(GattlibCharacteristic)()
_characteristics_count = c_int(0)
ret = gattlib_discover_char(self._connection, byref(_characteristics), byref(_characteristics_count))
ret = gattlib_discover_char(self.connection, byref(_characteristics), byref(_characteristics_count))
handle_return(ret)
self._characteristics = {}
@ -108,7 +111,6 @@ class Device:
logging.debug("Characteristic UUID:0x%x" % characteristic.short_uuid)
def get_advertisement_data(self):
_advertisement_data = POINTER(GattlibAdvertisementData)()
_advertisement_data_count = c_size_t(0)
@ -116,10 +118,17 @@ class Device:
_manufacturer_data = c_void_p(None)
_manufacturer_data_len = c_size_t(0)
ret = gattlib_get_advertisement_data(self._connection,
byref(_advertisement_data), byref(_advertisement_data_count),
byref(_manufacturer_id),
byref(_manufacturer_data), byref(_manufacturer_data_len))
if self._connection is None:
ret = gattlib_get_advertisement_data_from_mac(self._adapter._adapter, self._addr,
byref(_advertisement_data), byref(_advertisement_data_count),
byref(_manufacturer_id),
byref(_manufacturer_data), byref(_manufacturer_data_len))
else:
ret = gattlib_get_advertisement_data(self._connection,
byref(_advertisement_data), byref(_advertisement_data_count),
byref(_manufacturer_id),
byref(_manufacturer_data), byref(_manufacturer_data_len))
handle_return(ret)
advertisement_data = {}
@ -148,7 +157,6 @@ class Device:
return advertisement_data, _manufacturer_id.value, manufacturer_data
@property
def services(self):
if not hasattr(self, '_services'):