目录
上一篇《Android 6.0 Settings源码简单分析之蓝牙(1)》我们只是简单的介绍了蓝牙的界面和流程的启动,讲得比较粗糙,这次我们继续深入讨论蓝牙,设计Framework的代码
源码:Android 6.0
应用:Settings 和 Framework(权且看做一个应用吧^_^)
目录:
Settings\src\com\android\settings\bluetooth
base\packages\settingslib\src\com\android\settingslib\bluetooth
base\services\core\java\com\android\server
好了,原归正传,下面由我细细道来。
由上一篇(如上)知道,虽然Settings中蓝牙入口类是BluetoothSettings.java,但其没有重写onCreate()方法,因此先执行了其父类DeviceListPreferenceFragment.java,因此开始从其父类开始从头讲起。
1、DeviceListPreferenceFragment.onCreate()
//DeviceListPreferenceFragment.java 【Settings】
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2、蓝牙管理类的初始化
mLocalManager = Utils.getLocalBtManager(getActivity());
if (mLocalManager == null) {
Log.e(TAG, "Bluetooth is not supported on this device");
return;
}
获取LocalAdapter,这个在蓝牙管理类初始化时就初始化了
mLocalAdapter = mLocalManager.getBluetoothAdapter();
......
}
2、蓝牙管理类的初始化
mLocalManager = Utils.getLocalBtManager(getActivity());
蓝牙管理类的初始化,这里封装起来了,因为后续很多地方都在调用这个方法。
//Utils.java 【Settings】
public static LocalBluetoothManager getLocalBtManager(Context context) {
LocalBluetoothManager初始化
return LocalBluetoothManager.getInstance(context, mOnInitCallback);
}
LocalBluetoothManager使用了懒汉单例模式,加了synchronized表示线程安全,一次只能一个访问。
//LocalBluetoothManager.java 【Framework】
public static synchronized LocalBluetoothManager getInstance(Context context,
BluetoothManagerCallback onInitCallback) {
非null判断,第一次调用时走这里
if (sInstance == null) {
2.1 LocalBluetoothAdapter的初始化
LocalBluetoothAdapter adapter = LocalBluetoothAdapter.getInstance();
if (adapter == null) {
return null;
}
// This will be around as long as this process is
Context appContext = context.getApplicationContext();
sInstance = new LocalBluetoothManager(adapter, appContext);
if (onInitCallback != null) {
onInitCallback.onBluetoothManagerInitialized(appContext, sInstance);
}
}
return sInstance;
}
private LocalBluetoothManager(LocalBluetoothAdapter adapter, Context context) {
mContext = context;
mLocalAdapter = adapter;
mCachedDeviceManager = new CachedBluetoothDeviceManager(context, this);
mEventManager = new BluetoothEventManager(mLocalAdapter,
mCachedDeviceManager, context);
mProfileManager = new LocalBluetoothProfileManager(context,
mLocalAdapter, mCachedDeviceManager, mEventManager);
}
2.1 、LocalBluetoothAdapter的初始化
也是用了懒汉单例模式
//LocalBluetoothAdapter.java 【Framework】
static synchronized LocalBluetoothAdapter getInstance() {
懒汉模式
if (sInstance == null) {
2.2、BluetoothAdapter通过getDefaultAdapter初始化
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter != null) {
sInstance = new LocalBluetoothAdapter(adapter);
}
}
return sInstance;
}
private LocalBluetoothAdapter(BluetoothAdapter adapter) {
mAdapter = adapter;
}
2.2、BluetoothAdapter通过getDefaultAdapter初始化
Android代码中有注释,默认提供一种蓝牙适配器,但是API可以支持多种。(直接看英文吧)
Get a handle to the default local Bluetooth adapter.Currently Android only supports one Bluetooth adapter, but the API could be extended to support more. This will always return the default adapter.
如果getDefaultAdapter()返回是null,表示当前设备不支持蓝牙。
public static synchronized BluetoothAdapter getDefaultAdapter() {
if (sAdapter == null) {
【返回蓝牙服务的引用】
IBinder b = ServiceManager.getService(BLUETOOTH_MANAGER_SERVICE);
if (b != null) {
【获取BluetoothManagerService】
IBluetoothManager managerService = IBluetoothManager.Stub.asInterface(b);
sAdapter = new BluetoothAdapter(managerService);
} else {
Log.e(TAG, "Bluetooth binder is null");
}
}
return sAdapter;
}
BluetoothAdapter初始化
BluetoothAdapter(IBluetoothManager managerService) {
【判读蓝牙服务是否启动,如果没有就抛异常】
if (managerService == null) {
throw new IllegalArgumentException("bluetooth manager service is null");
}
try {
【注册adapter,mManagerCallback】
mService = managerService.registerAdapter(mManagerCallback);
} catch (RemoteException e) {Log.e(TAG, "", e);}
mManagerService = managerService;
mLeScanClients = new HashMap<LeScanCallback, ScanCallback>();
mToken = new Binder();
}
3、
【没有完成。。。 [尴尬]。。。还是先放出来了 】