问题
Environment:
- Linux
- BlueZ Bluetooth stack
- C API
- No usage of the dbus interface
I must say that the HCI BlueZ C API ( hci_lib.h ) is poorly documented, having that said, Is there a C hci_* API controlling the host discover-able state? something similar to "hci_write_simple_pairing_mode" enabling control of discoverability?
回答1:
The following hci command makes the adapter discoverable
hciconfig hci0 piscan
Following is the corresponding code from hciconfig
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) {
Here ctl is what is got from:
if ((ctl = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI)) < 0)
回答2:
Solution can be found at hciconfig.c in specific at 'cmd_scan' implementation ( having opt = "piscan" ), derivation of that follows
HRESULT BluZbridge::SetDiscoverable(IN BOOL bVisible) {
if (0 == m_fdHCI)
return HRESULT_FROM_WIN32(ERROR_NOT_READY);
hci_dev_req req = { DEFAULT_HCI_ADAPTER_ID, ((TRUE == bVisible) ? (SCAN_PAGE | SCAN_INQUIRY) : SCAN_DISABLED) };
if (ioctl(m_fdHCI, HCISETSCAN, (unsigned long)&req) < 0) {
perror("Can't set scan mode on hci0");
return HRESULT_GET_ERRNO();
}
return S_OK;
}
来源:https://stackoverflow.com/questions/30058715/bluez-hci-api-to-make-the-host-discoverable