#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdbool.h>
#include <wordexp.h>

int start_hci_scan(int start)
{
    int fd;
    int err;

    fd = hci_open_dev(0);
    if (fd < 0)
    {
        printf("open %s failed\n", 0);
        return -1;
    }

    if (start)
    {
        err = hci_le_set_scan_enable(fd, 0x01, 0x01, 10000);
        if (err < 0)
        {
            printf("set scan enable failed, err:%d; %s errno:%d\n", err, strerror(errno), errno);
            return -1;
        }

        printf("start scan\n");
    }
    else
    {
        err = hci_le_set_scan_enable(fd, 0x00, 0x00, 10000);
        if (err < 0)
        {
            printf("set scan disable failed, err:%d; %s errno:%d\n", err, strerror(errno), errno);
            return -1;
        }
        printf("stop scan\n");
    }

    return 0;
}




int main(int argc, char *argv[])
{
    if (argv[1][0] == '0')
    {
        start_hci_scan(0);
    }
    else
    {
        start_hci_scan(1);
    }
}