Created
March 10, 2020 21:34
-
-
Save dnicolson/8d4ae9d6731a1f0cc8d7a1ed36263874 to your computer and use it in GitHub Desktop.
IOBluetooth code to log when devices are connected and disconnected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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