com.google.android.gms.fitness.BleApi |
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());In case Bluetooth is required (for example for
startBleScan(GoogleApiClient, StartBleScanRequest)
), but is disabled, Google Fit will send back a result with status code set toDISABLED_BLUETOOTH
. In this case, the app should usestartResolutionForResult(Activity, int)
to show the dialog allowing the user to enable Bluetooth.Example of handling disabled Bluetooth:
public class MyActivity extends FragmentActivity { private static final int REQUEST_BLUETOOTH = 1001; private GoogleApiClient mGoogleApiClient; ... private final ResultCallbackmResultCallback = 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); } }
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Associates a BLE device with a user.
| |||||||||||
Associates a BLE device with a 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 a user.
| |||||||||||
Disassociates a BLE device with a user.
|
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.
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. |
---|---|
bleDevice | the device to claim |
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.
Since this method requires Bluetooth, please refer to BleApi
doc about handling
disabled Bluetooth.
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. |
---|---|
deviceAddress | the hardware address of the device. A scan will be performed to find a matching device. |
Lists all BLE devices that are associated with the current user.
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. |
---|
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.
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.
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 |
Disassociates a BLE device with a user. The device's associated
DataType
s 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, BleDevice)
.
.
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. |
---|---|
deviceAddress | the hardware address of the device |
Disassociates a BLE device with a user. The device's associated
DataType
s 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, BleDevice)
.
.
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. |
---|---|
bleDevice | the device to unclaim |