【发布时间】:2016-10-09 06:27:45
【问题描述】:
我想做一个蓝牙服务器,可以启动,其他设备可以与之配对。
我可以正常编译代码,服务器启动,但是当我从安卓手机扫描蓝牙设备时看不到广告。
我想我需要打开可发现模式。
我尝试在 hci_lib.h 中搜索使蓝牙可被发现但找不到的函数。
这是我到现在为止的代码。
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#include <bluetooth/rfcomm.h>
int main(int argc, char **argv)
{
struct sockaddr_rc loc_addr = { 0 }, rem_addr = { 0 };
char buf[1024] = { 0 };
int s, client, bytes_read,dev_id,sock;
socklen_t opt = sizeof(rem_addr);
dev_id = hci_get_route(NULL);
if (dev_id < 0) {
perror("No Bluetooth Adapter Available");
exit(1);
}
if (hci_devinfo(dev_id, &dev_info) < 0) {
perror("Can't get device info");
exit(1);
}
sock = hci_open_dev( dev_id );
if (sock < 0) {
perror("HCI device open failed");
free(info);
exit(1);
}
// allocate socket
s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
// bind socket to port 1 of the first available
// local bluetooth adapter
loc_addr.rc_family = AF_BLUETOOTH;
loc_addr.rc_bdaddr = *BDADDR_ANY;
loc_addr.rc_channel = (uint8_t) 1;
bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr));
// put socket into listening mode
listen(s, 1);
// accept one connection
client = accept(s, (struct sockaddr *)&rem_addr, &opt);
ba2str( &rem_addr.rc_bdaddr, buf );
fprintf(stderr, "accepted connection from %s\n", buf);
memset(buf, 0, sizeof(buf));
// read data from the client
bytes_read = read(client, buf, sizeof(buf));
if( bytes_read > 0 ) {
printf("received [%s]\n", buf);
}
// close connection
close(client);
close(s);
return 0;
}
如何设置蓝牙可发现模式?
【问题讨论】: