Android 蓝牙开发——BluetoothAdapter(二)

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

        蓝牙开发这部分主要以APP端调用功能接口为开始到 Framework 端的调用流程最后到调用状态机结束不涉及蓝牙协议栈的开发分析。

一、APP获取适配器

1、蓝牙权限

<mainifest>
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
</mainifest>

2、获取蓝牙适配器

        所有的蓝牙 Activity 都是需要 BluetoothAdapter 的。获取 BluetoothAdapter 调用BluetoothAdapter 的静态方法 getDefaultAdapter() 方法。会返回一个表示设备自身的蓝牙适配器蓝牙无线装置的 BluetoothAdapter。如果返回 null 则说明该设备不支持蓝牙。

BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();

        此时可以调用 BluetoothAdapter 中的各种方法实现蓝牙的基本操作了。 

二、源码分析

1、BluetoothAdapter 创建及获取

public static final String BLUETOOTH_MANAGER_SERVICE = "bluetooth_manager";
private final IBluetoothManager mManagerService;
 
public static synchronized BluetoothAdapter getDefaultAdapter() {
    if (sAdapter == null) {
        sAdapter = createAdapter(BluetoothManager.resolveAttributionSource(null));
    }
    return sAdapter;
}
 
public static BluetoothAdapter createAdapter(AttributionSource attributionSource) {
    IBinder binder = ServiceManager.getService(BLUETOOTH_MANAGER_SERVICE);
    if (binder != null) {
        return new BluetoothAdapter(IBluetoothManager.Stub.asInterface(binder), attributionSource);
    } else {
        Log.e(TAG, "Bluetooth binder is null");
        return null;
    }
}
 
BluetoothAdapter(IBluetoothManager managerService, AttributionSource attributionSource) {
    mManagerService = Objects.requireNonNull(managerService);
    mAttributionSource = Objects.requireNonNull(attributionSource);
    synchronized (mServiceLock.writeLock()) {
        mService = getBluetoothService(mManagerCallback);
    }
    mLeScanClients = new HashMap<LeScanCallback, ScanCallback>();
    mToken = new Binder(DESCRIPTOR);
}

        可以看到首先在我们调用的 getDefaultAdapter 方法里去创建 Adapter然后在 BluetoothAdapter 的构造方法里去调用 getBluetoothService 方法得到 mService且 mService 的类型为 IBluetooth这是一个 aidl 的接口他的实现实在 AdapterService.java 中。

源码位置

/system/bt/service/common/android/bluetooth/IBluetooth.aidl

/packages/apps/Bluetooth/src/com/android/bluetooth/btservice/AdapterService.java

        继续查看 BluetoothAdapter 源码很多功能都是通过 mService 去调用 AdapterService 中的方法实现的。

2、常用方法

方法名描述
enable打开蓝牙功能
disable关闭蓝牙功能
isEnable判断蓝牙功能是否打开
startDiscovery开始搜索周围的蓝牙设备
cancelDiscovery取消搜索操作
isDiscovering判断当前是否正在搜索设备
getBondedDevices获取己绑定的设备列表
setName设置本机的蓝牙名称
getName获取本机的蓝牙名称
getAddress获取本机的蓝牙地址
getRemoteDevice根据蓝牙地址获取远程的蓝牙设备
getState获取本地蓝牙适配器的状态

3、Adapter属性变更

        协议栈蓝牙属性发生变化通过 AdapterProperties.java 中 adapterPropertyChangedCallback 回调函数通知上来。此变更为车机侧属性变更

static final int BT_PROPERTY_BDNAME = 0x01; // 车机蓝牙设备名称修改

static final int BT_PROPERTY_BDADDR = 0x02; // 车机蓝牙设备地址修改

static final int BT_PROPERTY_UUIDS = 0x03; // 车机蓝牙设备支持的协议更新

static final int BT_PROPERTY_CLASS_OF_DEVICE = 0x04; // 不常用

static final int BT_PROPERTY_ADAPTER_SCAN_MODE = 0x07; // 车机蓝牙设备可见性更新

static final int BT_PROPERTY_ADAPTER_BONDED_DEVICES = 0x08; // 返回车机蓝牙设备当前有多少配对成功的设备

static final int BT_PROPERTY_ADAPTER_DISCOVERABLE_TIMEOUT = 0x09; // 不常用

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: android