LoginSignup
3
4

More than 5 years have passed since last update.

ペアリングしたBluetoothデバイスの「名前」を取得する方法

Posted at

ペアリングしたデバイスは設定画面で名前を変えることができますよね。

device.png

どうしてもこの値が欲しかった。
BluetoothDeviceを見ても、それらしいメソッドは見つかりません。
getName()というのがあったのですが、これは名前を変更する前のデフォルトの値が返ってきました。

Androidのソースを覗いてみるとgetAlias()を発見。
試してみると、ちゃんと取得できました。

ただし、@hideなメソッドなので使用する際は注意。


以下、リフレクションを使ってgetAlias()を呼び出すコード。

private String getAlias(BluetoothDevice device) {
    try {
        Method getAlias = BluetoothDevice.class.getMethod("getAlias");
        return (String) getAlias.invoke(device);
    } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
        return null;
    }
}
3
4
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4