Skip to content

Commit

Permalink
Update android_ble_scanner.py
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkusPiotrowski authored Apr 29, 2024
1 parent f7cb059 commit 00a1590
Showing 1 changed file with 41 additions and 29 deletions.
70 changes: 41 additions & 29 deletions Example/android_ble_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ async def start_scan(self, widget):
scanner.discovered_devices_and_advertisement_data
contains each discovered device only once.
"""
self.device_list.value = 'Start BLE scan...\n'
self.data_list.value = ''
self.print_device('Start BLE scan...', clear=True)
self.print_data(clear=True)

async with Scanner() as scanner:
await asyncio.sleep(10)
self.device_list.value += '...scanning stopped.\n'
self.print_device('...scanning stopped.')
self.show_scan_result(
scanner.discovered_devices_and_advertisement_data
)
Expand All @@ -93,29 +93,25 @@ async def start_discover(self, widget):
'return_adv=True' returns a dic.
'return_adv=False' would just return a list of discovered devices.
"""
self.device_list.value = 'Start BLE scan...\n'
self.data_list.value = ''
self.print_device('Start BLE scan...', clear=True)
self.print_data(clear=True)

result = await Scanner.discover(return_adv=True)
self.device_list.value += '...scanning stopped.\n'
self.print_device('...scanning stopped.')
self.show_scan_result(result)

async def manual_scan_with_callback(self, widget):
"""Start and stop a scan manually and display results via callback.
Callback must be synchronous (for bleekWare).
"""

"""Start and stop a scan manually and display results via callback."""
if not self.scan_on:
self.scanner = Scanner(self.scan_callback)
await self.scanner.start()
self.scan_on = True
self.device_list.value = 'Start BLE scan...\n'
self.data_list.value = 'Device data: \n'
self.print_device('Start BLE scan...', clear=True)
self.print_data('Device data:', clear=True)
else:
await self.scanner.stop()
self.scan_on = False
self.device_list.value += '...scanning stopped.\n'
self.print_device('...scanning stopped.')

async def manual_scan_with_generator(self, widget):
"""Start and stop a scan manually and display results via generator.
Expand All @@ -127,15 +123,16 @@ async def manual_scan_with_generator(self, widget):
self.scan_on = True
self.scanner = Scanner()
await self.scanner.start()
self.device_list.value = 'Start BLE scan...\n'
self.data_list.value = 'Device data: \n'
self.print_device('Start BLE scan...', clear=True)
self.print_data('Device data:', clear=True)
async for device, data in self.scanner.advertisement_data():
self.device_list.value += self.get_name(device) + '\n'
self.data_list.value += str(device) + '\n'
self.data_list.value += str(data) + '\n\n'
self.print_device(self.get_name(device))
self.print_data(str(device))
self.print_data(str(data))
self.print_data()
if not self.scan_on:
await self.scanner.stop()
self.device_list.value += '...scanning stopped.\n'
self.print_device('...scanning stopped.')
break
else:
self.scan_on = False
Expand All @@ -146,25 +143,26 @@ def scan_callback(self, device, advertisement_data):
The callback is called on each detection event, so the same
device can pop up several times during the scan.
For the moment, the callback function in bleekWare must be
synchronous.
This callback can be a normal or an async function.
"""
self.device_list.value += self.get_name(device) + '\n'
self.data_list.value += str(device) + '\n'
self.data_list.value += str(advertisement_data) + '\n\n'
self.print_device(self.get_name(device))
self.print_data(str(device))
self.print_data(str(advertisement_data))
self.print_data()

def show_scan_result(self, data):
"""Show names of found devices and attached advertisment data.
'data' is a dictionary, where the keys are the BLE addresses
and the values are tuples of BLE device, advertisement data.
"""
self.device_list.value += 'Found devices: \n'
self.data_list.value += 'Device data: \n'
self.print_device('Found devices:')
self.print_data('Device data:', clear=True)
for key in data:
device, adv_data = data[key]
self.device_list.value += self.get_name(device) + '\n'
self.data_list.value += f'{device}\n{adv_data}\n\n'
self.print_device(self.get_name(device))
self.print_data(f'{device}\n{adv_data}')
self.print_data()

def get_name(self, device):
"""Return name or address of BLE device."""
Expand All @@ -173,6 +171,20 @@ def get_name(self, device):
else:
return f'No name ({device.address})'

def print_device(self, device='', clear=False):
"""Write device name to MultilineTextInput for devices."""
if clear:
self.device_list.value = ''
self.device_list.value += device + '\n'
self.device_list.scroll_to_bottom()

def print_data(self, data='', clear=False):
"""Write device data to MultilineTextInput for device data."""
if clear:
self.data_list.value = ''
self.data_list.value += data + '\n'
self.data_list.scroll_to_bottom()


def main():
return BleScannerApp()

0 comments on commit 00a1590

Please sign in to comment.