蓝牙配对过程分析(经典蓝牙) |
您所在的位置:网站首页 › 优派x10蓝牙配对教程图解 › 蓝牙配对过程分析(经典蓝牙) |
打开手机/手表的蓝牙,就能在列表中看到扫描到的蓝牙,点击就可以实现配对功能。目前手表支持与所有设备的配对,但是仅支持与耳机类型的设备进行连接 安全简易配对SSP(Secure simple pairing),蓝牙2.0之后配对方式,简易安全配对一共有四种,其中Out of Band很少使用到,具体如下: Numeric Comparison 配对双方都显示一个6位的数字,由用户来核对数字是否一致,并输入Yes/No,两端Yes表示一致即可配对,可以防止中间人攻击。 使用场景:两端设备可以弹出6位十进制数,并且有yes和no按钮。 Passkey Entry 配对目标输入一个在本地设备上显示的6位数字,输入正确即可配对,并可以防止中间人攻击。 使用场景:一端设备可以显示,另一端设备可以输入。 Just Works 不会进行鉴权,不能防止中间人攻击用于配对没有显示没有输入的设备,主动发起连接即可配对,用户看不到配对过程,不可以防止中间人攻击,例如连接蓝牙耳机。 使用场景:用于即不能显示6位随机数,也不能输入的设备。 Out of Band 两设备的通过别的途径交换配对信息,例如一些NFC蓝牙音箱。 一、原生应用设置界面的packages\Settings目录: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); } else { final Context context = getContext(); SearchIndexableRaw data = new SearchIndexableRaw(context); data.className = BluetoothSettings.class.getName(); data.title = mCachedDevice.getName(); data.screenTitle = context.getResources().getString(R.string.bluetooth_settings); data.iconResId = R.drawable.ic_settings_bluetooth; data.enabled = true; Index.getInstance(context).updateFromSearchIndexableData(data); } }Y:\HLOS\frameworks\base\packages\SettingsLib\src\com\android\settingslib\bluetooth\CachedBluetoothDevice.java 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; } 二、framework层配对逻辑Y:\HLOS\frameworks\base\core\java\android\bluetooth\BluetoothDevice.java public boolean createBond() { if (sService == null) { Log.e(TAG, "BT not enabled. Cannot create bond to Remote Device"); return false; } try { Log.i(TAG, "createBond() for device " + getAddress() + " called by pid: " + Process.myPid() + " tid: " + Process.myTid()); return sService.createBond(this, TRANSPORT_AUTO);//跨进程调用到AdapterService中的方法 } catch (RemoteException e) {Log.e(TAG, "", e);} return false; } 三、frameworks的aidl文件 private static IBluetooth sService;HLOS\frameworks\base\core\java\android\bluetooth\IBluetooth.aidl boolean createBond(in BluetoothDevice device); 这个是属于跨进程通信的方法,通过IBluetooth.aidl文件,调用到AdapterService.java中的createBond方法 四、从framework跨进程调到packages\BluetoothY:\HLOS\packages\apps\Bluetooth\src\com\android\bluetooth\btservice\AdapterService.java public boolean createBond(BluetoothDevice device, int transport) { if (!Utils.checkCallerAllowManagedProfiles(mService)) { Log.w(TAG, "createBond() - Not allowed for non-active user"); return false; } AdapterService service = getService(); if (service == null) return false; return service.createBond(device, transport, null);//调用本服务的createBond }同在AdapterService文件下 boolean createBond(BluetoothDevice device, int transport, OobData oobData) { enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, "Need BLUETOOTH ADMIN permission"); DeviceProperties deviceProp = mRemoteDevices.getDeviceProperties(device); if (deviceProp != null && deviceProp.getBondState() != BluetoothDevice.BOND_NONE) { return false; } // Multicast: Do not allow bonding while multcast A2dpService a2dpService = A2dpService.getA2dpService(); if (a2dpService != null && a2dpService.isMulticastFeatureEnabled() && a2dpService.isMulticastOngoing(null)) { Log.i(TAG,"A2dp Multicast is ongoing, ignore bonding"); return false; } // Pairing is unreliable while scanning, so cancel discovery // Note, remove this when native stack improves cancelDiscoveryNative();//配对过程,取消扫描 Message msg = mBondStateMachine.obtainMessage(BondStateMachine.CREATE_BOND); msg. |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |