I am trying to connect to bluetooth using C program via Raspberry pi/ beagle bone. The program can scan & connect with the desired device , also finds the RSSI value. But it couldnt pair up with the device like how the normal device pairing happens
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#include <bluetooth/rfcomm.h>
#include <sys/ioctl.h>
#define MAX_DATA_SIZE 100
int main() {
inquiry_info *devices = NULL;
int max_devices = 10;
int num_devices;
int device_id = hci_get_route(NULL);
int hci_socket = hci_open_dev(device_id);
if (device_id < 0 || hci_socket < 0) {
perror("Error opening HCI socket");
exit(1);
}
devices = (inquiry_info*)malloc(max_devices * sizeof(inquiry_info));
num_devices = hci_inquiry(device_id, 8, max_devices, NULL, &devices, IREQ_CACHE_FLUSH);
if (num_devices < 0) {
perror("Error while inquiring devices");
exit(1);
}
printf("Found %d devices:\n", num_devices);
for (int i = 0; i < num_devices; i++) {
char address[19];
ba2str(&(devices[i].bdaddr), address);
// Get the device name
char name[248];
if (hci_read_remote_name(hci_socket, &(devices[i].bdaddr), sizeof(name), name, 0) < 0)
strcpy(name, "[unknown]");
printf("%d. %s - %s\n", i+1, address, name);
}
char target_name[248];
printf("Enter the name of the device to connect: ");
scanf("%s", target_name);
int device_choice = -1;
for (int i = 0; i < num_devices; i++) {
char name[248];
if (hci_read_remote_name(hci_socket, &(devices[i].bdaddr), sizeof(name), name, 0) < 0)
continue;
if (strcmp(target_name, name) == 0) {
device_choice = i + 1;
break;
}
}
if (device_choice == -1) {
printf("Device '%s' not found\n", target_name);
free(devices);
close(hci_socket);
return 1;
}
printf("Connected to device '%s' successfully\n", target_name);
// Get the selected device's address
bdaddr_t target_address = devices[device_choice - 1].bdaddr;
char target_address_str[18];
ba2str(&target_address, target_address_str);
// Establish an RFCOMM Bluetooth connection
int connection_socket = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
struct sockaddr_rc loc_addr = { 0 }, rem_addr = { 0 };
loc_addr.rc_family = AF_BLUETOOTH;
loc_addr.rc_bdaddr = *BDADDR_ANY;
loc_addr.rc_channel = (uint8_t)1;
bind(connection_socket, (struct sockaddr *)&loc_addr, sizeof(loc_addr));
rem_addr.rc_family = AF_BLUETOOTH;
rem_addr.rc_channel = (uint8_t)1;
str2ba(target_address_str, &rem_addr.rc_bdaddr);
connect(connection_socket, (struct sockaddr *)&rem_addr, sizeof(rem_addr));
printf("Connection established successfully\n");
// Get the RSSI value of the connected device
struct hci_conn_info_req req;
bdaddr_t bdaddr;
uint8_t handle;
int8_t rssi;
str2ba(target_address_str, &bdaddr);
bacpy(&req.bdaddr, &bdaddr);
req.type = ACL_LINK;
ioctl(hci_socket, HCIGETCONNINFO, (unsigned long)&req);
handle = req.conn_info->handle;
if (hci_read_rssi(hci_socket, handle, &rssi, 1000) < 0) {
perror("Error reading RSSI value");
free(devices);
close(connection_socket);
close(hci_socket);
return 1;
}
printf("RSSI value of the connected device: %d dBm\n", rssi);
// Data Transmission and Reception
char data[MAX_DATA_SIZE];
char received_data[MAX_DATA_SIZE];
printf("Enter the data to send: ");
scanf("%s", data);
// Send data
int data_length = strlen(data);
int bytes_sent = write(connection_socket, data, data_length);
if (bytes_sent < 0) {
perror("Error sending data");
free(devices);
close(connection_socket);
close(hci_socket);
return 1;
}
printf("Data sent successfully: %s\n", data);
// Receive data
int bytes_received = read(connection_socket, received_data, sizeof(received_data));
if (bytes_received < 0) {
perror("Error receiving data");
free(devices);
close(connection_socket);
close(hci_socket);
return 1;
}
received_data[bytes_received] = '\0';
printf("Data received: %s\n", received_data);
// Close the Bluetooth connection
close(connection_socket);
free(devices);
close(hci_socket);
return 0;
}
This program scans,connect with the desired device (however only displays on screen , not paired with the device). And couldn't transfer data. How to pair with device using c program?