使用c代码

时间:2016-06-08 19:56:58

标签: bluetooth fedora bluez

我想创建一个启动的蓝牙服务器,其他设备可以与它配对。

我可以让代码编译好,服务器启动,但是当我从android手机扫描蓝牙设备时,我看不到广告。

我想我需要打开可发现模式。

我尝试在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;
}

要设置蓝牙可发现模式需要做什么?

3 个答案:

答案 0 :(得分:3)

通过BlueZ命令行,可通过hciconfig工具实现启用可发现性的命令,如下所示:

hciconfig hciX piscan

其中hciX是系统上安装的HCI设备,大多数情况下都是hci0。您可以尝试上面的命令,如果它没有返回错误,那么您应该能够通过执行查询来通过Android设备看到您的Linux机器。如果这对您有用,那么您可以在以下链接的hciconfig源中找到您需要的代码: -

https://github.com/aguedes/bluez/blob/master/tools/hciconfig.c

然后你的出发点将是以下功能: -

static void cmd_scan(int ctl, int hdev, char *opt)
{
    struct hci_dev_req dr;

    dr.dev_id  = hdev;
    dr.dev_opt = SCAN_DISABLED;
    if (!strcmp(opt, "iscan"))
        dr.dev_opt = SCAN_INQUIRY;
    else if (!strcmp(opt, "pscan"))
        dr.dev_opt = SCAN_PAGE;
    else if (!strcmp(opt, "piscan"))
        dr.dev_opt = SCAN_PAGE | SCAN_INQUIRY;

    if (ioctl(ctl, HCISETSCAN, (unsigned long) &dr) < 0) {
        fprintf(stderr, "Can't set scan mode on hci%d: %s (%d)\n",
                        hdev, strerror(errno), errno);
        exit(1);
    }
}

我希望这会有所帮助

答案 1 :(得分:0)

我相信你想要hci_inquiry()函数。它在蓝牙适配器上启动设备发现。

int hci_inquiry (int adapter_id, int len, int max_rsp,
    const uint8 t *lap, inquiry_info **devs, long flags);

这是一个很好的教程: http://people.csail.mit.edu/albert/bluez-intro/c404.html#bzi-choosing

答案 2 :(得分:0)

要将Youssif的答案进一步发展,可以使用以下代码

#include <bluetooth/hci.h>
#include <sys/ioctl.h>

int ctl = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
// Open HCI socket
if (ctl < 0) {
    // Can't open HCI socket
}

struct hci_dev_req dr;
dr.dev_id = 0;  // hci0
dr.dev_opt = SCAN_DISABLED;
dr.dev_opt = SCAN_PAGE | SCAN_INQUIRY;

if (ioctl(ctl, HCISETSCAN, (unsigned long)&dr) < 0) {
   // Can't set scan mode
} else {
   // "hci0 is now discoverable
}

close(ctl);

// Normal setup of rfcomm
int socket = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
// ....