如何获取当前连接的蓝牙设备的名称?

时间:2021-01-06 20:24:12

I would like to make my android app detect if a Bluetooth headset (either by name or mac address) is connected and then run code based on whether it is connected or not. The problem is I've been hitting a brick wall in regards to how to get the name/mac address of the currently connected Bluetooth headset.

我想让我的android应用程序检测蓝牙耳机(名字或mac地址)是否连接,然后根据是否连接运行代码。问题是,我在如何获得当前连接的蓝牙耳机的名称/mac地址的问题上一直碰壁。

The following code simply gives me the hostname of the Android device:

下面的代码简单地给出了Android设备的主机名:

  BA = BluetoothAdapter.getDefaultAdapter();
  String name = BA.getName();

When I used "getBondedDevices()", it usually just gives me the devices that the phone has been paired to, but not actually what is connected at the moment. In other words, it gives me the list of previously paired devices, but not the actual connected device.

当我使用“getBondedDevices()它通常只提供给我手机已经配对的设备,而不是现在连接的设备。换句话说,它给了我之前配对设备的列表,而不是实际连接的设备。

I have no clue as to what I'm doing because this is the first time dealing with Bluetooth devices and the Google's developer site is great about telling what BluetoothDevice is capable of, but not necessarily of examples of how to use it.

我不知道我在做什么,因为这是第一次处理蓝牙设备,而谷歌的开发者网站很好地说明了蓝牙设备的能力,但并不一定是使用它的例子。

So can someone please give me some helpful code that could get the name/mac address of the current connected bluetooth handset. Thanks in advance for any and all help

谁能给我一些有用的代码,可以得到当前连接的蓝牙手机的名称/mac地址。提前感谢所有人的帮助。

1 个解决方案

#1


0  

Once you re making a bluetooth connection that time you can save the connected device information(name and address) in shared preferences and get it when you want to get connected device info.

一旦你做了一个蓝牙连接,你就可以将连接的设备信息(名称和地址)保存在共享的首选项中,并在你想要连接设备信息的时候得到它。

#1


0  

Once you re making a bluetooth connection that time you can save the connected device information(name and address) in shared preferences and get it when you want to get connected device info.

一旦你做了一个蓝牙连接,你就可以将连接的设备信息(名称和地址)保存在共享的首选项中,并在你想要连接设备信息的时候得到它。