การจับคู่บลูทูธกับ Nrf UART ทำงานไม่ถูกต้อง

การจับคู่ Bluetooth ทำงานไม่ถูกต้อง ฉันกำลังพัฒนาแอปพลิเคชันโดยใช้การจับคู่ Bluetooth กับ UART ที่นี่ฉันได้รวมแนวคิดและโปรแกรมของฉันไว้แล้ว ช่วยฉันแก้ไขปัญหาด้วย

ผลลัพธ์ที่คาดหวังของฉันคือหากผู้ใช้กดปุ่มเชื่อมต่อ ควรจับคู่โดยไม่ต้องป้อนข้อมูลจากผู้ใช้และหน้าจอยืนยันสำหรับคำขอจับคู่และ PIN ในที่สุดอุปกรณ์ก็ตอบสนองต่อการเชื่อมต่ออีกครั้ง

ผลลัพธ์ที่แท้จริงของฉันคือหน้าจอยืนยันและป๊อปอัปอินพุตของผู้ใช้จะเปิดขึ้น หลังจากนั้นอุปกรณ์จะถูกจับคู่ ในที่สุดอุปกรณ์ก็ไม่ตอบสนองกลับไปว่าฉันเชื่อมต่อแล้ว

ฉันติดอยู่ในปัญหานั้นมากกว่า 2 วัน ช่วยฉันออกจากปัญหานี้

1. ลงทะเบียน PAIRING ในวิธี onstart()

          IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
         this.registerReceiver(mPairingRequestReceiver, filter);

<แข็งแกร่ง>2. BroadcastReceiver สำหรับรับคำขอการจับคู่

  private BroadcastReceiver mPairingRequestReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(BluetoothDevice.ACTION_PAIRING_REQUEST)) {
            try {
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                int pin = intent.getIntExtra("android.bluetooth.device.extra.PAIRING_KEY", 123456);
                //the pin in case you need to accept for an specific pin
                byte[] pinBytes;
                pinBytes = ("" + pin).getBytes("UTF-8");
                device.setPin(pinBytes);


        } catch (Exception e) {
                Log.e(TAG, "Error occurs when trying to auto pair");
                e.printStackTrace();
            }
        }
    }
};

/* หลังจากเชื่อมต่ออุปกรณ์แล้ว ฉันกำลังสร้างพันธบัตร*/

     @Override
     public void onDeviceConnected(BluetoothDevice device) {

        device.createBond();

      }

person sivaprakash    schedule 22.01.2018    source แหล่งที่มา


คำตอบ (1)


คุณสามารถข้ามกระบวนการจับคู่ Bluetooth ดั้งเดิมและจับคู่กับอุปกรณ์ต่อพ่วง Bluetooth โดยทางโปรแกรมตามหลักไวยากรณ์ ลองสิ่งนี้:

ลงทะเบียนผู้รับสำหรับ BluetoothDevice.ACTION_PAIRING_REQUEST ด้วยลำดับความสำคัญสูงสุด

private void notPaired(){
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
    filter.setPriority(SYSTEM_HIGH_PRIORITY-1);
    registerReceiver(mReceiver, filter);
    mDevice.createBound();// OR establish connection with the device and read characteristic for triggering the pairing process 
    getBoundState();
}

private final BroadcastReceiver mReceiver = new BroadcastReceiver()
{
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if(BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)){
            final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, BluetoothDevice.ERROR);

            if(type == BluetoothDevice.PAIRING_VARIANT_PIN){
                byte[] pin = "123456".getBytes();
                device.setPin(pin);
                Log.i("Pairing Process ", "Pairing Key Entered");
                abortBroadcast();
            }else
                Log.i("Pairing Process: ", "Unexected Pairing type");
        }
    }
};

เพื่อให้แน่ใจว่าอุปกรณ์ได้รับการจับคู่แล้ว ให้ลงทะเบียนตัวรับสัญญาณสำหรับ BluetoothDevice.ACTION_BOND_STATE_CHANGED

private void getBoundState(){
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
    registerReceiver(boundStateReciver, filter);
}

private final BroadcastReceiver boundStateReciver= new BroadcastReceiver()
{
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
            final int d = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,-1);
            switch(d){
                case BluetoothDevice.BOND_BONDED:
                    Log.i("Pairing Process ", "Paired successfully");
                break;
            }
        }
    }
};

ใน Manifests ให้เพิ่มสิทธิ์ นี้

<uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" />
person Salman Naseem    schedule 22.01.2018
comment
คุณต้องตั้งค่าลำดับความสำคัญสูงสุดให้กับ IntentFilter ของคุณเพื่อหลีกเลี่ยงป๊อปอัปการจับคู่ระบบ - person Salman Naseem; 22.01.2018
comment
ฉันได้ลองทั้งลำดับความสำคัญสูงและต่ำแล้ว มันไม่ได้ทำงานได้ดี มันแสดงป๊อปอัปเดียวกัน - person sivaprakash; 22.01.2018
comment
พวกคุณไม่ได้รับป๊อปอัปการจับคู่ที่มาและไปเพียงเสี้ยววินาทีใช่ไหม? - person Yogesh Byndoor; 07.11.2018
comment
@YogeshByndoor ตั้งค่าลำดับความสำคัญสูงในตัวกรองเจตนา filter.setPriority(SYSTEM_HIGH_PRIORITY-1); - person Salman Naseem; 07.11.2018