Get a list of classic Bluetooth devices connected (no BLE) [EAAccessoryManager]

I need to make an application that can find out if I am connected to a classic Bluetooth device or not (in fact it will be a Bluetooth car device).

My first step is to tell you what current connected classic Bluetooth devices. I can’t use CoreBluetooth because it is only for LE. I am trying to use the structure of an external accessory.

Here is the code (the 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 tried when the iPhone was connected to a Bluetooth keyboard as well as with a Bluetooth headset. In both cases, the console displays that the number is 0.

How can I display the correct number?

+5
source share
2 answers

From the Apple documentation:

"Applications that support external accessories should be sure to correctly configure their Info.plist file. In particular, you must include the UISupportedExternalAccessoryProtocols key to declare the specific hardware protocols supported by your application. For more information on this structure, see Programming External Accessories Themes . " here

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

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

Hello

+1
source

You can not. What you can do is check the playback route. The problem is that your car speakers will be HeadsetBT. This is the code that I use in my application.

 // 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" 
+1
source

Source: https://habr.com/ru/post/1243357/


All Articles