2
Good afternoon, I’m making an android application to connect and communicate with an Internet via Bluetooth, my problem is regarding pairing, where I can’t set the password automatically to pair with the Internet, but it asks the password for the user to type, ignoring the code made.
This is the code, in java, that I’m trying to use, if you have any advice 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");
}