How to program a Bluetooth LE device using C on Linux x86?
Asked Answered
J

3

8

I have a bluetooth device which I can control using gatttool on linux. I want to develop my own c program that can send commands to it.

I have done bluetooth programming in the past and it is relatively straightforward, similar to network programming but this time, it is a bluetooth low energy device and following the principles here results in a host is down message when I can clearly connect/disconnect from it using gatttool.

How do I create this program? I know I should be using the bluez library but I am not sure where to start with Low energy devices.

int main(int argc, char **argv)
{
   struct sockaddr_rc addr = { 0 };
   int s, status;
   char dest[18] = "B4:99:4C:5C:EE:49";
   char buf[2048];
   pthread_t rthread;

   setbuf(stdout, NULL); 
   // allocate a socket
   s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
   // set the connection parameters (who to connect to)
   addr.rc_family = AF_BLUETOOTH;
   addr.rc_channel = (uint8_t) 1;
   str2ba( dest, &addr.rc_bdaddr );
   // connect to server
   status = connect(s, (struct sockaddr *)&addr, sizeof(addr));

   if( status < 0 ){
      perror("Error connecting to host\n");
      exit(1);
   }

   while(fgets(buf, sizeof(buf), stdin) != NULL){
      status = send(s, buf, sizeof(buf), 0);
      if(status < 0){
         printf("Error sending.\n");
     exit(1);
      }
   }

   close(s);

   return;
Jacksonjacksonville answered 27/3, 2015 at 20:20 Comment(0)
R
5

I've been trying to figure out how to do this too: you may want to take a look at the source code in sandeepmistry/noble:src/l2cap-ble.c on Github. (The C component was factored out in this commit, so you need to look at older versions of the source.)

After building it (requires libbluetooth-dev) and running it, the l2cap-ble example essentially creates a simple TTY-like connection to the BLE device:

$ gcc -o l2cap-ble l2cap-ble.c utility.c -lbluetooth
$ ./l2cap-ble 12:34:56:78:9A:BC [public|random]

The source code illustrates a few BLE-specific functions (hci_*) that need to be interspersed with the standard socket I/O code.

UPDATE: I wrote a much more substantial and fully-functional program starting from this code: https://github.com/dlenski/ttblue. You can use this source code as an example of how to talk to a BLE gadget using Bluez.

Ruination answered 29/7, 2015 at 2:21 Comment(2)
Your code is not available any more. Can you post it please?Interlining
It's not my code. However, I wrote a much more substantial and fully-functional program based on it. You can use this as an example of how to talk to a BLE gadget using Bluez: github.com/dlenski/ttblueRuination
X
2

your program is for classic bluetooth,to support my statement i would say ON any classic bluetooth device your code work would work fine

To get lescan i suggest to go though this link.sudo ./st would scan for nearby ble divices

https://github.com/carsonmcdonald/bluez-experiments

Xl answered 28/3, 2015 at 14:23 Comment(0)
R
0

Another project on github.com looks clean: https://github.com/edrosten/libblepp

It's been mentioned in a discussion here: https://mbientlab.com/community/discussion/2492/bluetooth-le-library-linux

It's C++ not C though.

Radiolarian answered 17/12, 2020 at 17:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.