Skip to content

Instantly share code, notes, and snippets.

@alpereira7
Created January 12, 2021 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alpereira7/a268814e203b5cdc7cd0a8c1ebdc1b59 to your computer and use it in GitHub Desktop.
Save alpereira7/a268814e203b5cdc7cd0a8c1ebdc1b59 to your computer and use it in GitHub Desktop.
// Simple program to read and write Class of Device.
// To compile: `gcc -o rw_cod rw_cod.c -lbluetooth`
// To install lbluetooth: `apt-get install libbluetooth-dev`
// Execute with 'sudo'
// Note the original CoD first because it has strict rules, you might need to write it back later.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> // for close()
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
int main(void){
int dev_id; // ID of Bluetooth adapter
int sock;
// Get Device ID
dev_id = hci_devid("00:11:22:33:44:55"); // To know adapter adress: `hciconfig -a`
// Open Socket
sock = hci_open_dev(dev_id);
// Write and read CoD
int timeout = 1000;
uint8_t cls[3] = {0};
int class;
int ret;
uint32_t new_class = 0x260420;
ret = hci_read_class_of_dev(sock, cls, timeout);
class = cls[0] | cls[1] << 8 | cls[2] << 16;
printf("Class of Device = 0x%06x [%d]\n", class, ret);
// Set CoD
ret = hci_write_class_of_dev(sock, new_class, 2000);
printf("Required new Class of Device = 0x%06x [%d]\n", new_class, ret);
// Check read CoD
ret = hci_read_class_of_dev(sock, cls, timeout);
class = cls[0] | cls[1] << 8 | cls[2] << 16;
printf("Actual new Class of Device = 0x%06x [%d]\n", class, ret);
close(sock);
printf("OK!\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment