Set matching password?

2

Good afternoon, I'm doing an android application to connect and communicate with an arduino via Bluetooth, my problem is in relation to the pairing, where I can not set the password automatically to pair with the arduino, but it asks for the password for the user typing, ignoring the code done.

This is the code, in java, that I am trying to use, if you have any advice, it will help me a lot. Thank you.

if (broadcastActive){
            /* pesquisa remota */
            publishPair("ACTION_PAIRING_REQUEST","pareamento");
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            String name = "";
            try{
                name = device.getName().trim().toUpperCase();
            }catch (Exception e){ }
            publishPair("ACTION_PAIRING_REQUEST","Name: " + name);

            /* alguns aparelhos podem não possuir nome e, por enquanto, devem ser ingnorados */
            if (!name.equals("")) {
                publishPair("ACTION_PAIRING_REQUEST","Name: " + name);

                int permissionCheck = ContextCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_PRIVILEGED);
                publishPair("Permission", ""+permissionCheck);
                try {
                    publishPair("ACTION_PAIRING_REQUEST","Vai setar a senha");
                    int defaltPin    = Integer.parseInt(DEFAULT_PIN);
                    int pin          = intent.getIntExtra("android.bluetooth.device.extra.PAIRING_KEY", defaltPin);
                    byte[] pinBytes;
                    pinBytes         = ("" + pin).getBytes("UTF-8");
                    if(device.setPin(pinBytes)){
                        publishPair("device.setPin(pinBytes)", "sucesso");
                    }else{
                        publishPair("device.setPin(pinBytes)", "Falha");
                    }

                    if(ContextCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_PRIVILEGED) != PackageManager.PERMISSION_GRANTED){
                        BluetoothDevice dev = null;
                        try{
                            Thread.currentThread().sleep(3000); // 2 segundos
                            dev = listaDePareamento(device.getName());
                        }catch (Exception e){ }
                        if (dev == null) {
                            publishPair("ACTION_PAIRING_REQUEST","O pareamento falhou 1");
                            Toast.makeText(context, "O pareamento falhou 1", Toast.LENGTH_SHORT).show();
                        }else {
                            publishPair("ACTION_PAIRING_REQUEST", "Pareamento realizado com sucesso");
                            Toast.makeText(context, "Pareamento realizado com sucesso", Toast.LENGTH_SHORT).show();
                            broadcastActive = false;
                        }
                    }else{
                        if(device.setPairingConfirmation(true)){
                            publishPair("ACTION_PAIRING_REQUEST","Pareamento realizado com sucesso");
                            Toast.makeText(context, "Pareamento realizado com sucesso", Toast.LENGTH_SHORT).show();
                            broadcastActive = false;
                        }
                    }
                } catch (Exception ex) {
                    publishPair("ACTION_PAIRING_REQUEST","O pareamento falhou: "+ex.getMessage());
                }
            }
        }else{
            publishPair("statu", "broadcastActive é falso");
        }
    
asked by anonymous 18.05.2018 / 22:21

0 answers