android如何获取连接蓝牙设备的名称

时间:2016-06-29 10:13:51

标签: android bluetooth

我想知道如何在android中获取连接的蓝牙设备的名称 这是代码

NetworkInfo bluetooth = connectivityManager .getNetworkInfo(ConnectivityManager.TYPE_BLUETOOTH);
if(bluetooth.isConnected())
                {
                    Toast.makeText(myprofile3Context,"bluetooth is connected", Toast.LENGTH_SHORT).show();
                } 

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

2 个答案:

答案 0 :(得分: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;
    }
    

答案 1 :(得分: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并在permissions

中添加manifest