1

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.'

ganjaam
  • 1,030
  • 3
  • 17
  • 29
  • Does this answer your question? [What is changed with Bluetooth in Android SDK 32?](https://stackoverflow.com/questions/72271363/what-is-changed-with-bluetooth-in-android-sdk-32) – Youssef Wagih May 23 '23 at 09:31
  • @YoussefWagih, it's close to what I need. Suppose my device name is 'Samsung Note 7'. I've changed it to 'Superman.' The answer to that post returns the name 'Samsung Note 7.' But I need a way to get 'Superman.' – ganjaam May 23 '23 at 09:54

0 Answers0