Skip to content

Instantly share code, notes, and snippets.

@kylemcdonald
Created December 14, 2016 21:28
Show Gist options
  • Save kylemcdonald/796d3c7cf956024b3ecb2524b30a01a3 to your computer and use it in GitHub Desktop.
Save kylemcdonald/796d3c7cf956024b3ecb2524b30a01a3 to your computer and use it in GitHub Desktop.
Small command line tool for displaying real time Bluetooth signal strength on a Mac.
// gcc -Wall -o BluetoothRSSI BluetoothRSSI.m -framework Foundation -framework IOBluetooth ; ./BluetoothRSSI
#import <Foundation/Foundation.h>
#import <IOBluetooth/IOBluetooth.h>
int width = 32; // display width in characters
int refresh = 33; // refresh rate milliseconds
int main(int argc, const char * argv[]) {
@autoreleasepool {
// only flush stdout when fflush(stdout) is called
setvbuf(stdout, NULL, _IOFBF, BUFSIZ);
IOBluetoothDevice *device;
NSArray *devices = [IOBluetoothDevice pairedDevices];
NSEnumerator *e;
int lineNumber = 0;
int halfwidth = width >> 1;
while (true) {
if(lineNumber > 0) {
printf("\033[%dF", lineNumber); // ansi escape, n=lineNumber, move n lines up
lineNumber = 0;
}
e = [devices objectEnumerator];
while (device = [e nextObject]) {
if ([device isConnected]) {
BluetoothHCIRSSIValue rssi = [device RSSI];
printf("\033[%dG", 0); // ansi escape, position 0, absolute horizontal move
if(rssi < -halfwidth) {
printf("<");
} else {
printf("[");
}
int tick = halfwidth + rssi;
for(int i = 0; i < width; i++) {
if(i == tick) {
printf("+");
} else if(i == halfwidth) {
printf("|");
} else {
printf("-");
}
}
if(rssi > halfwidth) {
printf(">");
} else {
printf("]");
}
lineNumber++;
}
}
fflush(stdout);
usleep(refresh * 1000);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment