標籤:
串連配對密碼已知且固定的藍牙裝置時,明明在代碼裡就可以完成配對,卻依舊被系統彈出配對視窗.
這無疑是令人難受的.
所以,便嘗試著去屏蔽掉這個配對視窗.
要點:
- 中斷系統發出的藍芽配對廣播
- 需要用到ClsUtils開源架構
- 個人測試在Android 4.4上成功攔截,在4.2上不行,也許是高版本把藍芽配對廣播設為了有序廣播?
code:
public class BluetoothConnectReceiver extends BroadcastReceiver{ private final String TAG = "BluetoothConnectReceiver"; @SuppressLint("NewApi") @Override public void onReceive(Context context, Intent intent) { Log.i(TAG, TAG + " -> BluetoothConnectReceiver start!"); if (intent.getAction().equals("android.bluetooth.device.action.PAIRING_REQUEST")) { BluetoothDevice btDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); Log.i(TAG, TAG + " -> ddd"); try { Log.i(TAG, TAG + " -> Device Name = " + btDevice.getName()); if ("Device Name X".equals(btDevice.getName())) { abortBroadcast(); ClsUtils.setPin(btDevice.getClass(), btDevice, "password"); ClsUtils.createBond(btDevice.getClass(), btDevice); ClsUtils.cancelPairingUserInput(btDevice.getClass(), btDevice); } } catch (Exception e) { e.printStackTrace(); } } }}
通過預設的密碼直接靜默配對.
manifest:
<receiver android:name=".BluetoothConnectReceiver" > <intent-filter android:priority="1000" > <action android:name="android.bluetooth.device.action.PAIRING_REQUEST" /> </intent-filter></receiver>
接收器的優先順序設為最高1000,保證最先被廣播通知.
Android 屏蔽藍芽串連時的首次配對系統彈窗