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

    BLE ATT Wrapper

    Scheduled Pinned Locked Moved Guides
    blebluetooth
    1 Posts 1 Posters 626 Views 2 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.
    • C Offline
      cmisztur
      last edited by cmisztur

      Example of creating BLE GATT services and characteristics.

      https://github.com/cmisztur/pycom-ble-gatt-wrapper

      usage:

      import gc
      import machine
      import utime
      import ubinascii
      import uos
      from machine import Timer
      
      from L99_BLEGATTS import BLEGATTS
      
      hardware_serial = ubinascii.hexlify(machine.unique_id()).decode()
      l99_serial = 'L99-%s' % hardware_serial
      
      def ble_connection_callback(is_connected):
          if is_connected: 
              print("BLE connected")
          else:
              print("BLE disconnected")
      
      ble = BLEGATTS()
      ble.init(advert_name=l99_serial, connect_callback=ble_connection_callback)
      
      ble.addService(service_name='device_info',uuid=6154) \
          .addReadChar (name='l99_serial', uuid=0, static_read=l99_serial) \
          .addReadChar (name='hardware_serial', uuid=1, static_read=hardware_serial) \
          .addReadChar (name='sysname', uuid=2, dynamic_read=lambda cn,id: uos.uname()[0]) \
          .addReadChar (name='nodename', uuid=3, dynamic_read=lambda cn,id: uos.uname()[1]) \
          .addReadChar (name='release', uuid=4, dynamic_read=lambda cn,id: uos.uname()[2]) \
          .addReadChar (name='version_number', uuid=5, dynamic_read=lambda cn,id: uos.uname()[3].split(' on ')[0]) \
          .addReadChar (name='version_date', uuid=6, dynamic_read=lambda cn,id: uos.uname()[3].split(' on ')[1]) \
          .addReadChar (name='machine', uuid=7, dynamic_read=lambda cn,id: uos.uname()[4]) \
          .addReadChar (name='lora_version', uuid=8, dynamic_read=lambda cn,id: uos.uname()[5]) \
          .start()
      
      ble.addService(service_name='char_tests',uuid=0) \
          .addNotifyChar (name='note1', uuid=0, static_read=0) \
          .addReadNotifyChar (name='note2', uuid=1, static_read=0) \
          .addReadChar (name='self', uuid=2, dynamic_read=lambda cn,id: cn+str(id)) \
          .addReadWriteChar (name='r+w', uuid=3,
                              dynamic_read=lambda cn,id: char_rw_test_read(cn,id),
                              dynamic_write=lambda cn,id,val: char_rw_test_write(cn,id,val)) \
          .addReadWriteNotifyChar (name='r+w+n', uuid=4,
                                  dynamic_read=lambda cn,id: char_rwn_test_read(cn,id),
                                  dynamic_write=lambda cn,id,val: char_rwn_test_write(cn,id,val)) \
          .start()
      
      ble.advertise()
      
      rw_var = 1337
      
      def char_rw_test_read(char_name,char_uuid):
          global rw_var
          return rw_var
      
      def char_rw_test_write(char_name,char_uuid,char_value):
          global rw_var
          rw_var = char_value
      
      def char_rwn_test_read(char_name,char_uuid):
          global rw_var
          return rw_var
      
      def char_rwn_test_write(char_name,char_uuid,char_value):
          global rw_var
          rw_var = char_value
          global ble
          ble.setCharValue('char_tests','r+w+n',rw_var) # ain't gonna notify itself
      
      def char_timer(t):
          global counter
          counter = counter + 1
          ble.setCharValue('char_tests','note1',counter)
          ble.setCharValue('char_tests','note2',counter) # i can only get one or the other to notify, but not both
      
      counter = 1
      timer = Timer.Alarm(handler=char_timer, ms=2000, periodic=True)
      
      1 Reply Last reply Reply Quote 2

      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