public interface

# BleApi

com.google.android.gms.fitness.BleApi

## Class Overview

API for scanning, claiming, and using Bluetooth Low Energy 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.BLE_API)
             ...
             .build();
         client.connect();

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

## Enabling Bluetooth

Many methods in this class require Bluetooth to be enabled. In case it isn't, the method will return a result with status code set to `DISABLED_BLUETOOTH`. In this case, the app should use `startResolutionForResult(Activity, int)` to show the dialog allowing the user to enable Bluetooth.

Example:

         public class MyActivity extends FragmentActivity {

             private static final int REQUEST_BLUETOOTH = 1001;

             private GoogleApiClient mGoogleApiClient;
             ...

             private final ResultCallback mResultCallback = new ResultCallback() {
                 @Override
                 public void onResult(Status status) {
                     ...
                     if (!status.isSuccess()) {
                         switch (status.getStatusCode()) {
                             case FitnessStatusCodes.DISABLED_BLUETOOTH:
                                 try {
                                     status.startResolutionForResult(
                                             MyActivity.this, REQUEST_BLUETOOTH);
                                 } catch (SendIntentException e) {
                                     ...
                                 }
                                 break;
                             ...
                         }
                     }
                 }
             };

             @Override
             protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                 super.onActivityResult(requestCode, resultCode, data);
                 switch (requestCode) {
                     case REQUEST_BLUETOOTH:
                         startBleScan();
                         break;
                     ...
                 }
             }

             private void startBleScan() {
                 StartBleScanRequest request = ...
                 PendingResult result =
                         Fitness.BleApi.startBleScan(mGoogleApiClient, request);
                 result.setResultCallback(mResultCallback);
             }
         }

## Summary

Public Methods
abstract PendingResult<Status> | claimBleDevice(GoogleApiClient client, String deviceAddress) Associates a BLE device with the current user, using only the device address.
abstract PendingResult<Status> | claimBleDevice(GoogleApiClient client, BleDevice bleDevice) Associates a BLE device with the current user.
abstract PendingResult<BleDevicesResult> | listClaimedBleDevices(GoogleApiClient client) Lists all BLE devices that are associated with the current 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, String deviceAddress) Disassociates a BLE device with the current user, using the device's address.
abstract PendingResult<Status> | unclaimBleDevice(GoogleApiClient client, BleDevice bleDevice) Disassociates a BLE device with the current user.

## Public Methods

#### public abstract PendingResult<Status> claimBleDevice (GoogleApiClient client, String deviceAddress)

Associates a BLE device with the current user, using only the device address. When a device is claimed by a user, the device will be available through Google Fit. If a full `BleDevice` is available, calling `claimBleDevice(GoogleApiClient, BleDevice)` is preferred.

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.

Since this method requires Bluetooth, please refer to `BleApi` doc about handling disabled Bluetooth.

##### Parameters

client | an existing GoogleApiClient. Must be connected at the time of this call.
deviceAddress | the hardware address of the device. A scan will be performed to find a matching device.

##### Returns

- a pending result containing if the claim was made successfully

#### public abstract PendingResult<Status> claimBleDevice (GoogleApiClient client, BleDevice bleDevice)

Associates a BLE device with the current 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. Must be connected at the time of this call.
bleDevice | the device to claim

##### Returns

- a pending result containing if the claim was made successfully

#### public abstract PendingResult<BleDevicesResult> listClaimedBleDevices (GoogleApiClient client)

Lists all BLE devices that are associated with the current user.

##### Parameters

client | an existing GoogleApiClient. It does not need to be connected at the time of this call, but the find operation will be delayed until the connection is complete.

##### Returns

- a pending result containing found claimed BLE devices.

#### 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, String)` 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.

Since this method requires Bluetooth, please refer to `BleApi` doc about handling disabled Bluetooth.

#### 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.

Since this method requires Bluetooth, please refer to `BleApi` doc about handling disabled Bluetooth.

##### 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, String deviceAddress)

Disassociates a BLE device with the current user, using the device's address. The device's associated `DataSources` will no longer be available in Google Fit, and all of the registrations for this device will be removed. .

##### Parameters

client | an existing GoogleApiClient. Must be connected at the time of this call.
deviceAddress | the hardware address of the device

##### Returns

- a pending result containing if the unclaim was made successfully

#### public abstract PendingResult<Status> unclaimBleDevice (GoogleApiClient client, BleDevice bleDevice)

Disassociates a BLE device with the current user. The device's associated `DataSources` will no longer be available in Google Fit, and all of the registrations for this device will be removed. .

##### Parameters

client | an existing GoogleApiClient. Must be connected at the time of this call.
bleDevice | the device to unclaim

##### Returns

- a pending result containing if the unclaim was made successfully