Use Bluetooth Sensors

Google Fit includes support for Bluetooth Low Energy sensors that implement a standard GATT profile. The Bluetooth Low Energy API enables your app to look for available BLE devices and to use a BLE device with the Recording API and the Sensors API.

Scan for available devices

To scan for nearby BLE devices, use the BleApi.startBleScan method:

// 1. Define a callback object
BleScanCallback callback = new BleScanCallback() {
    @Override
    void onDeviceFound(BleDevice device) {
        // A device that provides the requested data types is available
        // -> Claim this BLE device (See next example)
    }
    @Override
    void onScanStopped() {
        // The scan timed out or was interrupted
    }
};

// 2. Create a scan request object:
// - Specify the data types you're interested in
// - Provide the callback object
StartBleScanRequest request = new StartBleScanRequest.Builder()
    .setDataTypes(DataType.TYPE_HEART_RATE_BPM)
    .setBleScanCallback(callback)
    .build();

// 3. Invoke the Bluetooth Low Energy API with:
// - The Google API client
// - The scan request
PendingResult<Status> pendingResult =
    Fitness.BleApi.startBleScan(mClient, request);

// 4. Check the result (see other examples)

Claim a device

To make a BLE device available to the Recording API and the Sensors API, use the BleApi.claimBleDevice method:

// After the platform invokes your callback
// with a compatible BLE device (bleDevice):

// 1. Invoke the Bluetooth Low Energy API with:
// - The Google API client
// - The BleDevice object provided in the callback
PendingResult<Status> pendingResult =
    Fitness.BleApi.claimBleDevice(mClient, bleDevice);

// 2. Check the result (see other examples)

If the BLE device is claimed successfully, you can access it like any other sensor using the Recording API and the Sensors API. For example, you can obtain a data source object for the BLE device with the SensorsApi.findDataSources method.

Release a device

To stop using a BLE device with Google Fit, use the BleApi.unclaimBleDevice method:

// When you no longer need the BLE device

// 1. Invoke the Bluetooth Low Energy API with:
// - The Google API client
// - The BLE device (from the initial scan)
PendingResult<Status> pendingResult =
    Fitness.BleApi.unclaimBleDevice(mClient, bleDevice);

// 2. Check the result (see other examples)

If the request is successful, the BLE device no longer appears as an available sensor in Google Fit.

Send feedback about...