I displayed the bluetooth name as the device name in my application in the following way:
public static String getDeviceName(ContentResolver contentResolver) {
String userDeviceName = Settings.Secure.getString(contentResolver, "bluetooth_name");
if (userDeviceName == null) return "";
Log.d(TAG, "getDeviceName: userDeviceName: " + userDeviceName);
return userDeviceName;
}
However, for devices with SDK version higher than 32, this approach throws a security exception like this:
Caused by java.lang.SecurityException: Settings key: <bluetooth_name> is only readable to apps with targetSdkVersion lower than or equal to: 31
at android.provider.Settings$NameValueCache.getStringForUser(Settings.java:3323)
at android.provider.Settings$Secure.getStringForUser(Settings.java:9400)
at android.provider.Settings$Secure.getString(Settings.java:9356)
at com.bytecode.sola_android.utils.ValueUtils.getDeviceName(ValueUtils.java:26)
at com.bytecode.sola_android.activity.MainActivity.onCreate(MainActivity.java:55)
at android.app.Activity.performCreate(Activity.java:8591)
..............................
..............................
..............................
How can I get the device name (or Bluetooth name) in devices with target SDK version 32 or higher?
Note: I've found an answer that fetches device name in the following manner:
Settings.Global.getString(application.contentResolver, Settings.Global.DEVICE_NAME)
?: "UNKNOWN"
However, it returns the model name of the device. But I'm trying to fetch the device name the user can edit. For instance, in settings -> about phone -> device name or in settings -> Bluetooth -> device name, the user can change the name from 'Samsung' to 'XYZ.' I need to find a way to retrieve the value 'XYZ.' But this method returns the value 'Samsung.'