安卓如何获取已连接蓝牙设备的名称

4
我想知道如何在Android中获取已连接蓝牙设备的名称。以下是代码:
NetworkInfo bluetooth = connectivityManager .getNetworkInfo(ConnectivityManager.TYPE_BLUETOOTH);
if(bluetooth.isConnected())
                {
                    Toast.makeText(myprofile3Context,"bluetooth is connected", Toast.LENGTH_SHORT).show();
                } 

在这里,我检查蓝牙设备是否已连接。如果蓝牙连接成功,我想知道如何获取已连接设备的名称。


请访问以下链接以获取有关在Android中获取蓝牙适配器设备名称和MAC地址的说明:https://cmanios.wordpress.com/2012/05/09/bluetooth-adapter-device-name-and-mac-address-in-android/ - Sagar Nayak
2个回答

0
尝试一下。
public String getLocalBluetoothName(){
    if(mBluetoothAdapter == null){
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    }
    String name = mBluetoothAdapter.getName();
    if(name == null){
        System.out.println("Name is null!");
        name = mBluetoothAdapter.getAddress();
    }
    return name;
}

2
我无法获取自己的设备名称。我想要获取其他连接设备的名称。 - faisal iqbal
@faisaliqbal,我也遇到了同样的问题,你有解决方案吗? - GNK

0
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        // When discovery finds a device
        if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent
                    .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                          //you can get name by device.getName()

        } else if (BluetoothAdapter.ACL_DISCONNECTED
                .equals(action)) {

        }
    }
 };

使用extends BroadcastReceiver并在manifest中添加permissions


我想要它没有广播接收器。 - faisal iqbal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接