BluetoothDevice#getName() return null in Android 14 while retrying pairing after canceling first pairing request

151 Views Asked by At

BluetoothDevice#getName() return null on Android 14

While trying to connect with below steps, Getting null value for BluetoothDevice#getName()

  1. Scan Bluetooth device
  2. Cancel pairing request
  3. Retry the pairing
  4. BluetoothDevice#getName() returns null in retry

Note: Issue specific to Android 14. Working fine till Android 13.

2

There are 2 best solutions below

0
Renzo Sampietro On

The GATT Server will broadcast its Services by sending advertising packets. Since the Device Name is optional in AdvertiseData, you may need to include a control like the following in your code. Refer to 1 and 2 By Andrew Lunsford

override fun onScanResult(callbackType: Int, result: ScanResult) {
            val device: BluetoothDevice = result.getDevice()
            // ...do whatever you want with this found device

}

fun getDeviceName () : String? {
        return device.name
}

if (getDeviceName() != null)
        { deviceName = getDeviceName() 
    // do whatever you want with this device name
    }
        else
        { 
    // code for the case of device.name is null
    }    
0
Teenagex On

bluetoothGatt = bleDevice.connectGatt(context, false, bluetoothGattCallback) midiManager.openBluetoothDevice(bleDevice, deviceOpenedListener, handler)

 val builder = ScanSettings.Builder()
  if (Build.VERSION.SDK_INT > Build.VERSION_CODES.TIRAMISU) {
    builder.setScanMode(ScanSettings.SCAN_MODE_BALANCED)
  }else{
    builder.setScanMode(ScanSettings.SCAN_MODE_LOW_POWER)
  }
  val settings = builder.build()