Summary

In this article I discuss BLE “Observing” and add that functionality to my PSoC 6 – CYW43xxx AnyCloud BLE Adverting Scanner project.

Story

In part1 of this series I discussed the pieces parts required to get the AnyCloud Bluetooth Stack operating using the AnyCloud SDK running on a PSoC 6 with a CY43xxx combo.  Then in part 2 I built a project with those parts and started up the Bluetooth Host stack.  The project didn’t really do anything, actually nothing, so it wasn’t very interesting, but it was going.  In this article I will discuss BLE advertising scanning, how to configure it in the AnyCloud project and finally how to add it to the project.

For this series of articles I will:

  1. Discuss an overview of Bluetooth Application Architecture (Part 1)
  2. Explain how to Start the AnyCloud Bluetooth Stack (Part 1) 
    1. Discuss the configuration structure: bt_platform_cfg_setting (Part 1)
    2. Discuss the configuration structure: wiced_bt_cfg_settings (Part 1)
    3. Discuss the Tasks created by the Bluetooth Stack (Part 1)
    4. Build the Basic Project (Part 2)
    5. Explain BLE Advertising - Scanner/Observer (Part 3)
    6. Explain BLE Advertising - Advertise (Part 3)
    7. How does scanning work in the AnyCloud Bluetooth Stack?  (Part 3)
    8. Add Observing to our Project  (Part 3)
    9. Program and Test the Project  (Part 3)
    10. Add the IoT Expert Bluetooth Utility Library (Part 4)
    11. Mulithreading Dangers (Part 4)
    12. Add a Queue to the Bluetooth Manager Thread (Part 4)
    13. Add a Timer to Process the Queue (Part 4)
    14. A Potential Threading Bug (Part 4)
    15. Add a Scan Off & On Command (Part 4)
    16. Create an Advertising Data Database Task (Part 5)
    17. Update the Advertising Database to Accept Submitted ADV Packets (Part 5)
    18. Update the Bluetooth Manager to Submit Adv Packets (Part 5)
    19. Create an Advertising Data Database (Part 5)
    20. Add a Command to Print the Database (Part 5)
    21. Refactor a silly duplication (Part 6)
    22. Update the print (Part 6)
    23. Add the "decode" command (Part 6)
    24. Add the decode packets functionality (Part 6)
    25. Program and test (Part 6)
    26. Design a new advertising database (Part 7)
    27. Update to the new database (Part 7)
    28. Update printing (Part 7)
    29. Update the advertising add (Part 7)
    30. Create a watch command (Part 7)
    31. Create a record command (Part 7)
    32. Create an erase command (Part 7)
    33. Update the filter data structure (Part 8)
    34. Fix the Add function (Part 8)
    35. Add the filter command (Part 8)
    36. Update the printing (Part 8)
    37. Fix a memory leak (Part 9)
    38. Add packet age (Part 9)
    39. Fix the printing, again (Part 9)
    40. Fix the add packet function (Part 9)
    41. Update the database structure for sort and purge (Part 10)
    42. Add the sort and purge commands (Part 10)
    43. Add the sort functionality (Part 10)
    44. Add the purge functionality (Part 10)
    45. The End & Commentary (Part 10)

    All of the code can be found at git@github.com:iotexpert/AnyCloudBLEScanner.git and https://github.com/iotexpert/AnyCloudBLEScanner.git

    There are git tags in place starting at part 5 so that you can look at just that version of the code.  "git tag" to list the tags.  And "git checkout part6" to look at the part 6 version of the code.

    Explain BLE Advertising – Scanner/Observer

    You might recall that there are four roles that a BLE device can perform

    • Peripheral – low power devices that broadcast advertisements, then accept a single connection
    • Central – devices like cell phones that connect to peripherals.  They may run multiple connections at a time.
    • Broadcaster – a nonconnectable peripheral that sends out advertisements
    • Observer – A central-like device that listens for broadcasters (or advertising peripherals)

    And you might remember that advertisements are short, up to 31-byte, packets of data that give information which can be used for one or more of:

    • advertising the availability to connect
    • advertising services
    • advertising the name
    • advertising vendor specific information
    • advertising beacon data (like temperature or …)
    • advertising location

    And, if you forgot, BLE operates on 40 channels.  But to save power in peripherals, all of the advertising happens on channel 37, 38 and 39.  Specifically a peripheral or broadcaster will send out the advertising data on channel 37, then 38 then 39, then wait… then do it again.  But why one channel at a time?  Because BLE radio’s can be tuned to transmit and receive on only one channel at a time (a power saving and complexity reducing feature)

    Inside of the Central/Observer it will listen on channel 37 for a “window” amount of time.  Then it will do nothing for an interval-window amount of time.  Then it will do that same thing on channel 28 then 39.  But why only one channel at a time?  Same reason as above, it saves power and simplifies the design.  Why not have the window and the interval be the same?  Once again, it saves power.

    Here is a picture:

    But, what happens if you are not listening when the advertiser advertises?  You missed it.  Tough shit.  It turns out that setting the scan window and interval will greatly impact the probability that you hear advertisements.  And, you are more likely to hear advertisements because they are sent on three channels.  But it seems like it will never work.  Will it? … yes, of course, or they wouldn’t have done it that way 🙂

    BLE Advertising – The Advertiser Peripheral or Broadcaster

    So what exactly is inside of an advertising packet?  Volume 6 part B Section 2.3 of the bluetooth core spec describes the advertising protocol data unit (PDU)

    But what is inside of the header?

    This leaves us with what is inside of the “payload”.  The answer is that the ADV_IND Payload Data Unit (PDU) contains an address of 6-bytes plus up to 31 bytes of data.

    The AdvA field shall contain the advertiser’s public or random device address as indicated by TxAdd.

    The actual AdvData field is further broken up into “AD Structures” like this:

    And what is the “AD Type”, well it is a one byte of one of the following:

    And then where do you find the assigned numbers for the field types?  In the “Assigned Numbers and GAP“.  Here is a clip from the spec.

    And conveniently enough we enumerated them for you inside of the SDK header file wiced_bt_ble.h

    How does scanning work in the AnyCloud Bluetooth Stack?

    To turn on observing/scanning you need to call the function:

    Which will cause the host stack to tell the controller to start scanning for advertising packets.  It will set the scan window and scan interval the low duty scan settings from the bluetooth configuration structure… which we setup with the Bluetooth configurator.

    When the controller hears an advertising packet, it will send the HCI advertising report to the Bluetooth host stack, which with then call you back.  Specifically it will call you back by calling the p_scan_result_cback” function.

    You provide the callback function which has the prototype:

    which contains two parameters, p_scan_result which is a structure that has the mac address and some thing data plus the p_adv_data which has the raw bytes of the advertising packet.

    Add Observing to our Project

    OK.  Lets add this to our project by creating a callback function like this:

    Lines 5-9: Just prints out the raw bytes of the MAC address of the remote device, the one advertising

    To print out the raw advertising data you need to remember that it is formatted as

    1. A length (of all of the data of the field)
    2. A type
    3. The rest of the data

    When you find a field of length of 0 you know that you have reached the end of the data

    On Lines 13-20: I print out one field at a time and the raw data

    Then update the management callback to start the scanner after the stack is successfully started

    Program and Test

    Now when I run the program data comes blasting out of the screen because there are a boatload of ble devices in my house

    In the next article Ill add some more smarts to manage the data to be easier to look at.

    For your information here is all of the file bluetoothManager.c

     

    Recommended Posts

    No comment yet, add your voice below!


    Add a Comment

    Your email address will not be published. Required fields are marked *