Skip to content
Advertisement

Prevent bluetooth driver actions?

I want to controll my bluetooth controller completely programmatically with HCI commands. (Basically mimic the behaviour of other bluetooth controllers, e.g. of android devices) However, with the driver running, there will always be unwanted commands sent automatically. Is it somehow possible to prevent this? Or is this only possible by basically writing my own driver for the bluetooth adapter? I didn’t really try much (other than systemctl stop bluetooth, which obviously doesn’t work) because I don’t know where to start. Maybe I can block outgoing bluetooth commands somewhow, and only shortly enable it when I am sending something? Thanks.

Edit: I can of course send my hci message, then turn off the hci device -> this way only my message is sent. But I still want to receive incoming message to the controller. I just don’t want the host to send message I don’t specify.

Edit2: According to Direct Control of HCI Device (Bypass Bluetooth Drivers) on Linux there is HCI_USER_CHANNEl, which is basically what I want. I took the code from there to write a function:

#include <errno.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BTPROTO_HCI   1

struct sockaddr_hci {
sa_family_t     hci_family;
unsigned short  hci_dev;
unsigned short  hci_channel;
};

extern "C" int usocket(){

    int sock = socket(AF_BLUETOOTH, SOCK_RAW ,  BTPROTO_HCI);
    struct sockaddr_hci a;

    memset(&a, 0, sizeof(a));
    a.hci_family = AF_BLUETOOTH;
    a.hci_dev = 0; //0 for hci0
    a.hci_channel = 1; //1 for HCI_CHANNEL_USER

    int ret = bind(sock, (struct sockaddr *) &a, sizeof(a));
    if(ret==-1){
       perror("Error binding socket. Errno: ");
       return -1;
    }
    return sock;
  }

However, calling it always leads to “device or resource busy”, but I have nothing running on the hci device. Any advice?

Advertisement

Answer

sudo hciconfig hci0 down before creating user channel socket is needed. Other than that, see my edits. Marking this as done.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement