【问题标题】:How get the device name of bluetooth paired devices?如何获取蓝牙配对设备的设备名称?
【发布时间】:2019-05-20 18:59:42
【问题描述】:

我在获取蓝牙设备的名称时遇到了一些问题。

我正在连接名称为 MTP-3 的蓝牙打印机(此名称是硬代码),但我想连接另一台具有其他名称的蓝牙打印机,因此我更改了第二台打印机的名称设置中操作系统的配对设备列表,但它不起作用。

另外,如果我将第一台打印机的名称更改为 MTP-3 以外的名称,打印机仍会与我的应用程序连接。

我正在寻找一种方法,即使我使用操作系统更改了名称,也可以为我提供与 Android 配对的设备的确切名称。

  Set<BluetoothDevice> pairedDevice = bluetoothAdapter.getBondedDevices();

            if (pairedDevice.size() > 0) {
                for (BluetoothDevice pairedDev : pairedDevice) {

    if(pairedDev.getName.equals("MTP-3")){
          bluetoothDevice = pairedDev;
          Toast.makeText(getApplicationContext(), "found it", Toast.LENGTH_SHORT).show();
          break;
    }
}
}

【问题讨论】:

    标签: java android bluetooth


    【解决方案1】:

    下面的代码会获取蓝牙名称,如果没有名称,则返回地址。

    public String getDeviceName(){
        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】:

      你可以从返回的集合中得到它:

      BluetoothAdapter mBluetoothAdapter =BluetoothAdapter.getDefaultAdapter();
      Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-14
        • 1970-01-01
        • 2015-12-26
        • 1970-01-01
        • 2016-09-26
        相关资源
        最近更新 更多