Exploring Bluetooth Low Energy Communication in Android

Hello everyone, I am an experienced Java tutorial author. Today we will learn how to use Bluetooth Low Energy (BLE) for Android development. BLE is an emerging Bluetooth technology that helps us develop Bluetooth applications with lower power consumption and more stable connections.

BLE is a new low-power Bluetooth technology introduced with Bluetooth 4.0. Compared to classic Bluetooth, the biggest advantage of BLE is its ability to significantly reduce device power consumption, extending battery life from several days to several months or even years. This is crucial for applications like wearable devices that require long operational times.

java copy

// Check if the device supports BLE
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
    // Does not support BLE
}

BLE uses a completely new architecture, mainly consisting of four roles:

  • Central – Typically a device with applications and more processing power, such as a smartphone.
  • Peripheral – Low-power nodes, which can be various sensors like heart rate monitors and thermometers.
  • GATT Server – Exists on peripheral devices to store services and characteristic values.
  • GATT Client – Runs on central devices and connects to peripherals to read and write characteristic values.

The central device scans for nearby peripherals, connects to the desired device, and communicates with it.

java copy

// Enable Bluetooth
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
btAdapter.enable();

// Start scanning for BLE devices
btScanner = btAdapter.getBluetoothLeScanner();
scanSettings = new ScanSettings.Builder()
        .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
        .build();
scanner.startScan(null, scanSettings, scanCallback);

// Handle scan results
private ScanCallback scanCallback = new ScanCallback() {
    @Override
    public void onBatchScanResults(List<ScanResult> results) {
        for (ScanResult result : results) {
            // Handle scanned BLE devices
        }
    }
};

The code above first enables Bluetooth, then starts scanning for BLE devices. We can handle the scan results in the scanCallback’s onBatchScanResults method.

Note that you need to request ACCESS_FINE_LOCATION permission to scan and connect to BLE devices.

After scanning for the desired device, we can connect and communicate with it.

java copy

// Connect to the device
device.connectGatt(this, false, gattCallback);

// Discover device services and characteristics
boolean discoverServices = gatt.discoverServices();

// Read and write characteristic values
BluetoothGattCharacteristic characteristic = 
boolean readValue = gatt.readCharacteristic(characteristic);
boolean writeValue = characteristic.setValue(bytes);
boolean sendValue = gatt.writeCharacteristic(characteristic);

The code above connects to a BLE device, discovers the services and characteristics it offers, and reads and writes a characteristic value.

Tip: To read and write data from a remote device, we need to find the correct service and characteristic UUIDs, which are usually provided by the device manufacturer.

Sometimes we need to continuously retrieve data from a BLE device, such as heart rate data from a heart rate monitor. In this case, we can set up characteristic notifications.

java copy

characteristic.setNotificationCallback(notificationCallback);
boolean enableNotification = gatt.setCharacteristicNotification(characteristic, true);
// Receive notification data
private BluetoothGattCallback notificationCallback = new BluetoothGattCallback() {
    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic c) {
        // Parse data
    }
};

The code above enables notifications for a characteristic, and when the device has new data, a notification is received and processed in notificationCallback.

Note that the data formats from different devices may vary, and we need to refer to the device documentation to correctly parse the data.

Today we learned how to develop BLE applications using Android. I encourage everyone to practice hands-on; you will definitely encounter various problems and difficulties along the way, but don’t be discouraged. Believe in yourself, and you will overcome the challenges. The secret of programming lies in continuous practice and summarization. Happy learning!

()

Leave a Comment