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

    Find my phone bluetooth with my lopy4

    Scheduled Pinned Locked Moved LoPy
    6 Posts 3 Posters 110 Views 3 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.
    • M Offline
      M m
      last edited by M m

      Hello!!

      I'm trying to scan the bluetooth devices that are near me (my lopy).

      After reading this, I'm doing the following:

      bluetooth_devices = []
      global_values.bluetooth.start_scan(5)
      while global_values.bluetooth.isscanning():
          adv = global_values.bluetooth.get_adv()
          if adv:
              if adv[0] not in bluetooth_devices:
                  # 0: mac, 1: addr_type, 2: adv_type, 3: rssi, 4: data
                  bluetooth_devices.append(adv[0])
      
      if len(bluetooth_devices) > 0:
              for dev in bluetooth_devices:
                  print('\t',ubinascii.hexlify(dev),'    ', dev)
      

      The problem is that prints some values (I suppose that are devices) but are not appearing neither of my two cell phones (obviously the bluetooth is connected). I know they are not appearing because I know the MAC of my cell phones,

      This would be one of them,
      Capture+_2021-04-25-19-12-35.png

      I'm doing it wrong? I'm not printing the MAC address? Seems easy, but I can't achieve it.

      Thank you very much! I love the device!

      L J 2 Replies Last reply Reply Quote 0
      • L Offline
        livius @M m
        last edited by

        @M-m
        I do not know how helpfull it will be but:
        Are you phones advertising in the moment of scan?
        And more wide question are then some new or old one? As BL <> BLE, and some models cannot operate as BLE.
        You can also increase timeout from 5 to e.g. 20.
        And are you sure that you look at proper MAC address? As phones have e.g. MAC for wifi and MAC for Bluetooth.

        think about people first, then computers

        1 Reply Last reply Reply Quote 1
        • J Offline
          jcaron @M m
          last edited by

          @M-m for privacy reasons, many (most?) BLE devices implement MAC address randomisation in advertisements. You’ll probably have to rely on something different (service, manufacturer data...) to match your phone.

          1 Reply Last reply Reply Quote 1
          • M Offline
            M m
            last edited by

            @livius

            • Are you phones advertising in the moment of scan?
              - Yes, I need to be on a specific screen and yes I am on it.

            • And more wide question are then some new or old one?
              - They are Bluetooth 4.2. If I'm not wrong, BLE starts at version 4.0

            • You can also increase timeout from 5 to e.g. 20.
              - I did it and nothing changes.

            • And are you sure that you look at proper MAC address?
              - 100% sure. If you see the image in my question, is a screenshot of my bluetooth characteristics and says in Spanish "Dirección de Bluetooth" that means Bluetooth direction.

            @jcaron

            Thinking about this, I have two values about my phones. Example:

            • MAC direction: B4F1DAC0171C.
            • Bluetooth name: Q6 (in hex 5136)

            Can I use one of the two to find my phones?

            I tried this, but it doesn't solve anything for me.

            bluetooth_devices = []
            global_values.bluetooth.start_scan(30)
            while global_values.bluetooth.isscanning():
                adv = global_values.bluetooth.get_adv()
                if adv and adv[0] not in bluetooth_devices:
                    print('>>1  ',global_values.bluetooth.resolve_adv_data(adv.data, Bluetooth.ADV_NAME_CMPL))
                    try:
                        print('>>2a ',ubinascii.hexlify(global_values.bluetooth.resolve_adv_data(adv.data, Bluetooth.ADV_MANUFACTURER_DATA)))
                    except:
                        print('>>2b ',global_values.bluetooth.resolve_adv_data(adv.data, Bluetooth.ADV_MANUFACTURER_DATA))
                    # 0: mac, 1: addr_type, 2: adv_type, 3: rssi, 4: data
                    print(adv[0])
                    print(adv[1])
                    print(adv[2])
                    print(adv[3])
                    print(adv[4])
                    bluetooth_devices.append(adv[0])
            
            if len(bluetooth_devices) > 0:
                    for dev in bluetooth_devices:
                        print('\t',ubinascii.hexlify(dev),'    ', dev)
            

            I have all these prints because I thought maybe I can find the MAC or NAME mentioned before.

            Thank you for your attention.

            J 1 Reply Last reply Reply Quote 0
            • J Offline
              jcaron @M m
              last edited by

              @M-m What are you doing to make the phone advertise? IIRC most phones don't advertise by default (the pages for discovery, pairing, etc. are usually for classic BT, not BLE). You need an app that explicitly starts advertising, and then you can usually control what they advertise.

              M 1 Reply Last reply Reply Quote 0
              • M Offline
                M m @jcaron
                last edited by

                @jcaron Hello I'm here again, I was trying solving it without disturbing you but I can't understand how it works.

                I downloaded a few apps to advertise, but nothing solves my problem.

                To understand it more, I thought start using another lopy4 as an advertiser. I used this code:

                from network import Bluetooth
                
                bluetooth = Bluetooth()
                bluetooth.set_advertisement(name='LoPyNA', manufacturer_data="lopy_v1", 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")
                
                bluetooth.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=conn_cb)
                bluetooth.advertise(True)
                

                Now I come back to my first lopy with the BLE scanner and this is what I obtain:

                • Mac: b'30aea474bc8e'.
                • Data: b'02010607094c6f50794e41020aeb05122000400002010608ff6c6f70795f7631110731323334353637383930313233343536000000000000000000000000'.
                • Adv name: LoPyNA.

                Thus, my code is working fine because I can find BLE devices. But not good enough because it can't find my two cell phones or my computer (or if it does, I'm unable to recognize them).

                Some suggestion or idea?

                Thank you very much!

                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