Pairing two Bluetooth devices by sending a message with a PIN

332 views Asked by At

Bluetooth with code-pin

Hey guys I have a problem with a program I'm doing. I want to pair two Bluetooth devices and display a message to enter a PIN. Someone could help me? Thank you very much. Regards

This is my code:

 private void getPairedDevices() {
    Set<BluetoothDevice> pairedDevice = bluetoothAdapter.getBondedDevices();            
    if(pairedDevice.size()>0)
    {
        for(BluetoothDevice device : pairedDevice)
        {
            arrayListpaired.add(device.getName()+"\n"+device.getAddress()+ "\n DescContents: " + device.describeContents()
                     + "\n BondState: " + device.getBondState()  
                     + "\n HashCode: " + device.hashCode()  
                     + "\n toString: " + device.toString()  
                     + "\n Class: " + device.getClass()   
                     + "\n Uuids: " + device.getUuids()
                     + "\n BTClass: " + device.getBluetoothClass()
                     + "\n   "
                     );
            arrayListPairedBluetoothDevices.add(device);
        }
    }
    adapter.notifyDataSetChanged();
}
class ListItemClicked implements OnItemClickListener
{
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String ACTION_PAIRING_REQUEST = "android.bluetooth.device.action.PAIRING_REQUEST";
        Intent intent = new Intent(ACTION_PAIRING_REQUEST);
        String EXTRA_DEVICE = "android.bluetooth.device.extra.DEVICE";
        String device = null;
        intent.putExtra(EXTRA_DEVICE, device);
        String EXTRA_PAIRING_VARIANT = "android.bluetooth.device.extra.PAIRING_VARIANT";
        int PAIRING_VARIANT_PIN = 10;
        intent.putExtra(EXTRA_PAIRING_VARIANT, PAIRING_VARIANT_PIN);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        Boolean isBonded = false;
        try {
            isBonded = createBond(bdDevice);
            if(isBonded)
            {
                getPairedDevices();
                adapter.notifyDataSetChanged();
            }
        } catch (Exception e) {
            e.printStackTrace(); 
        }
        Log.i("Log", "The bond is created: "+isBonded);
    }       
}
0

There are 0 answers