0

I have two Android phones. I want to make an auto connection between them via Bluetooth. For example,

I have my android phone which paired to another Bluetooth. When I put these phone near together, they need to detect Bluetooth device, and automatically connect to a selected android phone (known Adress/MAC/Paired before). I don't need to connect it again. I want this kind of connectivity in my Android application.

I google and found some related reference, but they are did not solve the issue yet. I think that I need to create a thread/service to automatically connect Bluetooth when they are in range. However, I can not implement it. If you have a good solution, please let me know. Thank you

Automatically connect to paired bluetooth device when in range

Find already paired bluetooth devices automatically, when they are in range

   /**
     * The BroadcastReceiver that listens for discovered devices and changes the title when
     * discovery is finished
     */
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        // When discovery finds a device
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            // If it's already paired, skip it, because it's been listed already
            if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
                mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
            }
            // When discovery is finished, change the Activity title
        } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            setProgressBarIndeterminateVisibility(false);
            setTitle(R.string.select_device);
            if (mNewDevicesArrayAdapter.getCount() == 0) {
                String noDevices = getResources().getText(R.string.none_found).toString();
                mNewDevicesArrayAdapter.add(noDevices);
            }
        }
    }
};
Community
  • 1
  • 1
Jame
  • 3,746
  • 6
  • 52
  • 101

1 Answers1

0

These broadcast receivers will fire only after the discovery process runs.

Discovery can be started in two ways: either manually in Bluetooth settings, or programatically by calling BluetoothAdapter#startDiscovery(). However, the documentation states that:

Device discovery is a heavyweight procedure. New connections to remote Bluetooth devices should not be attempted while discovery is in progress, and existing connections will experience limited bandwidth and high latency. Use cancelDiscovery() to cancel an ongoing discovery. Discovery is not managed by the Activity, but is run as a system service, so an application should always call cancelDiscovery() even if it did not directly request a discovery, just to be sure.

This implies that discovery should be done as a one-off procedure, not continuously running in the background—apart from slowing down other Bluetooth connections, it will drain the battery quickly.

user149408
  • 5,385
  • 4
  • 33
  • 69