From c484af9d5844fa29be77d098542a74e313dad4c6 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 24 Mar 2019 12:45:52 +0100 Subject: HeadphoneKeyEventBluetooth: Listen for Bluetooth device pairing Register a delegate that gets called when Bluetooth devices are paired with the Mac. We now need to determine if the paired device is a pair of headphones (ideally if we can figure out media remote control capability that would be helpful), and then connect to them over L2CAP to get AVRCP messages. The `devicePairingFinished:error:` delegate method was a test. Leaving it in as a reminder that we also need to implement functionality that looks for already-connected Bluetooth headphones when DomeKey launches. --- DomeKey/HeadphoneKeyEventBluetooth.h | 3 ++- DomeKey/HeadphoneKeyEventBluetooth.m | 27 +++++++++++++++++---------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/DomeKey/HeadphoneKeyEventBluetooth.h b/DomeKey/HeadphoneKeyEventBluetooth.h index 86e0ce7..a55a539 100644 --- a/DomeKey/HeadphoneKeyEventBluetooth.h +++ b/DomeKey/HeadphoneKeyEventBluetooth.h @@ -7,11 +7,12 @@ // #import +#import #import "HeadphoneKeyEventDelegate.h" #import "dome_key_map.h" -@interface HeadphoneKeyEventBluetooth : NSObject { +@interface HeadphoneKeyEventBluetooth : NSObject { id _delegate; } diff --git a/DomeKey/HeadphoneKeyEventBluetooth.m b/DomeKey/HeadphoneKeyEventBluetooth.m index 52e22db..839a4ba 100644 --- a/DomeKey/HeadphoneKeyEventBluetooth.m +++ b/DomeKey/HeadphoneKeyEventBluetooth.m @@ -16,20 +16,27 @@ if (self) { _delegate = delegate; - MPRemoteCommandCenter *cc = [MPRemoteCommandCenter sharedCommandCenter]; - [cc playCommand]; - [cc pauseCommand]; - [cc stopCommand]; - [cc togglePlayPauseCommand]; - // [cc nextTrackCommand]; - // [cc seekForwardCommand]; - // [cc previousTrackCommand]; - // [cc seekBackwardCommand]; + IOBluetoothUserNotification *notification = [IOBluetoothDevice + registerForConnectNotifications:self + selector:@selector(connectNotification:forDevice:)]; + if (!notification) { + // TODO: error + NSLog(@"Connection notification error"); + } } return self; } -MPRemoteCommandHandler key_pressed(HeadphoneButton button) { +- (void)devicePairingFinished:(id)sender + error:(IOReturn)error +{ + NSLog(@"Paired: %@", sender); +} + +- (void)connectNotification:(IOBluetoothUserNotification *)notification + forDevice:(IOBluetoothDevice *)device +{ + NSLog(@"Paired notification: %@ ; Device: %@", notification, device); } @end -- cgit v1.2.3