[英]How get the device name of bluetooth paired devices?
I'm having some troubles getting the name of bluetooth devices. 我有一些麻烦得到蓝牙设备的名称。
I'm connecting a bluetooth printer that has the name of MTP-3
(this name is in hard code) , but I want to connect another bluetooth printer which has another name so I change the name of the second printer in the List of the Paired Devices of Operating System in the settings but It doesn't work. 我正在连接一台名为
MTP-3
的蓝牙打印机(这个名称是硬编码),但是我想连接另一台有另一个名字的蓝牙打印机,所以我更改了列表中的第二台打印机的名称。设置中的操作系统配对设备但它不起作用。
Also, if I change the name of the first printer to something other than MTP-3
the printer still connects with my app. 此外,如果我将第一台打印机的名称更改为
MTP-3
以外的其他名称,则打印机仍会与我的应用程序连接。
I'm looking for some method that gives me the exactly name of the paired devices with Android even if I change the name with the Opereting System. 我正在寻找一些方法,即使我使用Opereting System更改名称,也能为我提供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;
}
}
}
You can get it from the returned set: 你可以从返回的集合中获取它:
BluetoothAdapter mBluetoothAdapter =BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
The below code will get the bluetooth name, and if it doesn't have a name, it will return the address. 下面的代码将获得蓝牙名称,如果它没有名称,它将返回地址。
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.