Android Bluetooth源码分析总结 - framework部分

Android Bluetooth源码分析总结 - framework部分


本篇主要包括如下内容:
1. 源码目录
2. 类图
3. use case举例

  • 蓝牙服务初始化
  • 打开蓝牙流程
  • 搜索蓝牙流程

4. 应用层开发总结


1,BT framework代码主要位于如下目录:

android/frameworks/base/core/java/android/bluetooth
android/frameworks/base/services/core/java/com/android/server/BluetoothManagerService.java
android/packages/apps/Bluetooth

2,class diagram如下图所示:

主要类设计意图说明如下(摘录源码):

  • BluetoothAdapter 
/**
 * Represents the local device Bluetooth adapter. The BluetoothAdapter
 * lets you perform fundamental Bluetooth tasks, such as initiate
 * device discovery, query a list of bonded (paired) devices,
 * instantiate a BluetoothDevice using a known MAC address, and create
 * a BluetoothServerSocket to listen for connection requests from other
 * devices, and start a scan for Bluetooth LE devices.
 *
 * To get a BluetoothAdapter representing the local Bluetooth
 * adapter, when running on JELLY_BEAN_MR1 and below, call the
 * static getDefaultAdapter method; when running on JELLY_BEAN_MR2 and
 * higher, retrieve it through
 * android.content.Context#getSystemService with
 * android.content.Context#BLUETOOTH_SERVICE.
 * Fundamentally, this is your starting point for all
 * Bluetooth actions. Once you have the local adapter, you can get a set of
 * BluetoothDevice objects representing all paired devices with
 * getBondedDevices(); start device discovery with
 * startDiscovery(); or create a BluetoothServerSocket to
 * listen for incoming connection requests with
 * listenUsingRfcommWithServiceRecord(String,UUID); or start a scan for
 * Bluetooth LE devices with startLeScan(LeScanCallback callback).
 *
 * Note:
 * Most methods require the android.Manifest.permission#BLUETOOTH
 * permission and some also require the
 * android.Manifest.permission#BLUETOOTH_ADMIN permission.
 *
 */
public final class BluetoothAdapter {
        ......
}
  • BluetoothManagerService
class BluetoothManagerService extends IBluetoothManager.Stub {
        ......
}
/**
 * System private API for talking with the Bluetooth service.
 *
 * {@hide}
 */
interface IBluetoothManager
{
    IBluetooth registerAdapter(in IBluetoothManagerCallback callback);
    void unregisterAdapter(in IBluetoothManagerCallback callback);
    void registerStateChangeCallback(in IBluetoothStateChangeCallback callback);
    void unregisterStateChangeCallback(in IBluetoothStateChangeCallback callback);
    boolean isEnabled();
    boolean enable();
    boolean enableNoAutoConnect();
    boolean disable(boolean persist);
    IBluetoothGatt getBluetoothGatt();

    boolean bindBluetoothProfileService(int profile, IBluetoothProfileServiceConnection proxy);
    void unbindBluetoothProfileService(int profile, IBluetoothProfileServiceConnection proxy);

    String getAddress();
    String getName();

    boolean isBleScanAlwaysAvailable();
    int updateBleAppCount(IBinder b, boolean enable);
    boolean isBleAppPresent();
}
  • IBluetooth
/**
 * System private API for talking with the Bluetooth service.
 *
 * {@hide}
 */
interface IBluetooth
{
    boolean isEnabled();
    int getState();
    boolean enable();
    boolean enableNoAutoConnect();
    boolean disable();

    String getAddress();
    ParcelUuid[] getUuids();
    boolean setName(in String name);
    String getName();

    int getScanMode();
    boolean setScanMode(int mode, int duration);

    int getDiscoverableTimeout();
    boolean setDiscoverableTimeout(int timeout);

    boolean startDiscovery();
    bool
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值