Vic Vuci
Vic Vuci

Reputation: 7051

Is there a AudioManager.MODE_IN_COMMUNICATION Bluetooth permission interaction?

I have a very simple line of code:

audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);

However, recently on 4.0+ devices, I am seeing a crash due to this line saying it requires the bluetooth permission.

To be more precise, the error I'm seeing says:

java.lang.SecurityException: Need BLUETOOTH permission

at the line of my setMode.

I have the MODIFY_AUDIO_SETTINGS permission, however I do not see how this interacts with bluetooth, so I am looking for a confirmation of whether or not I truly need the BLUETOOTH permission for MODE_IN_COMMUNICATION

Upvotes: 4

Views: 4439

Answers (1)

Adam Stelmaszczyk
Adam Stelmaszczyk

Reputation: 19847

From a logical point of view, there is no way that AudioManager would use Bluetooth and thus need android.permission.BLUETOOTH.

From a source code point of view, setMode() needs only android.permission.MODIFY_AUDIO_SETTINGS:

  • AudioManager:1425

    public void setMode(int mode) {
        IAudioService service = getService();
        try {
            service.setMode(mode, mICallBack);
        } catch (RemoteException e) {
            Log.e(TAG, "Dead object in setMode", e);
        }
    }
    
  • AudioService:703

    public void setMode(int mode, IBinder cb) {
        if (!checkAudioSettingsPermission("setMode()")) {
            return;
        }
    
  • AudioService:1250

    boolean checkAudioSettingsPermission(String method) {
        if (mContext.checkCallingOrSelfPermission("android.permission.MODIFY_AUDIO_SETTINGS")
            == PackageManager.PERMISSION_GRANTED) {
        return true;
        }
    String msg = "Audio Settings Permission Denial: " + method + " from pid="
        + Binder.getCallingPid()
        + ", uid=" + Binder.getCallingUid();
        Log.w(TAG, msg);
        return false;
    }
    

Upvotes: 5

Related Questions