raspberry-pibluetoothraspberry-pi-zeroraspberry-os

Unable to connect to bluetooth remote from Raspberry Pi Zero 2 W


I'm a newbie to Raspberry Pi. I'm trying to write an app that interacts with an Amazon Fire TV bluetooth remote but the app can't connect.

I know the bluetooth controller is working because my keyboard and mouse are connected and work. I also paired the remote. bluetoothctl devices looks like this:

Device 0C:EE:99:17:54:F0 AR               <---- remote
Device D6:07:DC:1E:41:E9 MX MCHNCL
Device FE:DF:F9:FC:16:CF M720 Triathlon

Running bluetoothctl info 0C:EE:99:17:54:F0 produces this:

Device 0C:EE:99:17:54:F0 (public)
    Name: AR
    Alias: AR
    Appearance: 0x0180
    Paired: yes
    Bonded: yes
    Trusted: no
    Blocked: no
    Connected: yes
    LegacyPairing: no
    UUID: Generic Access Profile    (00001800-0000-1000-8000-00805f9b34fb)
    UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb)
    UUID: Device Information        (0000180a-0000-1000-8000-00805f9b34fb)
    UUID: Battery Service           (0000180f-0000-1000-8000-00805f9b34fb)
    UUID: Human Interface Device    (00001812-0000-1000-8000-00805f9b34fb)
    UUID: Scan Parameters           (00001813-0000-1000-8000-00805f9b34fb)
    UUID: Vendor specific           (5de20000-5e8d-11e6-8b77-86f30ca893d3)
    UUID: Vendor specific           (cfbfa000-762c-4912-a043-20e3ecde0a2d)
    UUID: Vendor specific           (fe151500-5e8d-11e6-8b77-86f30ca893d3)
    Modalias: usb:v0171p0413d0000
    ManufacturerData Key: 0x0171
    ManufacturerData Value:
  04 13                                            ..              
    Battery Percentage: 0x59 (89)

As far as I can tell, everything looks ok up to this point. Pressing the directional pad on the remote is like pressing the arrow keys on the keyboard, so it's definitely connected an in input device. Here is my program, which I got from Google Gemini:

#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#include <bluetooth/l2cap.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
    int dev_id = hci_get_route(NULL);
    int sock = hci_open_dev(dev_id);

    if (dev_id < 0 || sock < 0) {
        perror("Error opening HCI device");
    }

    inquiry_info *ii = NULL;
    int max_rsp = 255;
    int num_rsp;
    ii = (inquiry_info*) malloc(max_rsp * sizeof(inquiry_info));
    num_rsp = hci_inquiry(dev_id, 8, max_rsp, NULL, &ii, IREQ_CACHE_FLUSH);
    if (num_rsp < 0) {
        perror("HCI inquiry failed");
        close(sock);
        return 1;
    }
    printf("num_rsp = %d\n", num_rsp);

It outputs num_rsp = 0 every time.

...cont'd

    for (int i = 0; i < num_rsp; i++) {
        char addr[19];
        ba2str(&ii[i].bdaddr, addr);
    }
    free(ii);

    bdaddr_t target_bdaddr;
    str2ba("0C:EE:99:17:54:F0", &target_bdaddr);

    uint16_t handle;
    uint16_t ptype = HCI_DM1 | HCI_DM3 | HCI_DM5 | HCI_DH1 | HCI_DH3 | HCI_DH5;

    if (hci_create_connection(sock, &target_bdaddr, htobs(ptype), 0, 0, &handle, 0) < 0) {
        perror("Failed to create HCI connection");
    } else {
        printf("Connection established.");
    }

    return 0;
}

Trying to connect to 0C:EE:99:17:54:F0 fails every time. Running as myself, the error is "Operation not permitted." With sudo, it's "Input/output error".

Any suggestions are much appreciated!


Solution

  • If the remote has been connected via bluetoothctl, then no further Bluetooth coding is required. The OS makes the incoming data available via a file. On my system it is /dev/input/event5 - but it will be one of the "event" files. Just open this file and read the data. Your only problem is the data that the OS passes through as the keyboard input that you have seen.

    See inputs as buttons are pressed from the command line via
    hd /dev/input/event5
    
    OR C code
    FILE *stream;
    int c;
    stream = fopen("/dev/input/event5","rb");
    c = fgetc(stream);  // reads one byte
                        // but should come in blocks of 16 bytes for every button press