com.google.android.gms.fitness.BleApi |
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());
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 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 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.
|
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 |
GoogleApiClient : an existing GoogleApiClient. Must be connected at the time of this call. |
deviceAddress |
String : the hardware address of the device. A scan will be performed to find a
matching device. |
Returns | |
---|---|
PendingResult<Status> |
a pending result 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 | |
---|---|
client |
GoogleApiClient : an existing GoogleApiClient. Must be connected at the time of this call. |
bleDevice |
BleDevice : the device to claim |
Returns | |
---|---|
PendingResult<Status> |
a pending result containing if the claim was made successfully |
Lists all BLE devices that are associated with the current user.
Parameters | |
---|---|
client |
GoogleApiClient : 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 | |
---|---|
PendingResult<BleDevicesResult> |
a pending result containing found claimed BLE devices. |
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.
Parameters | |
---|---|
client |
GoogleApiClient |
request |
StartBleScanRequest |
Returns | |
---|---|
PendingResult<Status> |
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 |
GoogleApiClient : 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 |
BleScanCallback : the callback originally used to start the scan |
Returns | |
---|---|
PendingResult<Status> |
a pending result 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 | |
---|---|
client |
GoogleApiClient : an existing GoogleApiClient. Must be connected at the time of this call. |
deviceAddress |
String : the hardware address of the device |
Returns | |
---|---|
PendingResult<Status> |
a pending result 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 | |
---|---|
client |
GoogleApiClient : an existing GoogleApiClient. Must be connected at the time of this call. |
bleDevice |
BleDevice : the device to unclaim |
Returns | |
---|---|
PendingResult<Status> |
a pending result containing if the unclaim was made successfully |