element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Code Exchange
  • Technologies
  • More
Code Exchange
Forum Implementing Bluetooth technology - in C/C++ code.
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Code Exchange to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 15 replies
  • Subscribers 45 subscribers
  • Views 2565 views
  • Users 0 members are here
  • bluetooth
  • library
  • bluez
Related

Implementing Bluetooth technology - in C/C++ code.

anneranch
anneranch 10 months ago

I am looking for discussion on how to implement Bluetooth technology in C/C++ code.

My main concern is toi do this reliably and repeatedly.

I am basically using very old HCI approach, implementing  "bluez" "library" .

The code works, despite lack of documentation of "bluez library".

My "in words" question is

How  does "local  Bluetooth adapter " implements / enables  "scan" for remote Bluetooth device ,

how is "paring" and  "connect"  used dur9ing this process.]

The answer must be in "bluez"  library , however I am unable to "step thru " its code - too convoluted  and

not documented.

( I will post the link to C code when it became beneficial for detailed discussion )

 My secondary concern is

how to analyze and then  implement

"replacing USB serial cable  " witht Bluetooth technology / connecti0on?

Let's see how the forum sill respond and then I could give details for the secondary issue.

  • Sign in to reply
  • Cancel

Top Replies

  • shabaz
    shabaz 10 months ago +4
    The pairing, connect etc are all done by GAP messages. In the diagram below, you can see that those messages are ultimately converted down to a payload that is wrapped with a header and checksum, and then…
Parents
  • BigG
    BigG 10 months ago

    Can you explain why you decided to implement the Bluez stack as there are a couple other C++ options out there.

    ARM Mbed had a C++ implementation of a BLE stack: https://github.com/ARMmbed/ble

    https://github.com/arduino-libraries/ArduinoBLE (C++ for Arduino devices)

    Otherwise mainly c implementations (one example):

    docs.zephyrproject.org/.../bluetooth-arch.html

    github.com/.../hci

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • anneranch
    anneranch 10 months ago in reply to BigG

    I am  not sure I get the " why you decided to implement the Bluez stack". I run Linux / Ubuntu and "bluez" is standard  package implemented in Linux kernel. Hence I see no choise. I initially used Qt, but it is missing an option to "clear unknown database"  containing previously found remote devices, or  more accurately - I did not find it. That is part of my reason to go "as close as feasible" to HCI. Not being able to physically control the actually  scan for remote devices.

    I believe majority of  "improved (? with GUI ) Bluetooth stacks " are all based on "bluez" anyway.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • BigG
    BigG 10 months ago in reply to anneranch

    Ah ok. Now that makes better sense, especially the "not documented" part in your initial post.

    I believe the answers to the questions you asked would depend on the device driver chosen. There are a number of different hci implementations.

    https://elixir.bootlin.com/linux/v6.11.8/source/drivers/bluetooth

    I cannot vouch for this research paper but it popped up following a quick google search.

    https://www.researchgate.net/publication/345032366_Bluetooth_Stack_and_how_Linux_OS_handles_it

    Otherwise another suggestion is to search for yocto implementations of Bluetooth. Tends to throw up some info.

    https://mediatek.gitlab.io/aiot/doc/aiot-dev-guide/release/v23.1/sw/yocto/app-dev/bluetooth/bluez/bluez-common.html#scan-some-advertising-bluetooth-device-nearby

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • BigG
    BigG 10 months ago in reply to anneranch

    Ah ok. Now that makes better sense, especially the "not documented" part in your initial post.

    I believe the answers to the questions you asked would depend on the device driver chosen. There are a number of different hci implementations.

    https://elixir.bootlin.com/linux/v6.11.8/source/drivers/bluetooth

    I cannot vouch for this research paper but it popped up following a quick google search.

    https://www.researchgate.net/publication/345032366_Bluetooth_Stack_and_how_Linux_OS_handles_it

    Otherwise another suggestion is to search for yocto implementations of Bluetooth. Tends to throw up some info.

    https://mediatek.gitlab.io/aiot/doc/aiot-dev-guide/release/v23.1/sw/yocto/app-dev/bluetooth/bluez/bluez-common.html#scan-some-advertising-bluetooth-device-nearby

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Children
  • anneranch
    anneranch 10 months ago in reply to BigG

    Thanks for reply. I am little confused by "depends on device driver ". Allow me to get back to what I have as starting point.  Using "bluez" or actually HCI - my code  determines "dev_id" . I actually think it is wrong term , but will accept it for now.  That has no indication of any "dependency / relation" of actual physical device. In your terminology - no device driver is involved.

    Next (logical) step is to assign a socket to this "dev_id" - still no device driver is involved. I would actually say - "locating dev_id and assigning socket to it is the lowest, basic "access " to Bluetooth INDEPENDENT of actual hardware in use.

    Am I correct or not, so far ?

    Since I eventually need the Bluetooth address of the

    local device I am currently implementing ( a copy, not my original code  ) / modifying the following code:

    Unfortunately this code ADDS usage of "ioctl" , to HCI usage , which I am NOT ready to analyze.

    // Create a hci socket

    // combines hci_get_route and hci_open_dev


    int hciSocket = hci_open_dev(hci_get_route(NULL));
    if (hciSocket < 0)
    {
    perror("Failed to open HCI device");
    return 1;
    }

    // Create int and pointers to hold results for later
    int devCount;
    struct hci_dev_list_req *devList;
    struct hci_dev_info devInfo;

    // Allocate memory for devList pointer. Based on HCI_MAX_DEV (maximum number of HCI devices) multiplied by the size of struct hci_dev_req, plus the size of uint16_t (to store the device number)
    devList = (struct hci_dev_list_req *)malloc(HCI_MAX_DEV * sizeof(struct hci_dev_req) + sizeof(uint16_t));
    if (!devList)
    {
    perror("Failed to allocate HCI device request memory");
    close(hciSocket);
    return 1;
    }

    // Honestly not sure why we have to do this here?
    devList->dev_num = HCI_MAX_DEV;

    // Send the HCIGETDEVLIST command to get the device list.
    if (ioctl(hciSocket, HCIGETDEVLIST, (void *)devList) < 0)
    {
    perror("Failed to get HCI device list");
    free(devList);
    close(hciSocket);
    return 1;

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • BigG
    BigG 10 months ago in reply to anneranch

    Asking about device driver was just a framing question to understand your hardware config, as in my experience not all implementations are exactly the same - they're mostly the same. Reason being, at some point in the process (as noted byshabaz) the device driver takes over to perform the actual scanning process and return a data structure back to the bluez software for your application. This scanning process continues until it receives an instruction to stop. Once it stops you will be left with a device list (struct hci_dev_list_req *devList;)

    Then it's really a case of checking each item in the device list to see if one of the BLE peripheral devices matches your search requirements, or using one of the predefined filtering options.

    Usually steps involve a "power on" or initialisation process, then a "scan on" process to capture a list or to capture a device that matches the filter parameters. Then there's a connection process and finally, if requested, a bonding/pairing process.

    Based on your code, it appears you've allocated some memory for the scan list data structure based on HCI_MAX_DEV and have a check that the list received is not empty.

    https://git.kernel.org/pub/scm/bluetooth/bluez.git/about/

    https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/client

    https://people.csail.mit.edu/albert/bluez-intro/c404.html

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • anneranch
    anneranch 10 months ago in reply to BigG

    Thanks for reply. Appreciate your contribution.

    BigG said:
    people.csail.mit.edu/.../quote]

    The above is my "template". It is basically C code and if it does the job I do not argue with it. Yes , it identifies  "local devices(s) " and "inquires" about "remote devices". At this time I have a small issue - I  am not getting "remote device name" when I have TWO "local devices". So it is back to "hci_inquiry" source code. I am getting flustered with"bluez" using interchangeably "dev_id" or "socket" without much warning.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • anneranch
    anneranch 10 months ago in reply to anneranch

    I think I found the answer to my problems with Bluetooth.

    https://affix.sourceforge.net/affix-doc/index.html

    It is "unofficial" but seems to "go lower' than "blueZ".

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • anneranch
    anneranch 10 months ago in reply to anneranch

    There is more affix. Looks as "updated" from the previous doc.

    Now for stupid question.

    It has file for "Linux kernel 2.6".

    My Ubuntu kernel is "6".

    Is it same as 2.6??

    nov25-1@nov251-desktop:~$ uname -srm
    Linux 6.8.0-45-generic x86_64
    nov25-1@nov251-desktop:~$

    Download for 2.6 kernels
    ChangeLog: affix, affix-kernel.

    NB. Install affix-kernel package first and then affix package.
    NB.2. Affix 3.X.Y are for 2.6 kernels only ! (Support for 2.4 kernels here)

    Latest testing version (for developers) Package File to download Latest patch
    affix for 2.6 kernels affix-3.2.0.tar.gz
    Size: 468K, MD5 affix_320_sec.patch
    Size: 1.9K, MD5
    affix-kernel for 2.6 kernels affix-kernel-3.2.0.tar.gz
    Size: 260K, MD5 patch_hci_3_2_0
    Size: 901 , MD5

    Latest stable version (for end users) Package File to download Latest patch
    affix for 2.6 kernel affix-3.2.0.tar.gz
    Size: 468K, MD5 affix_320_sec.patch
    Size: 1.9K, MD5
    affix-kernel for 2.6 kernel affix-kernel-3.2.0.tar.gz
    Size: 260K, MD5 patch_hci_3_2_0
    Size: 901 , MD5

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • javagoza
    javagoza 10 months ago in reply to anneranch

    Linux kernel version history - Wikipedia

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • anneranch
    anneranch 10 months ago in reply to javagoza

    My Ubuntu kernel is "6".

    Is it same as 2.6??

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • javagoza
    javagoza 10 months ago in reply to anneranch

    No, version 2.6 is quite old; it's over 20 years old.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • anneranch
    anneranch 10 months ago in reply to javagoza

    Thank you. Then Affix for "version 2.6" is also old? Besides - I do not want to install it anyway _ I am still searching for source code for "hci_inquiry".

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube