Decoding BLE Scan Results

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Decoding BLE Scan Results

Post by devnull » Fri Oct 18, 2019 1:43 am

I am having difficulty decoding the scan results from BLE devices.

I have the following interrupt to display the scan results:

Code: Select all

def bt_irq(event, data):
  if event == _IRQ_SCAN_RESULT:
    addr_type, addr, iscon, rssi, adv_data = data
    if iscon:
      print('type:{} addr:{} rssi:{} data:{}'.format(addr_type, ubinascii.hexlify(addr), rssi, ubinascii.hexlify(adv_data)))

bt.gap_scan(20000)

Code: Select all

type:1 addr:b'6b438955c14c' rssi:-59 data:b'02010613ff4c000c0e00fd3056fefd65505e5913768491'
type:0 addr:b'28f076526d9f' rssi:-59 data:b'0201060aff4c0010050b186d104c'
type:1 addr:b'6b438955c14c' rssi:-59 data:b'02010613ff4c000c0e00fd3056fefd65505e5913768491'
type:0 addr:b'28f076526d9f' rssi:-59 data:b'0201060aff4c0010050b186d104c'
type:1 addr:b'6b438955c14c' rssi:-63 data:b'02010613ff4c000c0e00fd3056fefd65505e5913768491'
type:0 addr:b'28f076526d9f' rssi:-64 data:b'0201060aff4c0010050b186d104c'
type:1 addr:b'6b438955c14c' rssi:-66 data:b'02010613ff4c000c0e00fd3056fefd65505e5913768491'
However none of the addresses (addr) appear to match the addresses that are discovered on my phone using BLE scanners such NRF Connect app.

Am I missing something ???

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Decoding BLE Scan Results

Post by jimmo » Fri Oct 18, 2019 2:22 am

devnull wrote:
Fri Oct 18, 2019 1:43 am
However none of the addresses (addr) appear to match the addresses that are discovered on my phone using BLE scanners such NRF Connect app.
Are they byte-reversed? FYI, an earlier version of the PR did have the byte order reversed (it's a bit of a tradeoff here between consistency with other APIs and what naturally makes sense). The current version in master should match the "sticker order".

Do you have any devices with known addresses (i.e. mac address printed on a sticker) that you can scan for?

User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Re: Decoding BLE Scan Results

Post by devnull » Fri Oct 18, 2019 3:27 am

OK, managed to find a device with a mac address and the reported address appears to be correct.

But if I use a BLE scanner on my iphone the ID (which I assume is the mac address) is much longer i.e.

F5E3DC34-F500-3E17-OA49-F2A5EC0E66E2

I have managed to determine which device is my Beacon as the manufacturer data contains the AA20 service (reversed):

type:0 addr:b'cdd751150f7e' rssi:-52 data:b'(02)0106-(03)02[20aa]-(0e)ff001a0600350064cdd751150f7e'

You can see that the ID reported on iphone (F5E3DC34-F500-3E17-OA49-F2A5EC0E66E2) bears no resemblance to the device mac address (cdd751150f7e)

But how would i now connect to this device and read it's data that's transmitted once a second ??

danielm
Posts: 167
Joined: Mon Oct 05, 2015 12:24 pm

Re: Decoding BLE Scan Results

Post by danielm » Fri Oct 18, 2019 12:53 pm

The string is not BLE address but probably iBeacon UUID, which is encoded in the message.

Some resources on how to decode BLE advertisments:
https://www.silabs.com/community/wirele ... tisin-hGsf
https://www.blueupbeacons.com/docs/WorkshopBeacons.pdf

pagano.paganino
Posts: 89
Joined: Fri Sep 11, 2015 10:47 pm
Location: Italy

Re: Decoding BLE Scan Results

Post by pagano.paganino » Sat Oct 19, 2019 9:12 am


User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Re: Decoding BLE Scan Results

Post by devnull » Sun Oct 20, 2019 11:16 pm

So, based on my experiments / tests, it appears that you cannot directly read a service by UUID, but need to send several commands and parse the results ??

For example if I want to read the battery level: 0x180f, then I would need to:

1) discover all services for a device
2) parse the services result looking for 0x180f
3) discover characteristics using the start and end handles (29,32) from service 0x180f.
4) get the property from the characteristics query (31)
5) read the value of property 31

Code: Select all

bt.BLE().gattc_discover_services(64)
>> services 64 [29] [32] UUID16(0x180f)
ble.bt.gattc_discover_characteristics(64,[29],[32])
  
>> chars 64 30 [31] 18 UUID16(0x2a19)
ble.bt.gattc_read(64,[31])
The square brackets are not part of the command, I just added them for clarity of the values I was looking for.

Is this the correct way of reading a UUID i.e the battery levels ??

User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Re: Decoding BLE Scan Results

Post by devnull » Tue Oct 22, 2019 11:20 pm

Hi @jimmo - could you confirm that my 5 steps above are the correct way to read a value by it's UUID ??

Many Thanks

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Decoding BLE Scan Results

Post by jimmo » Wed Oct 23, 2019 5:20 am

Hi,

Yes, that is correct.

I haven't looked in detail, but I _think_ the handles are consistent, so if you already know the handle, then you can read/write directly from them immediately after a connect. But I'm not sure you can rely on that.

The BLE protocol does have support for looking up services and characteristics directly by UUID. But it really only simplifies the irq handler implementation (marginally).

shenouda
Posts: 3
Joined: Thu May 14, 2020 10:11 am

Re: Decoding BLE Scan Results

Post by shenouda » Thu May 14, 2020 10:15 am

devnull wrote:
Fri Oct 18, 2019 1:43 am

However none of the addresses (addr) appear to match the addresses that are discovered on my phone using BLE scanners such NRF Connect app.

Am I missing something ???
I am having the same issue, can't get past it,
like :
type:1 addr:b'790c96ba0d35' rssi:-38 data:b'1eff060001092002a7ad7a8ddbb036b1379963a399cc0731f7d591c43e87c9'
has address b'y\x0c\x96\xba\r5'
which reads on nRF connect app --> DC:FB:48:4D:62:A9
so how to get from this b'y\x0c\x96\xba\r5' to this DC:FB:48:4D:62:A9

are they the same ?? can't figure it out !! :?:

Post Reply