BleClient

public class BleClient extends GoogleApi<FitnessOptions>

Client 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 client supports scanning and claiming devices. Once a device is claimed, its data sources are exposed via the Sensors and Recording Clients, similar to local sensors.

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


    GoogleSignInOptionsExtension fitnessOptions =
      FitnessOptions.builder()
          .addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
          .build();

    GoogleSignInAccount gsa = GoogleSignIn.getAccountForExtension(this, fitnessOptions);

    Task<Void> response = Fitness.getBleClient(this, gsa).startBleScan(
        new StartBleScanRequest.Builder()
            .setDataTypes(DataType.TYPE_STEP_COUNT_DELTA)
            .setBleScanCallback(bleScanCallback)
            .build());
 

Enabling Bluetooth

Many methods in this class require Bluetooth to be enabled. In case it isn't, any registered OnFailureListener will be invoked with ResolvableApiException, 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 final OnFailureListener mFailureListener =
            new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception exception) {
                    if (exception instanceof ResolvableApiException) {
                        ResolvableApiException apiException = (ResolvableApiException) exception;
                        int statusCode = apiException.getStatusCode();
                        if (FitnessStatusCodes.DISABLED_BLUETOOTH == statusCode) {
                            try {
                                apiException.startResolutionForResult(
                                        MyActivity.this, REQUEST_BLUETOOTH);
                             } catch (IntentSender.SendIntentException e) {
                                ...
                             }
                        }
                    }
                }
            };

        @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() {
            GoogleSignInAccount googleSigninAccount = ...
            List<DataType> dataTypes = ...
            int timeoutSecs = ...
            BleScanCallback callback = ...
            Task<Void> response = Fitness
                                          .getBleClient(this, googleSigninAccount)
                                          .startBleScan(dataTypes, timeoutSecs, callback);
            response.addOnFailureListener(mFailureListener);
        }
    }
 

Public Method Summary

Task<Void>
claimBleDevice(String deviceAddress)
Associates a BLE device with the current user, using only the device address.
Task<Void>
claimBleDevice(BleDevice bleDevice)
Associates a BLE device with the current user.
Task<List<BleDevice>>
listClaimedBleDevices()
Lists all BLE devices that are associated with the current user.
Task<Void>
startBleScan(List<DataType> dataTypes, int timeoutSecs, BleScanCallback callback)
Starts a scan for BLE devices compatible with Google Fit.
Task<Boolean>
stopBleScan(BleScanCallback callback)
Stops a BLE devices scan.
Task<Void>
unclaimBleDevice(String deviceAddress)
Disassociates a BLE device with the current user, using the device's address.
Task<Void>
unclaimBleDevice(BleDevice bleDevice)
Disassociates a BLE device with the current user.

Inherited Method Summary

Public Methods

public Task<Void> claimBleDevice (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(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 BleClient doc about handling disabled Bluetooth.

Parameters
deviceAddress the hardware address of the device. A scan will be performed to find a matching device.
Returns
  • a task, containing if the claim was made successfully

public Task<Void> claimBleDevice (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
bleDevice the device to claim
Returns
  • a task, containing if the claim was made successfully

public Task<List<BleDevice>> listClaimedBleDevices ()

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

Returns
  • a task, containing found claimed BLE devices.

public Task<Void> startBleScan (List<DataType> dataTypes, int timeoutSecs, BleScanCallback callback)

Starts a scan for BLE devices compatible with Google Fit. Results are returned asynchronously through the BleScanCallback. 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(BleScanCallback), and claimBleDevice(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 BleClient doc about handling disabled Bluetooth.

public Task<Boolean> stopBleScan (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 BleClient doc about handling disabled Bluetooth.

Parameters
callback the callback originally used to start the scan
Returns
  • a task, containing if the scan was stopped successfully

public Task<Void> unclaimBleDevice (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
deviceAddress the hardware address of the device
Returns
  • a task containing if the unclaim was made successfully

public Task<Void> unclaimBleDevice (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
bleDevice the device to unclaim
Returns
  • a task, containing if the unclaim was made successfully