Advertising data from 1 lopy, trying to read from another one



  • High,

    I want to communicate using bluetooth from one LoPy to another, one LoPy is the GATT server and the other one is the Central device. Problem is I can't connect my Central Device to my Gatt server.

    • list item
      Here is the GATT server code :

    And here is the Central device code :

    And here is the Central device console :

    I don't know why but the creation of the GATT service object isn't working.

    Any suggestion?


    Log in to reply
     


  • High,

    Can I have some news about my issue?
    I don't know how to solve it !



  • This is a classic example of GATT server code and central device code

    /******************************************/
    Here is the GATT server code fro the first lopy :

    from network import Bluetooth
    
    bluetooth = Bluetooth()
    bluetooth.set_advertisement(name='LoPy', service_uuid=b'1234567890123456')
    
    def conn_cb (bt_o):
        events = bt_o.events()
        if  events & Bluetooth.CLIENT_CONNECTED:
    	print("Client connected")
        elif events & Bluetooth.CLIENT_DISCONNECTED:
    	print("Client disconnected")
    
    bluetooth.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=conn_cb)
    
    bluetooth.advertise(True)
    
    srv1 = bluetooth.service(uuid=b'1234567890123456', isprimary=True)
    
    chr1 = srv1.characteristic(uuid=b'ab34567890123456', value=5)
    
    def char1_cb(chr):
        print("Write request with value = {}".format(chr.value()))
    
    char1_cb = chr1.callback(trigger=Bluetooth.CHAR_WRITE_EVENT, handler=char1_cb)
    
    srv2 = bluetooth.service(uuid=1234, isprimary=True)
    
    chr2 = srv2.characteristic(uuid=4567)
    

    /********************************************/

    /********************************************/

    This is the code of the central device on the second LoPy :

    from network import Bluetooth
    import time
    bt = Bluetooth()
    bt.start_scan(-1)
    
    while True:
      adv = bt.get_adv()
      if adv and bt.resolve_adv_data(adv.data, Bluetooth.ADV_NAME_CMPL) == 'Glucose':
          try:
              print('1')
    	  conn = bt.connect(adv.mac)
    	  print('2')
    	  services = conn.services()
              print('3')
    	  for service in services:
    	      time.sleep(0.050)
    	      if type(service.uuid()) == bytes:
    	          print('Reading chars from service = {}'.format(service.uuid()))
    	      else:
    	          print('Reading chars from service = %x' % service.uuid())
    	      chars = service.characteristics()
    	      for char in chars:
    	          if (char.properties() & Bluetooth.PROP_READ):
    	              print('char {} value = {}'.format(char.uuid(), char.read()))
    	  conn.disconnect()
    	  break
          except:
    	  print("Error while connecting or reading from the BLE device")
    	  break
      else:
          time.sleep(0.050)
    

    /**************************************/

    /*************************************/

    This is what I've acquired on the Central device consol after executing the two codes:

    1
    2
    Error while connecting or reading from the BLE device 
    

    /****************************************************/

    I don't know why but the connection is always closed, I can't connect my Central device to my Gatt server



  • Can you post your code as text please? It's really difficult to test and check as PNGs.

    You can put it between a

    ```
    Your code here
    ```

    block to keep the formatting.


Log in to reply
 

1 out of 4

Pycom on Twitter