Configuring Permissions

The Android operating system limits access to some device functionality for end-user control and privacy. In order to use this functionality, configure your app to declare and request permissions from end users at runtime:

1

Review permissions used by Ditto. (Declaring Permissions)

2

Proactively check permission status to avoid unnecessary delay between granting access and Ditto being able to use the feature. (Requesting Permissions)

Declaring Permissions in the Android Manifest

Android requires certain permissions to be explicitly requested by the app to access features like Bluetooth Low Energy and Wi-Fi Aware. These permissions must be declared in the app’s manifest file and requested from the end user at runtime.

The Ditto SDK’s AndroidManifest.xml includes all of the necessary permissions for enabling its mesh network capabilities. These permissions will automatically be merged with your app’s permissions, so you should be aware of them.

<uses-permission android:name="android.permission.BLUETOOTH" 
    android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" 
    android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" 
    tools:targetApi="s" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" 
    tools:targetApi="s" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" 
    android:usesPermissionFlags="neverForLocation" 
    tools:targetApi="s" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" 
    android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" 
    android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.NEARBY_WIFI_DEVICES" 
    android:usesPermissionFlags="neverForLocation" 
    tools:targetApi="tiramisu" />

Some of these permissions have an android:maxSdkVersion attribute which means they are not used on devices running newer versions of Android. This is a best practice to respect users’ privacy when those permissions are not necessary.

However, some apps may still need to use one or more of the above permissions across more versions of Android. This can be accomplished by overriding the permission configuration in your app’s AndroidManifest.xml

To override any of these permission limitations in your app, do the following:

1

Open the AndroidManifest.xml located in the app/src/main directory of your project.

2

Within the same manifest tag, just before the application tag, add the relevant permissions you want to configure (location example):

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" 
    tools:remove="android:maxSdkVersion" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" 
    tools:remove="android:maxSdkVersion" />

Note the additional tools:remove attribute. This tells the manifest merger to selectively remove the android:maxSdkVersion behavior from the associated permissions, changing them to apply to all Android versions.

For more information, see the official Permissions on Android and Merge manifest files documentation.

Requesting Permissions at Runtime

In addition to being declared in the manifest, some permissions need to be requested from the user at runtime. To ensure your app has the required permissions for sync, use the DittoSyncPermissions helper class from the Ditto SDK. This helper class:

  • Simplifies the process of requesting end-user permissions at runtime with a single method call (ensurePermissions) for all necessary permissions.
  • Handles any errors encountered during the permissions request process.

Using the Helper

To use the DittoSyncPermissions helper class:

1

Import the DittoSyncPermissions helper class in the main activity of your app:

import live.ditto.transports.DittoSyncPermissions
2

Call the following in your Activity or Fragment’s onCreate method:

fun requestPermissions() {
    val missing = DittoSyncPermissions(this).missingPermissions()
    if (missing.isNotEmpty()) {
        this.requestPermissions(missing, 0)
    }
}
3

Refresh permissions after permissions are granted.

On Android, after granting permissions, Ditto may not immediately recognize the change, causing a delay in syncing. To address this, whenever a relevant permission changes, call refreshPermissions() to quickly check and start syncing with the new permissions. This can be done in the onRequestPermissionsResult() activity lifecycle method:

private val requestPermissionLauncher = registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { this.ditto.refreshPermissions() } override fun onCreate(savedInstanceState: Bundle?) { }