How to connect two Android devices via Bluetooth Low Energy

Minh Ngo
2 min readApr 26, 2022

If you want to connect two Android devices via Bluetooth Low Energy one device has to be the scanner and one the advertiser.

Check capabilities first

First we need to check if our device supports Bluetooth at all, if Bluetooth is turned on and if it can be an advertiser.

If everything is ok we can setup the advertiser.

Setup Bluetooth Low Energy advertiser

You get the bluetoothLeAdvertiser from the BluetoothAdapter and start advertising by calling startAdvertising() on it.

By providing a callback you are notified if advertising succeeds or fails with an error code.

You can stop advertising with stopAdvertising().

Setup Bluetooth Low Energy scanner

Beware that since Android N the permission android.permission.ACCESS_FINE_LOCATION needs to be granted.

When granted you can start scanning by calling startScan() on the bluetoothLeScanner that you get from the BluetoothAdapter.

In the callback that you provide to startScan() you will get the scan results or errors.

Also beware that you should stop scanning in any case after a certain amount of time to not drain the battery of the device.

Optimizations

Maybe you noticed that the startAdvertising() and startScan() methods provide some additional parameters.

You can use these parameters to optimize the connection process and narrow down which devices are scanned.

When advertising you can provide an advertise mode and a UUID. When scanning you can provide a scan mode and filter for a UUID.

--

--