java连接蓝牙原理流程_Bluetooth源码分析(三)蓝牙配对流程

先附上总结的时序图:

蓝牙配对时序图.png

1 UI

蓝牙配对开始于settings设备列表 /packages/apps/Settings/src/com/android/settings/bluetooth/DeviceListPreferenceFragment.java中。

DeviceListPreferenceFragment是蓝牙扫描到的设备列表,点击其中一个蓝牙设备,调用onPreferenceTreeClick方法开始蓝牙的配对过程。

@Override

public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,

Preference preference) {

if (KEY_BT_SCAN.equals(preference.getKey())) {

mLocalAdapter.startScanning(true);

return true;

}

if (preference instanceof BluetoothDevicePreference) {

BluetoothDevicePreference btPreference = (BluetoothDevicePreference) preference;

CachedBluetoothDevice device = btPreference.getCachedDevice();

mSelectedDevice = device.getDevice();

//配对连接

onDevicePreferenceClick(btPreference);

return true;

}

return super.onPreferenceTreeClick(preferenceScreen, preference);

}

在本地onDevicePreferenceClick方法中调用/packages/apps/Settings/src/com/android/settings/bluetooth/BluetoothDevicePreference.java的onClicked方法:

void onClicked() {

Context context = getContext();

int bondState = mCachedDevice.getBondState();// 获取设备的绑定状态

final MetricsFeatureProvider metricsFeatureProvider =

FeatureFactory.getFactory(context).getMetricsFeatureProvider();

if (mCachedDevice.isConnected()) {

metricsFeatureProvider.action(context,

MetricsEvent.ACTION_SETTINGS_BLUETOOTH_DISCONNECT);

askDisconnect(); // 已连接,询问是否断开连接

} else if (bondState == BluetoothDevice.BOND_BONDED) {

metricsFeatureProvider.action(context,

MetricsEvent.ACTION_SETTINGS_BLUETOOTH_CONNECT);

mCachedDevice.connect(true);// 已绑定,则进行连接

} else if (bondState == BluetoothDevice.BOND_NONE) {

metricsFeatureProvider.action(context,

MetricsEvent.ACTION_SETTINGS_BLUETOOTH_PAIR);

if (!mCachedDevice.hasHumanReadableName()) {

metricsFeatureProvider.action(context,

MetricsEvent.ACTION_SETTINGS_BLUETOOTH_PAIR_DEVICES_WITHOUT_NAMES);

}

pair();// 如果未绑定,则进行配对

}

}

这里先获取mCachedDevice的绑定状态,如果已经连接,则询问是否断开;如果已经绑定未连接,则开始连接;如果未连接也未绑定,则开始配对。这里我们先看配对。配对调用的是本地的pair方法:

private void pair() {

if (!mCachedDevice.startPairing()) {

Utils.showError(getContext(), mCachedDevice.getName(),

R.string.bluetooth_pairing_error_message);

}

}

pair方法会调用/frameworks/base/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java中的startPairing,启动配对

2 framework

public boolean startPairing() {

// Pairing is unreliable while scanning, so cancel discovery

// 配对时,如果正在扫描,则取消扫描

if (mLocalAdapter.isDiscovering()) {

mLocalAdapter.cancelDiscovery();

}

// 开始配对

if (!mDevice.createBond()) {

return false;

}

// 标识位,配对完成后,自动连接

mConnectAfterPairing = true; // auto-connect after pairing

return true;

}

createBond调用/frameworks/base/core/java/android/bluetooth/BluetoothDevice.java

中的createBond方法:

public boolean createBond(int transport) {

final IBluetooth service = sService;

if (service == null) {

Log.e(TAG, "BT not enabled. Cannot create bond to Remote Device");

return false;

}

if (TRANSPORT_AUTO > transport || transport > TRANSPORT_LE) {

throw new IllegalArgumentException(transport + " is not a valid Bluetooth transpo

你可能感兴趣的:(java连接蓝牙原理流程)