Android Unblock device Microphone

35 Views Asked by At

I have created a android app that provides the option to search the products through voice search with Speech recognition. It works on all phones except pixel phones. As pixel mobiles have new feature "Block Mic access", "Block camera access" which are hardware level blocking. My app cannot detect that the microphone is blocked. Even though I have checked whether the mic is muted or not muted with Audiomanager and mic available with package manager, everything returns true. As this is a new feature of restricting access to hardwares in pixel phones, i couldn't find a solution.

I have tried using package manager to detect mic is available. Also, tried audio manager to detect mic is muted. Still everything returns true.

1

There are 1 best solutions below

0
A S M Rakibul Hasan On

As Some Mobile have new feature "Block Mic access", "Block camera access" which are hardware level blocking. Try this solution, I got Boolean value Camera Access Block State By this solution.

import android.Manifest
import android.annotation.SuppressLint
import android.annotation.TargetApi
import android.app.AppOpsManager
import android.content.Context
import android.os.Build
import android.os.Process
import androidx.annotation.RestrictTo
import androidx.core.app.AppOpsManagerCompat

@TargetApi(Build.VERSION_CODES.S)
@RestrictTo(RestrictTo.Scope.LIBRARY)
object SensorPrivacyCheck {
    fun isMicrophoneBlocked(): Boolean {
        return checkIsPrivacyToggled(AppOpsManager.OPSTR_RECORD_AUDIO)
    }

    fun isCameraBlocked(): Boolean {
        return checkIsPrivacyToggled(AppOpsManager.OPSTR_CAMERA)
    }

    @SuppressLint("PrivateApi", "BlockedPrivateApi")
    private fun checkIsPrivacyToggled(opStr: String): Boolean {
        val appOpsManager: AppOpsManager =
            ApplicationContext.getSystemService(Context.APP_OPS_SERVICE) as AppOpsManager
        try {
            val mode = appOpsManager.unsafeCheckOpNoThrow(
                opStr,
                Process.myUid(),
                ApplicationContext.packageName
            )
            return mode != AppOpsManager.MODE_ALLOWED
        } catch (e: Throwable) {
            e.printStackTrace()
        }
        return false
    }
}