Skip to content

Instantly share code, notes, and snippets.

@grimpy
Last active July 18, 2022 16:22
Show Gist options
  • Save grimpy/dba849e7583a6c24b30c736f167156af to your computer and use it in GitHub Desktop.
Save grimpy/dba849e7583a6c24b30c736f167156af to your computer and use it in GitHub Desktop.
// Include the most common headers from the C standard library
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Include the main libnx system header, for Switch development
#include <switch.h>
#define MAX_DEVICES 20
// See also libnx pad.h / hid.h.
void print_hex(char* data[], int length) {
for (int i = 0; i < length; i++) {
printf("%02x ", data[i]);
}
}
// Main program entrypoint
int main(int argc, char* argv[])
{
// This example uses a text console, as a simple way to output text to the screen.
// If you want to write a software-rendered graphics application,
// take a look at the graphics/simplegfx example, which uses the libnx Framebuffer API instead.
// If on the other hand you want to write an OpenGL based application,
// take a look at the graphics/opengl set of examples, which uses EGL instead.
consoleInit(NULL);
setInitialize();
setsysInitialize();
// Configure our supported input layout: a single player with standard controller styles
padConfigureInput(1, HidNpadStyleSet_NpadStandard);
PadState pad;
padInitializeDefault(&pad);
SetSysBluetoothDevicesSettings settings[MAX_DEVICES];
s32 total_out;
printf("\x1b[1;1HPress PLUS to exit.");
printf("\x1b[2;1H");
int line = 4;
Result rc = setsysGetBluetoothDevicesSettings(&total_out, &settings, MAX_DEVICES);
if (R_SUCCEEDED(rc)) {
for (int i = 0; i < total_out; i++)
{
SetSysBluetoothDevicesSettings *device = &settings[i];
if (device->addr.address) {
printf("\x1b[%d;1HName: %s %d:%d %x btAddr: ", line, device->name2, device->vid, device->pid, device->sub_class);
for (int i = 0; i < 6; i++) {
printf("%02x ", device->addr.address[i]);
}
line++;
printf("\x1b[%d;1H Type: %d Key: ", line, device->key_type);
for (int i = 0; i < 16; i++) {
printf("%02x ", device->link_key[i]);
}
line++;
}
}
// Print link key etc
}
// Main loop
while(appletMainLoop())
{
// Scan the gamepad. This should be done once for each frame
padUpdate(&pad);
// padGetButtonsDown returns the set of buttons that have been
// newly pressed in this frame compared to the previous one
u64 kDown = padGetButtonsDown(&pad);
// padGetButtons returns the set of buttons that are currently pressed
if (kDown & HidNpadButton_Plus)
break; // break in order to return to hbmenu
// Update the console, sending a new frame to the display
consoleUpdate(NULL);
}
// Deinitialize and clean up resources used by the console (important!)
consoleExit(NULL);
return 0;
}
@ndeadly
Copy link

ndeadly commented Jul 18, 2022

setsysGetBluetoothDevicesSettings(&total_out, &settings, i);

You shouldn't call this in a loop, but instead define an array of SetSysBluetoothDevicesSettings to receive the entries and then loop on the output. From firmware 13.0.0+ the database can contain up to 20 entries. Prior to that it was 10.

Something like

#define MAX_DEVICES 20

SetSysBluetoothDevicesSettings settings[MAX_DEVICES];

s32 total_out;
Result rc = setsysGetBluetoothDevicesSettings(&total_out, settings, MAX_DEVICES);
if (R_SUCCEEDED(rc)) {
    for (int i = 0; i < total_out; ++i) {
        SetSysBluetoothDevicesSettings *device = &settings[i];
        
        // Print link key etc
    }
}

printf("%d ", settings.link_key[j]);

Also, you probably want to use %02x here instead of %d to print the bytes in hex, as this is likely more useful for whatever follows.

@grimpy
Copy link
Author

grimpy commented Jul 18, 2022

thanks for the comments, it wasnt really clear from the defenition to me and I'm not really a c guy :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment