我在获取蓝牙设备的名称时遇到了一些麻烦。
我正在连接一台名称为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;
}
}
}发布于 2019-05-21 03:07:02
您可以从返回的set中获取:
BluetoothAdapter mBluetoothAdapter =BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();发布于 2019-05-21 04:01:15
下面的代码将获取蓝牙名称,如果没有名称,它将返回地址。
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;
}https://stackoverflow.com/questions/56226688
复制相似问题