问题
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?
回答1:
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
回答2:
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"
来源:https://stackoverflow.com/questions/35495738/get-the-list-of-classic-bluetooth-connected-devices-no-ble-eaaccessorymanager