Bluetooth programming-device retrieval.

Source: Internet
Author: User
Tags socket error
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.