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

    write bluetooth FiPy

    Scheduled Pinned Locked Moved FiPy
    4 Posts 2 Posters 159 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.
    • T Offline
      Tgasser
      last edited by robert-hh

      Hello,
      I don't understand why, but when I do char.write(), nothing happens... It print well 'ok' then I can wait nothing happens... Thanks if you have an idea !

      from network import Bluetooth
      import time
      import ubinascii
      
      
      bt = Bluetooth()
      bt.start_scan(-1)
      
      while True:
          adv = bt.get_adv()
      
          if adv:
              print(ubinascii.hexlify(adv.mac))
              if bt.resolve_adv_data(adv.data, Bluetooth.ADV_NAME_CMPL) == 'Blabla':
                try:
                    conn = bt.connect(adv.mac)
                    services = conn.services()
                    for service in services:
                        time.sleep(0.050)
                        if service.uuid() == XXX
                            print('Reading chars from service = %x' % service.uuid())
                            chars = service.characteristics()
                            for char in chars:
                                    if (char.uuid()==XXX):
                                        if(char.properties() & Bluetooth.PROP_WRITE):
                                            print('ok')
                                            char.write(b'0xa01f')
                    conn.disconnect()
                    break
                except:
                    pass
          else:
              time.sleep(0.050)
      
      J 1 Reply Last reply Reply Quote 0
      • J Offline
        jcaron @Tgasser
        last edited by

        @Tgasser You have the code-quoting quotes the wrong way around, it's ```

        I don't think you are sending what you think or want: you are sending the characters 0, x, a, 0, 1, f, not 0xa0 and 0x1f.

        Instead of b'0xa01f', you can write b'\xa0\x1f' or bytes([0xa0,0x1f].

        Note that the order may be reversed based on the required endianness.

        T 2 Replies Last reply Reply Quote 1
        • T Offline
          Tgasser @jcaron
          last edited by

          @jcaron Thank you for your help, it works the way I wanted it to! Thanks for your advice.

          1 Reply Last reply Reply Quote 0
          • T Offline
            Tgasser @jcaron
            last edited by

            @jcaron I read several articles to invert the bytes and read the right values. If I have val= b'\xdd\xee' tanks to struct.pack('<h', *struct.unpack('>h', val)) I get the inverse val= b'\xee\xdd'.

            However, for val= b'\xxaa\xbb\xcc\xdd' I can't find a way to get val=b'\xdd\xcc\xbb\xaa'.

            Do you have any idea how to help me? Thank you in advance!

            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