Android APIs
public interface

BleApi

com.google.android.gms.fitness.BleApi

Class Overview

The Google Fit Bluetooth Low Energy API provides methods for scanning, claiming, and using Bluetooth smart devices in Google Fit.

Most BLE devices will accept connections from any other device, without the need for pairing. To prevent Google Fit from using data from a device the user does not own, we require a device to be claimed before it can be used in the platform.

The API supports scanning and claiming devices. Once a device is claimed, its data sources are exposed via the Sensors and Recording APIs, similar to local sensors.

The BLE API should be accessed from the Fitness entry point. Example:

     GoogleApiClient client = new GoogleApiClient.Builder(context)
         .addApi(Fitness.API)
         ...
         .build();
     client.connect();

     PendingResult<Status> pendingResult = Fitness.BleApi.startBleScan(
         client,
         new StartBleScanRequest.Builder()
             .setDataTypes(DataTypes.STEP_COUNT_DELTA)
             .setBleScanCallback(bleScanCallback)
             .build());

Summary

Public Methods
abstract PendingResult<Status> claimBleDevice(GoogleApiClient client, ClaimBleDeviceRequest request)
Associates a BLE device with a user.
abstract PendingResult<Status> startBleScan(GoogleApiClient client, StartBleScanRequest request)
Starts a scan for BLE devices compatible with Google Fit.
abstract PendingResult<Status> stopBleScan(GoogleApiClient client, BleScanCallback callback)
Stops a BLE devices scan.
abstract PendingResult<Status> unclaimBleDevice(GoogleApiClient client, UnclaimBleDeviceRequest request)
Disassociates a BLE device with a user.

Public Methods

public abstract PendingResult<Status> claimBleDevice (GoogleApiClient client, ClaimBleDeviceRequest request)

Associates a BLE device with a user. When a device is claimed by a user, the device will be available through Google Fit.

Prior to calling this method, you should stop any active Bluetooth scans you have started. In order to prevent Bluetooth issues, the application should avoid connecting directly to the device, but instead using Google Fit to do so.

Parameters
client an existing GoogleApiClient. It does not need to be connected at the time of this call, but the claim operation will be delayed until the connection is complete.
request a built request specifying the device in which to claim
Returns
  • a pending result containing if the claim was made successfully

public abstract PendingResult<Status> startBleScan (GoogleApiClient client, StartBleScanRequest request)

Starts a scan for BLE devices compatible with Google Fit. Results are returned asynchronously through the BleScanCallback in the request. The callback's onDeviceFound(BleDevice) method may be called multiple times, for each device that is found.

This method will normally be used to present a list of devices to the user, and to allow the user to pick a device to claim. Once the user selects a device or dismisses the picker activity, the scan can be stopped using stopBleScan(GoogleApiClient, BleScanCallback), and claimBleDevice(GoogleApiClient, ClaimBleDeviceRequest) can be used to associate the selected device with the user.

This scanning is battery-intensive, so try to minimize the amount of time scanning.

Parameters
client an existing GoogleApiClient. It does not need to be connected at the time of this call, but the scan operation will be delayed until the connection is complete.
request a built request specifying the data sources we’re interested in finding and a callback for discovered devices
Returns
  • a pending result containing if the scan was started successfully

public abstract PendingResult<Status> stopBleScan (GoogleApiClient client, BleScanCallback callback)

Stops a BLE devices scan. Should be called immediately after scanning is no longer needed.

If the scan is already stopped, or if it was never started, this method will succeed silently.

Parameters
client an existing GoogleApiClient. It does not need to be connected at the time of this call, but the stop scan operation will be delayed until the connection is complete.
callback the callback originally used to start the scan
Returns
  • a pending result containing if the scan was stopped successfully

public abstract PendingResult<Status> unclaimBleDevice (GoogleApiClient client, UnclaimBleDeviceRequest request)

Disassociates a BLE device with a user. The devices associated DataTypes will no longer be available exposed in Google Fit.

All of the registrations for this device will be removed.

This should be called after first calling claimBleDevice(GoogleApiClient, ClaimBleDeviceRequest). .

Parameters
client an existing GoogleApiClient. It does not need to be connected at the time of this call, but the unclaim operation will be delayed until the connection is complete.
request a built request specifying the device in which to unclaim
Returns
  • a pending result containing if the unclaim was made successfully