2
越来越HCI设备的列表,从司机我与Android的人机交互设备的工作,所以我尽量实现一个简单的代码获得蓝牙设备的数量:问题与Android的
...
struct hci_dev_req *dr;
int sk = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
if (sk < 0)
{
res = "invalid socket";
goto end;
}
struct hci_dev_list_req *dl = malloc(HCI_MAX_DEV * sizeof(*dr) + sizeof(*dl));
if (!dl)
{
res = "not enough memory";
goto end;
}
memset(dl, 0, HCI_MAX_DEV * sizeof(*dr) + sizeof(*dl));
dl->dev_num = HCI_MAX_DEV;
dr = dl->dev_req;
if (ioctl(sk, HCIGETDEVLIST, (void *) dl) < 0)
{
res = "unable to get device list";
goto end;
}
if(dl->dev_num == 0)
{
res = "device list is empty";
goto end;
}
...
所以每次我收到消息“设备列表为空”。为什么会这样? 只有我在程序中的权限才能显示它们:BLUETOOTH和BLUETOOTH_ADMIN。我以简单的用户身份运行应用程序,而不是以root身份运行。
Tnx。
我假设你使用JNI来调用这段代码,它是使用C++而不是Java API API的要求吗? –
是的,我使用的是JNI,因此它是唯一使用hci设备直接(ioctl(int,int,...)funciton)。Android API不实现hci(低级别)协议,但只有rfcomm(高级别),但它不足以实现我想实现的目标。 – Borg8