Android 屏蔽藍芽串連時的首次配對系統彈窗

來源:互聯網
上載者:User

標籤:

串連配對密碼已知且固定的藍牙裝置時,明明在代碼裡就可以完成配對,卻依舊被系統彈出配對視窗.

這無疑是令人難受的.

所以,便嘗試著去屏蔽掉這個配對視窗.

 

要點:

  • 中斷系統發出的藍芽配對廣播
  • 需要用到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 屏蔽藍芽串連時的首次配對系統彈窗

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.