Pair a bluetooth device in Android Studio

19,682

First request BLUETOOTH_ADMIN permission.

Then make your device discoverable:

private void makeDiscoverable() {
        Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
        startActivity(discoverableIntent);
        Log.i("Log", "Discoverable ");
    }

Then create a BroadcastReceiver to listen to action from system:

private BroadcastReceiver myReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Message msg = Message.obtain();
            String action = intent.getAction();
            if(BluetoothDevice.ACTION_FOUND.equals(action)){
               //Found, add to a device list
            }           
        }
    };

And start searching for devices by registering this BoardcastReceiver:

 private void startSearching() {
        Log.i("Log", "in the start searching method");
        IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        BluetoothDemo.this.registerReceiver(myReceiver, intentFilter);
        bluetoothAdapter.startDiscovery();
    }

After devices come from the BroadcastReceiver into a list, select your device from the list and createBond() with this:

 public boolean createBond(BluetoothDevice btDevice)  
    throws Exception  
    { 
        Class class1 = Class.forName("android.bluetooth.BluetoothDevice");
        Method createBondMethod = class1.getMethod("createBond");  
        Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice);  
        return returnValue.booleanValue();  
    } 

Then use your code above to manage with bonded devices.

Share:
19,682
Kirchhoff1415
Author by

Kirchhoff1415

Updated on June 14, 2022

Comments

  • Kirchhoff1415
    Kirchhoff1415 almost 2 years

    I'm creating an app that should connect via bluetooth to a specific device.

    I want my app to connect with this device no matter it is already paired or not.

    For now I have this

    private void findDevice() {
        Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
        if (pairedDevices.size() > 0) {
            for (BluetoothDevice device : pairedDevices) {
                if (device.getName().equals(DEVICE_NAME)) {
                    bluetoothDevice = device;
                    deviceFound = true;
                    break;
                }
            }
        }
    }
    

    But this function connects only to paired devices. If my device isn't already paired, I want to pair it. Have no idea how to do this.

    Can someone get me any advice please?

    • nhoxbypass
      nhoxbypass over 6 years
      Did you request BLUETOOTH_ADMIN permission?
    • Kirchhoff1415
      Kirchhoff1415 over 6 years
      yes, I did request
    • nhoxbypass
      nhoxbypass over 6 years
    • Kirchhoff1415
      Kirchhoff1415 over 6 years
      To be honest i have a problem with understanding the code from that topic. Im totaly new to Android studio.
    • Kirchhoff1415
      Kirchhoff1415 over 6 years
      im using Set<BluetoothDevice> pairedDevice = bluetoothAdapter.getBondedDevices(); to get all already bonded Devices, then just compare theirs names to find the one im willing to connect to. But haveing a problem with creating same list for unbonded devices.
  • Kirchhoff1415
    Kirchhoff1415 over 6 years
    man, much thanks, seems legit. But having a problem with BroadcastReciever. Android Studio says i cannot resolve a symbol 'BroadcastReciever'. Im searching on google for answer but cant find one :(
  • nhoxbypass
    nhoxbypass over 6 years
    @Kirchhoff1415 what problem?
  • Kirchhoff1415
    Kirchhoff1415 over 6 years
    ok, got it, i was missing import android.content.BroadcastReceiver; part
  • nhoxbypass
    nhoxbypass over 6 years
    So did you suceed?
  • Kirchhoff1415
    Kirchhoff1415 over 6 years
    ok, im having a problem with "created bond" function. im getting Cannot resolve symbol with 'Method' and with 'invoke' i've already coppied all imports from stackoverflow.com/questions/14228289/… but it didnt help :(
  • nhoxbypass
    nhoxbypass over 6 years
    This may help import java.lang.reflect.Method; @Kirchhoff1415
  • nhoxbypass
    nhoxbypass over 6 years
    @Kirchhoff1415 Good to hear that, if it work please mark my answer accepted for others to read.
  • nhoxbypass
    nhoxbypass almost 5 years
    @Kharisma BluetoothDemo is your activity. See Context#registerReceiver
  • Kharisma
    Kharisma almost 5 years
    where can I got this bluetoothAdapter.startDiscovery();