2
votes

I want to detect the below events when there is any incoming/outgoing call from an iOS device:

  1. Whether any bluetooth headset device is connected.
  2. Whether the audio is routed to any bluetooth headset device.

I am new to bluetooth accessory programming, is there any way to do it?

2

2 Answers

1
votes

In the below code, check the value of "micConnected" to see if headset connected.

AudioSessionInitialize(NULL, NULL, NULL, NULL);    
UInt32 propertySize, micConnected;
    AudioSessionGetProperty(kAudioSessionProperty_AudioInputAvailable, &propertySize, &micConnected);
1
votes

I solved it as given below:

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    NSError *err;

    // close down our current session...
    [audioSession setActive:NO error:nil];
    AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange,   &AudioSessionManager_audioRouteChangedListener, self);


    void AudioSessionManager_audioRouteChangedListener (void *inClientData, AudioSessionPropertyID inID, UInt32 inDataSize, const void *inData)
    {
        NSLog(@"AudioSessionManager_audioRouteChangedListener");
        MyAppDelegate *instance = (MyAppDelegate *)inClientData;
        CFDictionaryRef routeChangeDictionary = inData;


        // extract the route change reason...

        CFNumberRef routeChangeReasonRef = CFDictionaryGetValue (routeChangeDictionary, CFSTR(kAudioSession_AudioRouteChangeKey_Reason));
        SInt32 routeChangeReason = kAudioSessionRouteChangeReason_Unknown;
        if (routeChangeReasonRef)
            CFNumberGetValue (routeChangeReasonRef, kCFNumberSInt32Type, &routeChangeReason);

        // extract the old route..

        CFStringRef newRoute;
        if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 5.0)
        {
            newRoute = CFDictionaryGetValue(routeChangeDictionary, @"OutputDeviceDidChange_NewRoute");
        }
        else
        {
            newRoute = CFDictionaryGetValue(routeChangeDictionary, @"OutputDeviceDidChange_NewRoute");
        }


        CFStringRef oldRoute;
        oldRoute = CFDictionaryGetValue(routeChangeDictionary, @"OutputDeviceDidChange_OldRoute");
        NSLog(@"newRouteString:%@ oldRoute:%@",newRoute,oldRoute);

        [instance onAudioRouteChangedWithReason:routeChangeReason newRoute:(NSString *)newRoute oldRoute:(NSString *)oldRoute];
    }