CRIWARE Unity Plugin Manual  Last Updated: 2024-07-12
Detecting a Bluetooth device
In order to use CriAtomOutputDeviceObserver to detect the switch of the audio output device, the Bluetooth feature must be enabled.
Follow the steps below to set up the project and implement the permission acquisition on the application side.
Otherwise, it is not possible to detect and switch to the Bluetooth device.

Settings for Android.manifest

Please declare the permissions in your app’s Android.manifest.
Note that the authorization declaration needs to correspond to the Android version.
<manifest>
<uses-permission android:name="android.permission.BLUETOOTH"
android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
</manifest>

Acquiring the Runtime Permission

When your application is installed on a device that runs API Level 31 or higher, the user must request the runtime permission to obtain the Bluetooth-related permission.
Please check and request for the necessary permission after launching the application.
#if !UNITY_EDITOR && UNITY_ANDROID
using UnityEngine.Android;
#endif
// (omitted)
#if !UNITY_EDITOR && UNITY_ANDROID
private static void RequestBluetoothPermission()
{
var sdkVersion = GetAndroidSdkVersion();
if (sdkVersion >= 31)
{
if (!Permission.HasUserAuthorizedPermission("android.permission.BLUETOOTH_CONNECT"))
{
Permission.RequestUserPermission("android.permission.BLUETOOTH_CONNECT");
}
}
}
private static int GetAndroidSdkVersion()
{
using (var version = new AndroidJavaClass("android.os.Build$VERSION"))
{
return version.GetStatic<int>("SDK_INT");
}
}
#endif