Search code examples
androidbluetoothpermissionswear-os

Android getting dynamic permission from third party defined permissions


I'm working on a project that tries to send data from Android app to Tizen wear watch. I have a sample app provided by Samsung but this is super old which is based on Eclipse, (R.I.P).

So, I created a sample app and the first step is to get permissions (for Marshmallow OS). These permissions have provided by sample code in Manifest file.

<uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="com.samsung.accessory.permission.ACCESSORY_FRAMEWORK" />
    <uses-permission android:name="com.samsung.android.providers.context.permission.WRITE_USE_APP_FEATURE_SURVEY" />
    <uses-permission android:name="com.samsung.WATCH_APP_TYPE.Companion" />
    <uses-permission android:name="com.samsung.wmanager.ENABLE_NOTIFICATION" />

So, I'm trying to get these permissions on App launch.

public class MainActivity extends AppCompatActivity
{
    private static final String TAG = "MainActivity";
    private static final int MY_PERMISSIONS_REQUEST = 101;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Check permissions
        getPermissions();
    }

    public void getPermissions()
    {
        // No explanation needed, we can request the permission.
        ActivityCompat.requestPermissions(this, new String[]{
            Manifest.permission.BLUETOOTH, 
            Manifest.permission.BLUETOOTH_ADMIN, 
            "com.samsung.accessory.permission.ACCESSORY_FRAMEWORK", 
            "com.samsung.android.providers.context.permission.WRITE_USE_APP_FEATURE_SURVEY", 
            "com.samsung.WATCH_APP_TYPE.Companion", 
            "com.samsung.wmanager.ENABLE_NOTIFICATION"}, MY_PERMISSIONS_REQUEST);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults)
    {
        for(int i : grantResults)
        {
            Log.d(TAG, "Result: " + i);
        }

        switch (requestCode)
        {
            case MY_PERMISSIONS_REQUEST:
            {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED &&
                        grantResults[1] == PackageManager.PERMISSION_GRANTED &&
                        grantResults[2] == PackageManager.PERMISSION_GRANTED &&
                        grantResults[3] == PackageManager.PERMISSION_GRANTED &&
                        grantResults[4] == PackageManager.PERMISSION_GRANTED &&
                        grantResults[5] == PackageManager.PERMISSION_GRANTED)
                {
                    displayMessage("Permissions granted");
                }
                else
                {
                    displayMessage("Please accept permissions to proceed.");
                    this.finish();
                }
            }
        }
    }

    private void displayMessage(String message)
    {
        if (TextUtils.isEmpty(message))
        {
            return;
        }

        Toast.makeText(this, message, Toast.LENGTH_LONG).show();
    }
}

When I run it, no permission request dialog displays even for Bluetooth and app closes obviously (due to code). I see following result in Log:

D/MainActivity: Result: 0
D/MainActivity: Result: 0
D/MainActivity: Result: -1
D/MainActivity: Result: -1
D/MainActivity: Result: -1
D/MainActivity: Result: -1

For some reasons Android automatically give me Bluetooth permission but have no idea how to get those Samsung permissions.


Solution

  • Only specific dangerous permissions are require requesting them at run time. Therefore you should not need to write any permissions related code to be granted any of those permissions.