How to connect BLE devices using Linux bluetooth C library
This is what the old hci_xxx bluetooth C functions are doing at the lowest level. They probably don't work now because bluez/dbus is getting in the way. The following code works on a Raspberry Pi because it disables bluez first, and could be the basis of a C program - but it would be much easier to use one of the github libraries mentioned in the comments.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
struct sockaddr_hci
{
unsigned short hci_family;
unsigned short hci_dev;
unsigned short hci_channel;
};
struct hci_filter
{
unsigned long type_mask;
unsigned long event_mask[2];
unsigned short opcode;
};
#define BTPROTO_HCI 1
#define SOL_HCI 0
#define HCI_FILTER 2
#define HCIDEVDOWN 0x400448CA
unsigned char eventmask[16] = { 1,1,0x0C,8,0xFF,0xFF,0xFB,0xFF,0x07,0xF8,0xBF,0x3D };
unsigned char lemask[16] = { 0x01,0x01,0x20,0x08,0xBF,0x05,0,0,0,0,0,0 };
unsigned char leopen[30] = {1,0x0D,0x20,0x19,0x60,0,0x60,0,0,0,0x66,0x55,0x44,0x33,0x22,0x11,0,0x18,0,0x28,0,0,0,0x11,0x01,0,0,0,0};
int main()
{
int n,len,dd;
struct sockaddr_hci sa;
struct hci_filter flt;
char buf[256];
// set board address 00:1E:C0:2D:17:7C
leopen[15] = 0x00;
leopen[14] = 0x1E;
leopen[13] = 0xC0;
leopen[12] = 0x2D;
leopen[11] = 0x17;
leopen[10] = 0x7C;
dd = socket(31, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK, BTPROTO_HCI);
if(dd < 0)
{
printf("Socket open error\n");
return(0);
}
ioctl(dd,HCIDEVDOWN,0); // hci0
close(dd);
// AF_BLUETOOTH=31
dd = socket(31, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK, BTPROTO_HCI);
if(dd < 0)
{
printf("Socket open error\n");
return(0);
}
sa.hci_family = 31; // AF_BLUETOOTH;
sa.hci_dev = 0; // hci0/1/2...
sa.hci_channel = 1; // HCI_CHANNEL_USER
if(bind(dd,(struct sockaddr *)&sa,sizeof(sa)) < 0)
{
printf("Bind failed\n");
close(dd);
return(0);
}
write(dd,eventmask,12);
write(dd,lemask,12);
printf("Send hci LE connect\n");
write(dd,leopen,29);
printf("If get reply = 04 3E 13 01 00.. then has connected OK\n");
printf("REPLY =");
for(n = 0 ; n < 10 ; ++ n)
{
len = read(dd,buf,sizeof(buf));
for(n = 0 ; n < len ; ++n)
printf(" %02X",buf[n]);
printf("\n");
sleep(1);
}
printf("\nExit and disconnect\n");
}
Linux Bluetooth programming in C & socket.h
For the second question: you have to post the code you've written. Else we can't figure out what's bad.
For the first question: you need to put the library linker flags as the last parameters when invoking GCC:
gcc nxt_bt_connect.c -o nxt_bt_connect -lm -lbluetooth
Bluetooth interface control in C code ( Linux OS )
You can use the c code for hciconfig itself. Just download the BlueZ source and open tools/hciconfig.c and use the following functions:-
static void cmd_up(int ctl, int hdev, char *opt)
{
...
}
and
static void cmd_down(int ctl, int hdev, char *opt)
{
...
}
Related Topics
How to Get the List of Dependent Child Images in Docker
Keep the Window's Name Fixed in Tmux
What Is the Meaning of Each Line of the Assembly Output of a C Hello World
Use Find Command But Exclude Files in Two Directories
Linux Kernel Device Driver to Dma from a Device into User-Space Memory
Which Real-Time Priority Is the Highest Priority in Linux
Dropping of Connections with Tcp_Tw_Recycle
Shell Script to Kill the Process Listening on Port 3000
How to Check If There Are Symbolic Links Pointing to a Directory
Bash Scripting - How to Set the Group That New Files Will Be Created With
How to Ignore Some Differences in Diff Command
Using Mkdir -M -P and Chown Together Correctly
How to Move a Relative Symbolic Link
How to Output Return Code in Shell
Concatenating Two String Variables in Bash Appending Newline
Is Lib{Library Name}.A/.So a Naming Convention for Static Libraries in Linux