一、BluetoothDevice介绍
BluetoothDevice是Android中用于表示蓝牙设备的类,它包含了蓝牙设备的名称、地址、类型等信息,并提供了一些方法用于连接、配对、获取设备信息等操作。在Android中,我们可以通过BluetoothAdapter来获取BluetoothDevice对象,然后进行相关操作。例如,我们可以使用BluetoothAdapter的getBoundedDevices()方法获取已经配对的蓝牙设备集合,然后使用BluetoothDevice的getName()方法获取设备名称,使用getAddress()方法获取设备地址等。此外,BluetoothDevice还提供了一些其他的方法,例如connectGatt()方法用于连接GATT服务器,createBond()方法用于配对设备等。
以下是一个获取已配对蓝牙设备名称和地址的例子:
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
String deviceName = device.getName();
String deviceAddress = device.getAddress();
Log.d(TAG, "deviceName: " + deviceName + ", deviceAddress: " + deviceAddress);
}
}
二、BluetoothDevice相关接口
Attributable
Marker interface for a class which can have an AttributionSource assigned to it; these are typically Parcelable classes which need to be updated after crossing Binder transaction boundaries.
可以为其分配 AttributionSource 的类的标记接口;这些通常是 Parcelable 类,需要在跨越 Binder 事务边界后进行更新。
Attributable代码位于:
packages/modules/Bluetooth/framework/java/android/bluetooth/Attributable.java
Attributable的定义:
public interface Attributable {
static @Nullable <T extends Attributable> T setAttributionSource(@Nullable T attributable, @NonNull AttributionSource attributionSource) {}
static @Nullable <T extends Attributable> List<T> setAttributionSource(@Nullable List<T> attributableList, @NonNull AttributionSource attributionSource) {}
}