简体   繁体   English

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

[英]How do I get the name of the current connected bluetooth device?

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. 我想让我的android应用程序检测是否已连接蓝牙耳机(按名称或mac地址),然后根据是否已连接来运行代码。 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. 问题是,关于如何获取当前连接的蓝牙耳机的名称/ 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. 当我使用“ getBondedDevices()”时,它通常只是为我提供了与手机配对的设备,但实际上并没有提供当前连接的设备。 In other words, it gives me the list of previously paired devices, but not the actual connected device. 换句话说,它为我提供了先前配对设备的列表,但没有提供实际连接的设备的列表。

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. 我不知道自己在做什么,因为这是第一次处理蓝牙设备,而Google的开发人员站点非常擅长告诉蓝牙设备能够做什么,但不一定是如何使用它的示例。

So can someone please give me some helpful code that could get the name/mac address of the current connected bluetooth handset. 所以有人可以给我一些有用的代码,以获取当前连接的蓝牙手机的名称/ mac地址。 Thanks in advance for any and all help 预先感谢您提供的所有帮助

一旦建立了蓝牙连接,您就可以将连接的设备信息(名称和地址)保存在共享首选项中,并在需要获取连接的设备信息时获取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM