Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Two Lopy communicate via BLE, LOSE connection, CANNOT read service.

    Scheduled Pinned Locked Moved LoPy
    lopylopy bleserver client
    1 Posts 1 Posters 1.1k Views 1 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Y Offline
      yunying
      last edited by yunying

      Hello, I am trying communicate two lopy use BLE, one is server, another is client.
      The server success advertising and client could notice and connect. But we I am trying to for loop the service, it goes wrong and of course, I cannot read the characteristic.

      Here is the server code.

      from network import Bluetooth
      bluetooth = Bluetooth()
      bluetooth.init()
      #config data before send
      bluetooth.set_advertisement(name='LoPy', manufacturer_data = 'lopy_server_1',  service_data = b'19950922' ,service_uuid=b'1000000010000000')
      #bluetooth.set_advertisement(name='LoPy', service_data = b'19950922' , service_uuid=b'1234567890123456')
      def conn_cb (bt_o):
          events = bt_o.events() # this method returns the flags and clears the internal registry
          if  events & Bluetooth.CLIENT_CONNECTED:
              print("Client connected")
              #not sure
              bt_o.advertise(False)
          elif events & Bluetooth.CLIENT_DISCONNECTED:
              print("Client disconnected")
              #not sure
              bt_o.advertise(True)
      bluetooth.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=conn_cb)
      #start advertise 
      bluetooth.advertise(True)
      #Create a new service on the internal GATT server. Returns a object of type BluetoothServerService.
      #bluetooth.service(uuid, *, isprimary=True, nbr_chars=1, start=True)
      # start = True service start immediately
      srv1 = bluetooth.service(uuid=b'1000000010000000', isprimary=True, nbr_chars=1, start = True)
      chr1 = srv1.characteristic(uuid = b'ab34567890123456', properties = Bluetooth.PROP_READ, value=99999,  permissions = (1 << 0) | (1 << 4))
      #property = Bluetooth.PROP_READ | Bluetooth.PROP_WRITE
      char1_read_counter = 0
      def char1_cb_handler(chr):
          global char1_read_counter
          char1_read_counter += 1
          events = chr.events()
          if  events & Bluetooth.CHAR_WRITE_EVENT:
              print("Write request with value = {}".format(chr.value()))
          else:
              print('Read request on char 1')       
      char1_cb = chr1.callback(trigger=Bluetooth.CHAR_WRITE_EVENT | Bluetooth.CHAR_READ_EVENT, handler=char1_cb_handler)
      #char1_cb = chr1.callback(trigger=Bluetooth.CHAR_NOTIFY_EVENT, handler=char1_cb_handler)
      

      Here is the client code

      from network import Bluetooth
      import time
      bt = Bluetooth()
      bt.start_scan(-1)
      
      while True:
          adv = bt.get_adv()
          # resolve_adv_data(data , datatype)
          #bt.resolve_adv_data(adv.data, Bluetooth.ADV_NAME_CMPL) == 'Heart Rate'
          #  if adv and bt.resolve_adv_data(adv.data, Bluetooth.ADV_NAME_CMPL) == 'LoPy':
          if adv and bt.resolve_adv_data(adv.data, Bluetooth.ADV_NAME_CMPL) == 'LoPy':
              # stop scan before connect
              bt.stop_scan()
              print('333')
              try:
                  connection = bt.connect(adv.mac)  
                  if(connection.isconnected()):
                      print("Connection opened!")
           
                  # a list of services
                  services = connection.services()    
                  print('555')
                  time.sleep(0.050)
                  for service in services:
                      print('444')
                      time.sleep(0.050)
                      print('Reading chars from service = {}'.format(service.uuid()))
                          
                      chars = service.characteristics()
                      for char in chars:
                          print('char {} value = {}'.format(char.uuid(), char.read()))
                  connection.disconnect()
                  break
       
              except:
                  print("Error while connecting or reading from the BLE device")
                  break
          else:
              time.sleep(0.050)
      

      I already add stop scan, but connection.isconnected() never work.
      I also try to move connection.isconnected() but after connect, the service never show.

      1 Reply Last reply Reply Quote 0

      Hello! It looks like you're interested in this conversation, but you don't have an account yet.

      Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

      With your input, this post could be even better 💗

      Register Login
      • 1 / 1
      • First post
        Last post

      Pycom on Twitter

      Pycom