Android code to enable renaming a device with the 0$S prefix (code also checks if “0$S” was
already set as a prefix). Please note, this automation will require permissions.
/**
* "Ensure that you check Android permissions before executing read/write operations with the
Bluetooth Manager."
* Additional checks like if name already have prefix 0$S then show user information or show another
flow to user could be done
* <!--Before Android 12 (but still needed location, even if not requested)-->
* <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
* <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
* <uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30"
/>
* <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"
android:maxSdkVersion="30" />
*
* <!--From Android 12-->
* <uses-permission android:name="android.permission.BLUETOOTH_SCAN"
android:usesPermissionFlags="neverForLocation" />
* <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
*/
@SuppressLint("MissingPermission")
fun renameBluetoothDeviceName(context: AppCompatActivity) {
val btManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
val mBluetoothAdapter = btManager.adapter
if (!isBtNameAlreadyHavePrivacyPrefix(context)) {
mBluetoothAdapter.name = "0\$S " + mBluetoothAdapter.name
} else {
Toast.makeText(context, "Device name already have a prefix 0\$S.",
Toast.LENGTH_SHORT).show()
}
}
/**
* This method can be used to check if device name already have a 0$S prefix or not.
*/
@SuppressLint("MissingPermission")
fun isBtNameAlreadyHavePrivacyPrefix(context: AppCompatActivity): Boolean {
val btManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
val mBluetoothAdapter = btManager.adapter
return mBluetoothAdapter.name?.startsWith("0\$S") == true
}
Android sample code an app can use to read the name of the device on which it is installed and
determine if it includes the “0$S” prefix and trigger an opt-out or not.
/**
* This method can be used to check if device name already have a 0$S prefix or not to determine
* if UniversalOptOut Enabled or not,This method returns boolean true/false
* where true mean UniversalOptOut Enabled
* and false mean UniversalOptOut Disabled
*/
@SuppressLint("MissingPermission")
fun isUniversalOptOutEnabled(context: AppCompatActivity): Boolean {
val btManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
val mBluetoothAdapter = btManager.adapter
val isOptedOut = mBluetoothAdapter.name?.startsWith("0\$S") == true
if (isOptedOut) {
Log.e("UniversalOptOutStatus", "UniversalOptOut Enabled")
} else {
Log.e("UniversalOptOutStatus", "UniversalOptOut Disabled")
}
return isOptedOut
}
Android sample code an app can use to read the name of a device the smartphone is paired with
over Bluetooth (in case the “0$S” prefix is applied not to the smartphone but to a device that is
set with a universal opt-out signal).
/**
* Following function can be used to retrieve device name if it is already connected to a Bluetooth device.
* If required connection update as soon as they happen i.e., Listening to connected or disconnected state
* following broadcast receivers can be used
* BluetoothDevice.ACTION_ACL_CONNECTED
* BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED
* BluetoothDevice.ACTION_ACL_DISCONNECTED
*/
@SuppressLint("MissingPermission")
fun getConnectedDeviceName(context: AppCompatActivity): String ? {
val btManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
for (device in btManager.adapter.bondedDevices) {
if (isConnected((device))) {
return device.name
}
}
return "No device connected currently."
}
private fun isConnected(device: BluetoothDevice): Boolean {
return try {
val m: Method = device.javaClass.getMethod("isConnected")
m.invoke(device) as Boolean
} catch (e: Exception) {
throw IllegalStateException(e)
}
}