Skip to content

Instantly share code, notes, and snippets.

@dnicolson
Created March 10, 2020 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dnicolson/8d4ae9d6731a1f0cc8d7a1ed36263874 to your computer and use it in GitHub Desktop.
Save dnicolson/8d4ae9d6731a1f0cc8d7a1ed36263874 to your computer and use it in GitHub Desktop.
IOBluetooth code to log when devices are connected and disconnected
// clang -o bluetooth-connection bluetooth-connection.m -framework Foundation -framework IOBluetooth
#import <Foundation/Foundation.h>
#import <IOBluetooth/IOBluetooth.h>
@interface BluetoothConnection : NSObject {
}
@end
@implementation BluetoothConnection
-(id)init {
self = [super init];
if (self) {
[IOBluetoothDevice registerForConnectNotifications:self selector:@selector(deviceIsConnected:fromDevice:)];
}
return self;
}
-(void)deviceDidDisconnect:(IOBluetoothUserNotification*)notification fromDevice:(IOBluetoothDevice*)device {
NSLog(@"%@ (%@) disconnected", [device name], [device addressString]);
}
-(void)deviceIsConnected:(IOBluetoothUserNotification*)notification fromDevice:(IOBluetoothDevice*)device {
NSLog(@"%@ (%@) connected", [device name], [device addressString]);
[device registerForDisconnectNotification:self selector:@selector(deviceDidDisconnect:fromDevice:)];
}
@end
int main(int argc, const char *argv[])
{
@autoreleasepool {
BluetoothConnection *bluetoothConnection = [[BluetoothConnection alloc] init];
[[NSRunLoop currentRunLoop] run];
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment