您的位置:首页 > 编程语言

代码流程分析二:Settings-蓝牙分析-点击配对连接设备原理分析

2015-08-27 15:29 441 查看
蓝牙配对功能

配对手机,配对蓝牙耳机,配对电脑

(一)设置界面packages\Settings

A:搜索调用的地方:点击preference因为下面的条目是单独的一个所以到了blueetoothSettings的父类中。

目录:android\packages\apps\Settings\src\com\android\settings\bluetooth\DeviceListPreferenceFragment.java

void onDevicePreferenceClick(BluetoothDevicePreference btPreference) {

btPreference.onClicked();

}

B:

目录:android\packages\apps\Settings\src\com\android\settings\bluetooth\BluetoothDevicePreference.java

void onClicked() {

int bondState = mCachedDevice.getBondState();

if (mCachedDevice.isConnected()) {

askDisconnect();------------------------断开连接

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

mCachedDevice.connect(true);已经配对了就连接

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

pair();---------------------------没有配对就配对

}

}

private void pair() {

if (!mCachedDevice.startPairing()) {

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

R.string.bluetooth_pairing_error_message);

}

}

C:配对mCachedDevice.startPairing())

目录:android\packages\apps\Settings\src\com\android\settings\bluetooth\CachedBluetoothDevice.java
boolean startPairing() {

if(mLocalAdapter.checkPairingState() == true)

{

return true;------------------------------正在配对就终止该方法

}

mLocalAdapter.setPairingState(true);

if (mLocalAdapter.isDiscovering()) {

mLocalAdapter.cancelDiscovery();------------------当前设备正在搜索,就取消搜索

}

if (!mDevice.createBond()) {

mLocalAdapter.setPairingState(false);

return false;

}

mConnectAfterPairing = true; // auto-connect after pairing

return true;

}

(二)走到frameworks

D:配对mDevice.createBond()

目录:android\frameworks\base\core\java\android\bluetooth\bluetoothDevice.java
public boolean createBond() {

..............

return sService.createBond(this);

} catch (RemoteException e) {Log.e(TAG, "", e);}

return false;

}

private staticIBluetooth sService;

(三)走到frameworks的aidl文件

E:aidl文件

目录:android\frameworks\base\core\java\android\bluetooth\IBluetooth.aidl
boolean createBond(in BluetoothDevice device);

属于跨进程调用的写法不需要研究;所以直接搜索createBond()看在上层哪实现的这个方法。

(四)从framework跨进程调到packages\Bluetooth

F:mService.createBond()方法:

目录:android\packages\apps\Bluetooth\src\com\android\bluetooth\btservice\AdapterService.java

(1):公共
public boolean createBond(BluetoothDevice device) {

...............................

AdapterService service = getService();

if (service == null) return false;

return service.createBond(device);这的打log

}

(2):走

boolean createBond(BluetoothDevice device) {

enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,

"Need BLUETOOTH ADMIN permission");注册

........................................

cancelDiscoveryNative();

Message msg = mBondStateMachine.obtainMessage(BondStateMachine.CREATE_BOND);

msg.obj = device;

mBondStateMachine.sendMessage(msg);-----------------------------------再给那边发一个消息,创建了BondStateMachine.CREATE_BOND

return true;

}

(3):调到native,这是个标志,有native声明就会调用,系统会自动识别出来,因为有个native的大表。

private native booleancancelDiscoveryNative();这个是取消搜索设备,就是说配对的时候要取消搜索。
(3): mBondStateMachine.sendMessage(msg);这是主要的

G:接收消息:

目录:android\packages\apps\Bluetooth\src\com\android\bluetooth\btservice\BondStateMachine.java

switch(msg.what) {

case CREATE_BOND:这也得打,因为好像是2个都有这个方法

createBond(dev, true);

break;

当接收到的消息为CREATE_BOND时,执行createBond方法:

private boolean createBond(BluetoothDevice dev, boolean transition) {

if(mAdapterService == null) return false;

if (dev.getBondState() == BluetoothDevice.BOND_NONE) {

infoLog("Bond address is:" + dev);

byte[] addr = Utils.getBytesFromAddress(dev.getAddress());

if (!mAdapterService.createBondNative(addr)) {打印这个boolean值

sendIntent(dev, BluetoothDevice.BOND_NONE,

BluetoothDevice.UNBOND_REASON_REMOVED);

return false;

} else if (transition) {

transitionTo(mPendingCommandState);

}

return true;

}

return false;

}

H:AdapterService.createBondNative(addr)):

目录:android\packages\apps\Bluetooth\src\com\android\bluetooth\btservice\adapterService.java
又回到了F中才调native,其实之前F中就可以直接调的

native boolean createBondNative(byte[] address);

(五)packages\Bluetooth的java层调到jini层的cpp文件

I:nativeCpp.startDiscoveryNative方法:

目录:android\packages\apps\Bluetooth\jni\com_android_bluetooth_btservice_AdapterService.cpp

注册的流程跟前面的搜索一样。
static jboolean createBondNative(JNIEnv* env, jobject obj, jbyteArray address) {



addr = env->GetByteArrayElements(address, NULL);

if (addr == NULL) {

jniThrowIOException(env, EINVAL);

return result;

}

int ret = sBluetoothInterface->create_bond((bt_bdaddr_t *)addr);

env->ReleaseByteArrayElements(address, addr, 0);

result = (ret == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE;

return result;

}

(六)packages\Bluetooth的java层调到jini层的cpp文件--------------->在调到Hal层的.h文件中bt_interface_t ->create_bond()的一个接口;

H:sBluetoothInterface->create_bond()方法:

目录:android\hardware\libhardware\include\hardware\bluetooth.h
int (*create_bond)(const bt_bdaddr_t *bd_addr);

再往下就调到extenel层了。。。。现在只是发过去createBond
---------------------------------------------------------------------------------------------------------------------------------------
A:点击配对上层BluetoothEventManager收到的Log流程

点击配对的Log
怀疑会有三个状态,正在配对,配对中,配对完。完了配对好了又一个状态,然后才用uuid连接

01-08 18:30:02.979: V/lwn(3153): ScanningStateChangedHandler intent=Intent { act=android.bluetooth.adapter.action.DISCOVERY_FINISHED flg=0x10 } BluetoothDevice=null------------------------搜索结束

01-08 18:30:02.989: V/lwn(3153): BondStateChangedHandler intent=Intent { act=android.bluetooth.device.action.BOND_STATE_CHANGED flg=0x10 (has extras) } BluetoothDevice=3C:91:57:58:46:FA-----------配对状态变了

01-08 18:30:03.809: V/lwn(3153): NameChangedHandler intent=Intent { act=android.bluetooth.device.action.NAME_CHANGED flg=0x4000010 (has extras) } BluetoothDevice=3C:91:57:58:46:FA----------------应该是开始配对最后设备名字

01-08 18:30:05.149: V/lwn(3153): NameChangedHandler intent=Intent { act=android.bluetooth.device.action.NAME_CHANGED flg=0x4000010 (has extras) } BluetoothDevice=3C:91:57:58:46:FA----------------应该是正在配对设备的名字

01-08 18:30:05.349: V/lwn(3153): NameChangedHandler intent=Intent { act=android.bluetooth.device.action.NAME_CHANGED flg=0x4000010 (has extras) } BluetoothDevice=3C:91:57:58:46:FA----------------应该是配对成功设备的名字

01-08 18:30:05.359: V/lwn(3153): ClassChangedHandler intent=Intent { act=android.bluetooth.device.action.CLASS_CHANGED flg=0x4000010 (has extras) } BluetoothDevice=3C:91:57:58:46:FA---------------应该是配对设备的class

------对话框之前,点了配对另外一边也配对以后

01-08 18:30:16.069: V/lwn(3153): BondStateChangedHandler intent=Intent { act=android.bluetooth.device.action.BOND_STATE_CHANGED flg=0x10 (has extras) } BluetoothDevice=3C:91:57:58:46:FA--------应该是配对成功的状态

01-08 18:30:16.089: V/lwn(3153): UuidChangedHandler intent=Intent { act=android.bluetooth.device.action.UUID flg=0x10 (has extras) } BluetoothDevice=3C:91:57:58:46:FA----------------------------------------手机的连接uuid

01-08 18:30:16.099: V/lwn(3153): NameChangedHandler intent=Intent { act=android.bluetooth.device.action.NAME_CHANGED flg=0x4000010 (has extras) } BluetoothDevice=3C:91:57:58:46:FA----------------应该是连接成功的设备的名字

-----出来对话框不做操作,没配成功

01-08 18:42:10.649: V/lwn(3153): BondStateChangedHandler intent=Intent { act=android.bluetooth.device.action.BOND_STATE_CHANGED flg=0x10 (has extras) } BluetoothDevice=3C:91:57:58:46:FA

-----取消配对

01-08 18:40:24.609: V/lwn(3153): BondStateChangedHandler intent=Intent { act=android.bluetooth.device.action.BOND_STATE_CHANGED flg=0x10 (has extras) } BluetoothDevice=3C:91:57:58:46:FA

上面的这些log只是cpp文件中的一部分,它会发出很多,这只是其中的几个。。。

......................经过下层的调用,参考:http://blog.csdn.net/wendell_gong/article/details/16864467

(一)packages\Bluetooth的jini层的cpp文件

(1):sBluetoothCallbacks

bt_callbacks_t sBluetoothCallbacks = {

sizeof(sBluetoothCallbacks),

adapter_state_change_callback,

adapter_properties_callback,

remote_device_properties_callback,-------------->别的设备的name和class和uuid

device_found_callback,-----------------------搜索别的时候发现了设备

discovery_state_changed_callback,-------------->搜索开始结束

wake_state_changed_callback,

pin_request_callback,

ssp_request_callback,

bond_state_changed_callback,-------------->配对用

acl_state_changed_callback,

..................................

};

(3):

a:discovery_state_changed_callback

method_discoveryStateChangeCallback = env->GetMethodID(jniCallbackClass,

"discoveryStateChangeCallback", "(I)V");

b:bond_state_changed_callback,

method_bondStateChangeCallback = env->GetMethodID(jniCallbackClass,

"bondStateChangeCallback", "(I[BI)V");

c:adapter_properties_callback

method_devicePropertyChangedCallback = env->GetMethodID(jniCallbackClass,

"devicePropertyChangedCallback",

"([B[I[[B)V");

----------------------------------------------------------------jini调到java总之会调用到下面。

(二)jini调java

目录:android\packages\apps\Bluetooth\src\com\android\bluetooth\btservice\jniCallbacks.java
private AdapterProperties mAdapterProperties;

void discoveryStateChangeCallback(int state) {

mAdapterProperties.discoveryStateChangeCallback(state);

}

void bondStateChangeCallback(int status, byte[] address, int newState) {

mBondStateMachine.bondStateChangeCallback(status, address, newState);

}

void devicePropertyChangedCallback(byte[] address, int[] types, byte[][] val) {

mRemoteDevices.devicePropertyChangedCallback(address, types, val);

}

(二)java调java

D1:AdapterProperties .discoveryStateChangeCallback()方法:

目录:android\packages\apps\Bluetooth\src\com\android\bluetooth\adapterProperties.java

void discoveryStateChangeCallback(int state) {

.................

intent = new Intent(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);

mService.sendBroadcast(intent, mService.BLUETOOTH_PERM);

} else if ((state == AbstractionLayer.BT_DISCOVERY_STARTED) && !mDiscovering) {

mDiscovering = true;

intent = new Intent(BluetoothAdapter.ACTION_DISCOVERY_STARTED);

mService.sendBroadcast(intent, mService.BLUETOOTH_PERM);

}

}

}

D2:BondStateMachine.bondStateChangeCallback()方法:

目录:android\packages\apps\Bluetooth\src\com\android\bluetooth\bondStateMachine.java
A:void bondStateChangeCallback(int status, byte[] address, int newState) {



Message msg = obtainMessage(BONDING_STATE_CHANGE);

msg.obj = device;

if (newState == BOND_STATE_BONDED)

msg.arg1 = BluetoothDevice.BOND_BONDED;

else if (newState == BOND_STATE_BONDING)

msg.arg1 = BluetoothDevice.BOND_BONDING;

else

msg.arg1 = BluetoothDevice.BOND_NONE;

msg.arg2 = status;

sendMessage(msg);这是关键,他给自己发出去一个消息

B:switch(msg.what) {

case BONDING_STATE_CHANGE:

if (newState == BluetoothDevice.BOND_BONDING)

{

if(!mDevices.contains(dev)) {

mDevices.add(dev);

}

sendIntent(dev, newState, 0);

transitionTo(mPendingCommandState);

}

else
if(newState == BluetoothDevice.BOND_NONE) {

sendIntent(dev, newState, 0);

}
C:private void sendIntent(BluetoothDevice device, int newState, int reason) {

.............

Intent intent = new Intent(BluetoothDevice.ACTION_BOND_STATE_CHANGED);

intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);

intent.putExtra(BluetoothDevice.EXTRA_BOND_STATE, newState);

intent.putExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, oldState);

if (newState == BluetoothDevice.BOND_NONE)

intent.putExtra(BluetoothDevice.EXTRA_REASON, reason);

mAdapterService.sendBroadcastAsUser(intent, UserHandle.ALL,

AdapterService.BLUETOOTH_PERM);

}



D3:mRemoteDevices.devicePropertyChangedCallback(address, types, val);

目录:android\packages\apps\Bluetooth\src\com\android\bluetooth\remoteDevices.java

(1):Log中name

void devicePropertyChangedCallback{

case AbstractionLayer.BT_PROPERTY_BDNAME:

device.mName = new String(val);

intent = new Intent(BluetoothDevice.ACTION_NAME_CHANGED);

............................

break;

(2):Log中class

case AbstractionLayer.BT_PROPERTY_CLASS_OF_DEVICE:

device.mBluetoothClass = Utils.byteArrayToInt(val);

intent = new Intent(BluetoothDevice.ACTION_CLASS_CHANGED);

..............................

break;

}

(3):Log中uuid

case AbstractionLayer.BT_PROPERTY_UUIDS:

int numUuids = val.length/AbstractionLayer.BT_UUID_SIZE;

device.mUuids = Utils.byteArrayToUuid(val);

sendUuidIntent(bdDevice);

break;

private void sendUuidIntent(BluetoothDevice device) {

DeviceProperties prop = getDeviceProperties(device);

Intent intent = new Intent(BluetoothDevice.ACTION_UUID);

intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);

intent.putExtra(BluetoothDevice.EXTRA_UUID, prop == null? null: prop.mUuids);

mAdapterService.sendBroadcast(intent, AdapterService.BLUETOOTH_ADMIN_PERM);

mSdpTracker.remove(device);

}

出来对话框不做操作,没配成功

01-08 18:42:10.649: V/lwn(3153): BondStateChangedHandler intent=Intent { act=android.bluetooth.device.action.BOND_STATE_CHANGED flg=0x10 (has extras) } BluetoothDevice=3C:91:57:58:46:FA

取消配对

01-08 18:40:24.609: V/lwn(3153): BondStateChangedHandler intent=Intent { act=android.bluetooth.device.action.BOND_STATE_CHANGED flg=0x10 (has extras) } BluetoothDevice=3C:91:57:58:46:FA
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: