Continuous Bluetooth Device Discovery “Inquisition”

This program puts your bluetooth controller into the periodic Inquiry mode. The controller is hereinafter referred to as a dongle in the program, to avoid confusion with other things sometimes called controllers, such as your PCI-USB bridge) into periodic Inquiry mode.

See the Bluetooth specifications available via http or https with assignments via http or https and the corresponding baseband 802.15

hcitool spinq essentially invokes the command defined in Core 2.0 + EDR Volume 2 Part E section 7.1.3 with the RSSI information enabled via section 7.3.54. Inquisition reads both of those formats and was based on the information from those specifications, for BlueZin Linux

It then reports the comings and goings of discoverable bluetooth devices in the vincinity to standard out, suitable for piping into the standard in of something like netcat6, for reporting to a central location.

With a suitably recent version of Linux like 2.6.11 other bluetooth programs can often be used even when inq is left running in the background. The results get distorted somewhat tho’ugh…

The program is comprised of the following files.

Download it

The main Inquisition discover program
Decodes inquiry results for standard out.
A header file - links it all together.
The make file - you need the lib bluetooth and its includes. installed to make this.

It can be run like this, then it will send UDP packets for detected phones, etc. and listen for commands.
nc6 -6 -u -p 50001 ip6-localhost 50000 --exec="./inq -p" > /dev/null 2>&1 < /dev/null &

Some explanation of the options

-h or --help

Output a small description of the program and option information

-d or --debug

Normally the program tries to conserve bandwidth by only producing an event when a device has appeared or disappeared, and I do that by keeping track of the devices detected over the inquiry before last and not reporting unless there are changes in detected devices.

However you may like to see how fast your dongle detects devices on every periodic inquiry, and so with this I output the time since the start of the inquiry along with the device MAC or bdaddr and a decoded device flags, every time the dongle reports an inquiry result.

-i or --device

Not well tested as I only have the one dongle connected, but this would allow you to choose which dongle this program uses.

-s or --set

This option causes inq to try to enable periodic inquiry itself, it only works if it is running as root or has the cap_net_raw capability given to the process. It is best instead to start periodic inquiry with hcitool sqinq and exit it with hcitool epinq.

-l or --length

This is the interesting option as it controls the length of inquiry in 1.28 second units. It is only used with the --set option. By default it is set to 8.

No Warranty

This program links against the bluetooth library, and thusly when compiled becomes subject to the conditions of that license, which at the time of writing was the GNU GPL version 2.

It is possible to use with obexpushd to receive files and ussp-push to send them.

With ussp-push I would rather read data from a pipe or stdin than a file, so I replaced easy_readfile in obex_main.c with this:

uint8_t *easy_readfile(const char *name, int *size)
        int fd;
        size_t capacity = 4;
        ssize_t r;
        uint8_t *buf = NULL;
        uint8_t *newbuf;

        *size = 0;

        fd = open(name, O_RDONLY);
        if (fd == -1) {
                return NULL;

        do {
                capacity *= 2;
                if (!(newbuf = realloc(buf, capacity))) {
                        if (buf != NULL) {
                                newbuf = realloc(buf, 0);
                        return NULL;
                buf = newbuf;
                r = read(fd, buf + *size,capacity - *size);
                if (r < 0) {
                        if (buf != NULL) {
                                newbuf = realloc(buf, 0);
                        return NULL;
                *size += r;
        } while (capacity == *size);

        return buf;

OBEX push is not always the first advertised service on a device, it may help to look it up with sdptool rather than let ussp-push do it.

while read
        if test "${REPLY:0:13}" = "    Channel: "
done <<<"$(sdptool search --bdaddr ${ADDR} opush)"

ussp-push ${ADDR}@${CHANNEL} ${FILE} ${NAME}

Contact via XMPP