6
votes

I need to do an app which will be able to tell if I am currently connected to a classic Bluetooth device or not (actually, it will be a Bluetooth car device).

My first step is to tell what are the current connected classic Bluetooth devices. I cannot use CoreBluetooth because it's only for LE. I try to use the External Accessory framework.

Here is the code (a button starts the method):

- (IBAction)startMethodGetConnected:(id)sender {
     NSLog(@"button taped");
     // Get the number of accessories connected
     NSUInteger NumberOfAccessoriesConnected = [[EAAccessoryManager sharedAccessoryManager].connectedAccessories count];
     //Display the number 
     NSLog(@"number of accessories connected : %d", NumberOfAccessoriesConnected);
 }

I have tried when the iPhone was connected to a Bluetooth keyboard and also with a Bluetooth headset. In both cases, the console displays that the number is 0.

How can I display the correct number?

2
It seems that Bluetooth devices like "normal" Keyboard and "normal" Headset are not listed in EAAcessoryManager.Larme
Yes, I think it's because the device has to be MFI, which may not not be the case for "normal" bluetooth devices. The solution I found was to use the [[AVAudioSession sharedInstance] availableInputs] to get the bluetooth input, however, with this solution, I can not receive notifications while in the background (I would have to play music to be allowed to receive the notification).augustin

2 Answers

1
votes

From apple documentation:

"Applications that support external accessories must be sure to configure their Info.plist file correctly. Specifically, you must include the UISupportedExternalAccessoryProtocols key to declare the specific hardware protocols your application supports. For more information about this framework, see External Accessory Programming Topics." here

You need to add this key in your Info.plist file with the protocol of your MFi device.

<key>UISupportedExternalAccessoryProtocols</key>
<array>
    <string>your_protocol_name</string>
</array>

Regards

1
votes

You cannot. What you can do is to check the playback route. The problem is that your cars handsfree will be a HeadsetBT. This is the code I use in my app.

// create and set up the audio session
AVAudioSession* audioSession = [AVAudioSession sharedInstance];
[audioSession setDelegate:self];
[audioSession setCategory: AVAudioSessionCategoryPlayAndRecord error: nil];
[audioSession setActive: YES error: nil];

// set up for bluetooth microphone input
UInt32 allowBluetoothInput = 1;
OSStatus stat = AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryEnableBluetoothInput,
                                         sizeof (allowBluetoothInput),
                                         &allowBluetoothInput
                                         );
NSLog(@"status = %x", stat);    // problem if this is not zero

// check the audio route
UInt32 size = sizeof(CFStringRef);
CFStringRef route;
OSStatus result = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &size, &route);
NSLog(@"route = %@", route);
// if bluetooth headset connected, should be "HeadsetBT"
// if not connected, will be "ReceiverAndMicrophone"