Android BluetoothDevice

一、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) {}
}

三、BluetoothDevice相关类

BluetoothDevice

BluetoothDevice是一个代表蓝牙设备的类,它包含了蓝牙设备的名称、地址、类型、绑定状态等信息。你可以使用BluetoothAdapter类的方法来获取BluetoothDevice对象,例如:

BluetoothDevice代码位于:

packages/modules/Bluetooth/framework/java/android/bluetooth/BluetoothDevice.java

BluetoothDevice的定义:

public final class BluetoothDevice implements Parcelable, Attributable {}

BluetoothDevice API:

BluetoothGatt connectGatt(Context context, boolean autoConnect, BluetoothGattCallback callback):连接到由此设备托管的GATT服务器。
BluetoothGatt connectGatt(Context context, boolean autoConnect, BluetoothGattCallback callback, int transport):连接到由此设备托管的GATT服务器。
boolean createBond():开始与远程设备的绑定(配对)过程。
BluetoothSocket createInsecureRfcommSocketToServiceRecord(UUID uuid):使用 BluetoothSocket查找uuid,创建一个RFCOMM BluetoothSocket套接字准备启动到此远程设备的不安全传出连接。
BluetoothSocket  createRfcommSocketToServiceRecord(UUID uuid):创建一个RFCOMM BluetoothSocket准备好使用uuid的SDP查找启动到此远程设备的安全传出连接。
int describeContents():描述此Parcelable实例的封送表示中包含的特殊对象的种类。
boolean fetchUuidsWithSdp():在远程设备上执行服务发现以获取支持的UUID。
String getAddress():返回此BluetoothDevice的硬件地址。
int getBondState():获取远程设备的绑定状态。
String getName():获取远程设备的友好蓝牙名称。
int getType():获取远程设备的蓝牙设备类型,包括UNKNOWN、CLASSIC(BR/EDR devices) 、LE(Low Energy - LE-only)、DUAL(Dual Mode - BR/EDR/LE) 。
ParcelUuid[] getUuids():返回远程设备的支持功能(UUID)。
boolean setPairingConfirmation(boolean confirm):确认 PAIRING_VARIANT_PASSKEY_CONFIRMATION配对的密码。
boolean setPin(byte[] pin):在配对方法为 PAIRING_VARIANT_PIN时,在配对期间设置引脚
void setAttributionSource(@NonNull AttributionSource attributionSource):
BluetoothClass getBluetoothClass():获取远程设备的蓝牙类。

BluetoothClass

表示一个蓝牙类,该类描述设备的一般特征和功能。例如,Bluetooth 类将指定常规设备类型(如电话、计算机或耳机),以及它是否能够提供音频或电话等服务。

BluetoothClass代码位于:

packages/modules/Bluetooth/framework/java/android/bluetooth/BluetoothClass.java

BluetoothClass的定义:

public final class BluetoothClass implements Parcelable {}

public int getDeviceClass() :返回此 {@link BluetoothClass} 的(主要和次要)设备类类,具体如下:

   public static class Device {
        private static final int BITMASK = 0x1FFC;


        /**
         * Defines all major device class constants.
         * <p>See {@link BluetoothClass.Device} for minor classes.
         */
        public static class Major {
            private static final int BITMASK = 0x1F00;


            public static final int MISC = 0x0000;
            public static final int COMPUTER = 0x0100;
            public static final int PHONE = 0x0200;
            public static final int NETWORKING = 0x0300;
            public static final int AUDIO_VIDEO = 0x0400;
            public static final int PERIPHERAL = 0x0500;
            public static final int IMAGING = 0x0600;
            public static final int WEARABLE = 0x0700;
            public static final int TOY = 0x0800;
            public static final int HEALTH = 0x0900;
            public static final int UNCATEGORIZED = 0x1F00;
        }


        // Devices in the COMPUTER major class
        public static final int COMPUTER_UNCATEGORIZED = 0x0100;
        public static final int COMPUTER_DESKTOP = 0x0104;
        public static final int COMPUTER_SERVER = 0x0108;
        public static final int COMPUTER_LAPTOP = 0x010C;
        public static final int COMPUTER_HANDHELD_PC_PDA = 0x0110;
        public static final int COMPUTER_PALM_SIZE_PC_PDA = 0x0114;
        public static final int COMPUTER_WEARABLE = 0x0118;


        // Devices in the PHONE major class
        public static final int PHONE_UNCATEGORIZED = 0x0200;
        public static final int PHONE_CELLULAR = 0x0204;
        public static final int PHONE_CORDLESS = 0x0208;
        public static final int PHONE_SMART = 0x020C;
        public static final int PHONE_MODEM_OR_GATEWAY = 0x0210;
        public static final int PHONE_ISDN = 0x0214;


        // Minor classes for the AUDIO_VIDEO major class
        public static final int AUDIO_VIDEO_UNCATEGORIZED = 0x0400;
        public static final int AUDIO_VIDEO_WEARABLE_HEADSET = 0x0404;
        public static final int AUDIO_VIDEO_HANDSFREE = 0x0408;
        //public static final int AUDIO_VIDEO_RESERVED              = 0x040C;
        public static final int AUDIO_VIDEO_MICROPHONE = 0x0410;
        public static final int AUDIO_VIDEO_LOUDSPEAKER = 0x0414;
        public static final int AUDIO_VIDEO_HEADPHONES = 0x0418;
        public static final int AUDIO_VIDEO_PORTABLE_AUDIO = 0x041C;
        public static final int AUDIO_VIDEO_CAR_AUDIO = 0x0420;
        public static final int AUDIO_VIDEO_SET_TOP_BOX = 0x0424;
        public static final int AUDIO_VIDEO_HIFI_AUDIO = 0x0428;
        public static final int AUDIO_VIDEO_VCR = 0x042C;
        public static final int AUDIO_VIDEO_VIDEO_CAMERA = 0x0430;
        public static final int AUDIO_VIDEO_CAMCORDER = 0x0434;
        public static final int AUDIO_VIDEO_VIDEO_MONITOR = 0x0438;
        public static final int AUDIO_VIDEO_VIDEO_DISPLAY_AND_LOUDSPEAKER = 0x043C;
        public static final int AUDIO_VIDEO_VIDEO_CONFERENCING = 0x0440;
        //public static final int AUDIO_VIDEO_RESERVED              = 0x0444;
        public static final int AUDIO_VIDEO_VIDEO_GAMING_TOY = 0x0448;


        // Devices in the WEARABLE major class
        public static final int WEARABLE_UNCATEGORIZED = 0x0700;
        public static final int WEARABLE_WRIST_WATCH = 0x0704;
        public static final int WEARABLE_PAGER = 0x0708;
        public static final int WEARABLE_JACKET = 0x070C;
        public static final int WEARABLE_HELMET = 0x0710;
        public static final int WEARABLE_GLASSES = 0x0714;


        // Devices in the TOY major class
        public static final int TOY_UNCATEGORIZED = 0x0800;
        public static final int TOY_ROBOT = 0x0804;
        public static final int TOY_VEHICLE = 0x0808;
        public static final int TOY_DOLL_ACTION_FIGURE = 0x080C;
        public static final int TOY_CONTROLLER = 0x0810;
        public static final int TOY_GAME = 0x0814;


        // Devices in the HEALTH major class
        public static final int HEALTH_UNCATEGORIZED = 0x0900;
        public static final int HEALTH_BLOOD_PRESSURE = 0x0904;
        public static final int HEALTH_THERMOMETER = 0x0908;
        public static final int HEALTH_WEIGHING = 0x090C;
        public static final int HEALTH_GLUCOSE = 0x0910;
        public static final int HEALTH_PULSE_OXIMETER = 0x0914;
        public static final int HEALTH_PULSE_RATE = 0x0918;
        public static final int HEALTH_DATA_DISPLAY = 0x091C;


        // Devices in PERIPHERAL major class
        public static final int PERIPHERAL_NON_KEYBOARD_NON_POINTING = 0x0500;
        public static final int PERIPHERAL_KEYBOARD = 0x0540;
        public static final int PERIPHERAL_POINTING = 0x0580;
        public static final int PERIPHERAL_KEYBOARD_POINTING = 0x05C0;
    }

四、BluetoothDevice流程分析

BluetoothDevice 创建流程分析

Android13 BluetoothDevice 创建流程分析-CSDN博客

BluetoothDevice createBond流程分析

Android13 BluetoothDevice createBond流程分析-CSDN博客

  • 12
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值