Problem with getting list of HCI devices from driver in Android(在Android中从驱动程序获取HCI设备列表时出现问题)
问题描述
我必须使用Android的HCI设备,所以我尝试实现一个简单的代码来获取蓝牙设备的数量:
...
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_admin。并且我以简单用户身份运行应用程序,而不是以root用户身份运行。
Tnx.
推荐答案
好的,我找到了原因:设备不会响应简单用户(并且与您在程序清单中请求的权限无关)。只有当您是root用户(或来自内核模式)时,才能直接与设备通信。这意味着您不能直接从常规应用程序使用设备。非常难过:(
这篇关于在Android中从驱动程序获取HCI设备列表时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!