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