java.lang.Object | ||
↳ | com.google.android.gms.common.api.GoogleApi<com.google.android.gms.fitness.FitnessOptions> | |
↳ | com.google.android.gms.fitness.BleClient |
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 extension = FitnessOptions.builder() .addDataType(DataTypes.TYPE_STEP_COUNT_DELTA, FitnessOptions.READ) .build() GoogleSignInOptions signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .addExtension(extension) .build(); Task<GoogleSignInAccount> task = GoogleSignIn.getClient(this, signInOptions) .silentSignIn(); GoogleSignInAccount googleSigninAccount = Tasks.await(task); Task<Void> response = Fitness.getBleClient(this, googleSigninAccount).startBleScan( new StartBleScanRequest.Builder() .setDataTypes(DataTypes.TYPE_STEP_COUNT_DELTA) .setBleScanCallback(bleScanCallback) .build());
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 Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Associates a BLE device with the current user, using only the device address.
| |||||||||||
Associates a BLE device with the current user.
| |||||||||||
Lists all BLE devices that are associated with the current user.
| |||||||||||
Starts a scan for BLE devices compatible with Google Fit.
| |||||||||||
Stops a BLE devices scan.
| |||||||||||
Disassociates a BLE device with the current user, using the device's address.
| |||||||||||
Disassociates a BLE device with the current user.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() |
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 |
String : the hardware address of the device. A scan will be performed to find a
matching device. |
Returns | |
---|---|
Task<Void> |
a task , containing if the claim was made successfully
|
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 |
BleDevice : the device to claim |
Returns | |
---|---|
Task<Void> |
a task , containing if the claim was made successfully
|
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.
Parameters | |
---|---|
mDataTypes |
List |
timeoutSecs |
int |
callback |
BleScanCallback |
Returns | |
---|---|
Task<Void> |
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 |
BleScanCallback : the callback originally used to start the scan |
Returns | |
---|---|
Task<Boolean> |
a task , containing if the scan was stopped successfully
|
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 |
String : the hardware address of the device |
Returns | |
---|---|
Task<Void> |
a task containing if the unclaim was made successfully
|
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 |
BleDevice : the device to unclaim |
Returns | |
---|---|
Task<Void> |
a task , containing if the unclaim was made successfully
|