Attempting to connect two WiPy 3.0 via Bluetooth



  • I am setting up one client and one service WiPy. I cannot get them to connect.

    Using code directly from the pycom documentation for the client:

    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) == 'WiPySensor':
          try:
              conn = bt.connect(adv.mac)
              services = conn.services()
              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)
    

    and service:

    from network import Bluetooth
    bluetooth = Bluetooth()
    bluetooth.set_advertisement(name='WiPySensor', 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")
        elif events & Bluetooth.CLIENT_DISCONNECTED:
            print("Client disconnected")
    
    def char1_cb_handler(chr, data):
    
        # The data is a tuple containing the triggering event and the value if the event is a WRITE event.
        # We recommend fetching the event and value from the input parameter, and not via characteristic.event() and characteristic.value()
        events, value = data
        if  events & Bluetooth.CHAR_WRITE_EVENT:
            print("Write request with value = {}".format(value))
        else:
            print('Read request on char 1')
    
    bluetooth = Bluetooth()
    bluetooth.set_advertisement(name='WiPySensor', service_uuid=b'1234567890123456')
    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)
    char1_cb = chr1.callback(trigger=Bluetooth.CHAR_WRITE_EVENT | Bluetooth.CHAR_READ_EVENT, handler=char1_cb_handler)
    
    

    Running both, I get the error from the client:
    OSError: operation already in progress

    Not sure how to proceed from here.


Log in to reply
 

Pycom on Twitter