[英]How to get information about a connected/paired Bluetooth device in Java?
[英]How get the device name of bluetooth paired devices?
我有一些麻烦得到蓝牙设备的名称。
我正在连接一台名为MTP-3
的蓝牙打印机(这个名称是硬编码),但是我想连接另一台有另一个名字的蓝牙打印机,所以我更改了列表中的第二台打印机的名称。设置中的操作系统配对设备但它不起作用。
此外,如果我将第一台打印机的名称更改为MTP-3
以外的其他名称,则打印机仍会与我的应用程序连接。
我正在寻找一些方法,即使我使用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;
}
}
}
你可以从返回的集合中获取它:
BluetoothAdapter mBluetoothAdapter =BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
下面的代码将获得蓝牙名称,如果它没有名称,它将返回地址。
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.