天天看點

android 取消藍牙配對框,android - 藍牙配對 - 如何顯示簡單的取消/配對對話框? - 堆棧記憶體溢出...

我在GitHub為這個問題準備了一個簡單的測試項目 。

我正在嘗試建立一個Android應用程式,它将從計算機螢幕掃描QR代碼,然後使用資料(MAC位址和PIN或哈希)與藍牙裝置輕松配對(綁定)。

類似于流行的InstaWifi應用程式 - 但對于經典藍牙。

出于測試目的,我還沒有進行任何條形碼掃描,隻是顯示裝置清單:

android 取消藍牙配對框,android - 藍牙配對 - 如何顯示簡單的取消/配對對話框? - 堆棧記憶體溢出...

使用者觸摸其中一個裝置後,在MainActivity.java中嘗試配對:

private void startBluetoothPairing(BluetoothDevice device) {

Intent pairingIntent = new Intent(BluetoothDevice.ACTION_PAIRING_REQUEST);

pairingIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);

pairingIntent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT,

BluetoothDevice.PAIRING_VARIANT_PIN);

pairingIntent.putExtra(BluetoothDevice.EXTRA_PAIRING_KEY, 1234);

//device.setPin(new byte[]{1,2,3,4});

//device.setPairingConfirmation(false);

startActivityForResult(pairingIntent, REQUEST_BT_SETTINGS);

}

不幸的是,彈出視窗仍然要求PIN碼:

android 取消藍牙配對框,android - 藍牙配對 - 如何顯示簡單的取消/配對對話框? - 堆棧記憶體溢出...

因為我在源代碼中實際指定了一個PIN,是以我實際上期望顯示另一個更簡單的系統對話框(這個在進行NFC OOB配對時顯示):

從搜尋解決方案,我知道有一個setPin()方法,但它不适用于這裡(或者它?) - 因為我試圖将整個智能手機與藍牙裝置配對而不僅僅是應用程式......

我的問題:如何讓Android OS顯示簡單的取消/配對對話框?

在GitHub上搜尋藍牙配對請求字元串沒有顯示任何提示......

更新:在unrealsoul007的建議(謝謝)我更新了MainActivity.java中的源代碼,現在顯示簡單的取消/配對對話框:

private void startBluetoothPairing(BluetoothDevice device) {

Intent pairingIntent = new Intent(BluetoothDevice.ACTION_PAIRING_REQUEST);

pairingIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);

pairingIntent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT,

BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION);

pairingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

startActivityForResult(pairingIntent, REQUEST_BT_PAIRING);

}

但是我不确定如何完成配對過程 - 因為即使在關閉對話框之前,也會使用resultCode=0調用onActivityResult :

@Override

public void onActivityResult(int requestCode, int resultCode, Intent intent) {

// this is called before user clicks Cancel or Pair in the dialog

if (requestCode == REQUEST_BT_PAIRING) {

if (resultCode == Activity.RESULT_OK) { // 0 != -1

Log.d("XXX", "Let#s pair!!!!"); // NOT CALLED

}

return;

}

}