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

    Reading a bluetooth characteristic value on an Iphone

    Scheduled Pinned Locked Moved LoPy
    lopy blecharacteristicsread function
    1 Posts 1 Posters 658 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.
    • J Offline
      johnallen24
      last edited by

      I am trying to read the characteristic value from the LoPy on an Iphone with the example code from the Pycom documentation.

      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)

      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:
          if char1_read_counter < 3:
              print('Read request on char 1')
          else:
              return 'ABC DEF'
      

      char1_cb = chr1.callback(trigger=Bluetooth.CHAR_WRITE_EVENT | Bluetooth.CHAR_READ_EVENT, handler=char1_cb_handler)

      I can get the phone to connect to the LoPy and receive the characteristic data from it. However, I cannot figure out how to read the data to get the characteristic's value of 5. I am using Xcode with the Swift programming language and its Core Bluetooth framework. When reading a value on the LoPy, there is a simply a read() function. How does this function work and is there a way to get the code behind it, so I could set up a similar implementation in Swift?

      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