Today, I used bluez's lib to write a small Program Debugging, running through. Have a preliminary understanding of Bluetooth programming in Linux.
This function is used to search for other Bluetooth devices and obtain their friendly device names.
# Include <stdlib. h>
# Include <unistd. h>
# Include <sys/socket. h>
# Include <Bluetooth/Bluetooth. h> // three header files of Bluetooth.
# Include <Bluetooth/HCI. h>
# Include <Bluetooth/hci_lib.h>
Int main (INT argc, char ** argv)
{
Inquiry_info * II = NULL;
Int max_rsp, num_rsp;
Int dev_id, Sock, Len, flags;
Int I;
Char ADDR [19] = {0 };
Char name [248] = {0 };
Dev_id = hci_get_route (null); // obtain the first available local Bluetooth device
Sock = hci_open_dev (dev_id); // enable the bluetooth device.
If (dev_id <0 | sock <0 ){
Perror ("Opening socket error ");
Exit (1 );
}
Len = 8;
Max_rsp = 255;
Flags = ireq_cache_flush;
II = (inquiry_info *) malloc (max_rsp * sizeof (inquiry_info ));
Printf ("Start search.../N ");
Num_rsp = hci_inquiry (dev_id, Len, max_rsp, null, & ii, flags); // search for devices nearby
If (num_rsp <0) perror ("hci_inquiry error ");
For (I = 0; I <num_rsp; I ++ ){
Ba2str (& (ii + I)-> bdaddr, ADDR );
Memset (name, 0, sizeof (name ));
If (hci_read_remote_name (sock, & (ii + I)-> bdaddr, sizeof (name ),
Name, 0) <0) // query the friendly device name of the device
Strcpy (name, "[UNKNOWN]");
Printf ("% S % s/n", ADDR, name );
}
Printf ("End search./N ");
Free (II );
Close (sock );
Return 0;
}
Finally, I compiled the program and passed it. But it has not been tested. I will upload it to the board tomorrow.
Bluetooth has not been officially run on the board, and there may be many problems.