Making bluetooth discoverable using c code
Through the BlueZ command line, the command to enable discoverability is achieved through the hciconfig tool as follows:
hciconfig hciX piscan
Where hciX is your HCI device installed on the system, and in most cases will be hci0. You can try the above command and if it doesn't return an error then you should be able to see your Linux machine through your Android device by performing an inquiry. If this does work for you, then the code that you need can be found in the hciconfig source in the link below:-
https://github.com/aguedes/bluez/blob/master/tools/hciconfig.c
And then your starting point will be the following function:-
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);
}
}
I hope this helps