2

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
  • 9,695
  • 11
  • 48
  • 71
Kirchhoff1415
  • 41
  • 1
  • 1
  • 7

1 Answers1

5

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.

nhoxbypass
  • 9,695
  • 11
  • 48
  • 71
  • 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 :( – Kirchhoff1415 Oct 20 '17 at 03:15
  • @Kirchhoff1415 what problem? – nhoxbypass Oct 20 '17 at 03:16
  • ok, got it, i was missing import android.content.BroadcastReceiver; part – Kirchhoff1415 Oct 20 '17 at 03:20
  • So did you suceed? – nhoxbypass Oct 20 '17 at 03:44
  • 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 https://stackoverflow.com/questions/14228289/android-pair-devices-via-bluetooth-programmatically but it didnt help :( – Kirchhoff1415 Oct 20 '17 at 04:17
  • This may help `import java.lang.reflect.Method;` @Kirchhoff1415 – nhoxbypass Oct 20 '17 at 04:39
  • @Kirchhoff1415 Good to hear that, if it work please mark my answer accepted for others to read. – nhoxbypass Oct 20 '17 at 06:36
  • @Kharisma `BluetoothDemo` is your activity. See [Context#registerReceiver](https://developer.android.com/reference/android/content/Context#registerReceiver(android.content.BroadcastReceiver,%2520android.content.IntentFilter)) – nhoxbypass Jul 11 '19 at 02:53
  • where can I got this `bluetoothAdapter.startDiscovery();` – Kharisma Jul 11 '19 at 03:05