title: Bluetooth Low Energy in Python
body: <p>I'm trying to communicate with other devices using Bluetooth low energy. By communicate i mean sending short messages that will be displayed on that devices that are listening in the area I am in. I'm using Bleak Python library.
  Here is the example code, i found this code on stack and it lists Bluetooth devices.</p>
  <pre class="lang-py prettyprint-override"><code>import asyncio
  import logging
  
  from bleak import discover
  from bleak import BleakClient
  
  devices_dict = {}
  devices_list = []
  receive_data = []
  
  
  # To discover BLE devices nearby
  async def scan():
      dev = await discover()
      for i in range(0, len(dev)):
          # Print the devices discovered
          print(&quot;[&quot; + str(i) + &quot;]&quot; + dev[i].address, dev[i].name, dev[i].metadata[&quot;uuids&quot;])
          # Put devices information into list
          devices_dict[dev[i].address] = []
          devices_dict[dev[i].address].append(dev[i].name)
          devices_dict[dev[i].address].append(dev[i].metadata[&quot;uuids&quot;])
  
          devices_list.append(dev[i].address)
  
  
  def notification_handler(sender, data):
      print(', '.join('{:02x}'.format(x) for x in data))
  
  
  async def run(address, debug=False):
      log = logging.getLogger(__name__)
      if debug:
          import sys
  
          log.setLevel(logging.DEBUG)
          h = logging.StreamHandler(sys.stdout)
          h.setLevel(logging.DEBUG)
          log.addHandler(h)
  
      async with BleakClient(address) as client:
          x = await client.is_connected()
          log.info(&quot;Connected: {0}&quot;.format(x))
  
          for service in client.services:
              log.info(&quot;[Service] {0}: {1}&quot;.format(service.uuid, service.description))
              for char in service.characteristics:
                  if &quot;read&quot; in char.properties:
                      try:
                          value = bytes(await client.read_gatt_char(char.uuid))
                      except Exception as e:
                          value = str(e).encode()
                  else:
                      value = None
                  log.info(
                      &quot;\t[Characteristic] {0}: (Handle: {1}) ({2}) | Name: {3}, Value: {4} &quot;.format(
                          char.uuid,
                          char.handle,
                          &quot;,&quot;.join(char.properties),
                          char.description,
                          value,
                      )
                  )
                  for descriptor in char.descriptors:
                      value = await client.read_gatt_descriptor(descriptor.handle)
                      log.info(
                          &quot;\t\t[Descriptor] {0}: (Handle: {1}) | Value: {2} &quot;.format(
                              descriptor.uuid, descriptor.handle, bytes(value)
                          )
                      )
  
                  CHARACTERISTIC_UUID = &quot;put your characteristic uuid&quot;
  
                  await client.start_notify(CHARACTERISTIC_UUID, notification_handler)
                  await asyncio.sleep(5.0)
                  await client.stop_notify(CHARACTERISTIC_UUID)
  
  
  async def foo(address):
      async with BleakClient(address) as client:
          client.start_notify(address, callback)
  
  
  if __name__ == &quot;__main__&quot;:
      print(&quot;Scanning for peripherals...&quot;)
  
      loop = asyncio.get_event_loop()
      loop.run_until_complete(scan())
  
      # let user chose the device
      index = input('please select device from 0 to ' + str(len(devices_list)) + &quot;:&quot;)
      index = int(index)
      address = devices_list[index]
      print(&quot;Address is &quot; + address)
  
      # Run notify event
      loop = asyncio.get_event_loop()
  
  
      def callback(sender: int, data: bytearray):
          print(f&quot;{sender}: {data}&quot;)
  
  
      loop.run_until_complete(foo(address))
  
  </code></pre>
  <p>The problem is that sometimes it displays not all of the accessible devices. I would like to add feature that sends a short message to it, but I can't find any proper tutorial or code example. It should act like a Beacon.If it is not possible in Python, can you suggest any other language that has such features and will solve this problem?</p>