I want to request sensor data from my mi band 2 using my android app in real time. I have some difficulties with this. I use permissions BLUETOOTH
and BLUETOOTH_ADMIN
. I checked that I can see my device via Bluetooth le default API.
I am trying to use this example https://developers.google.com/fit/android/ble-sensors?hl=ru and all the time I get onScanStopped and this callback don't have some explanation, so I don't understand why it fails.
My code:
GoogleApiClient client = new GoogleApiClient.Builder(this)
.addApi(Fitness.SENSORS_API)
.addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ))
.addScope(new Scope(Scopes.FITNESS_BODY_READ))
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
client.connect();
And onConnected I have:
Fitness.getBleClient(this, GoogleSignIn.getLastSignedInAccount(this))
.startBleScan(Arrays.asList(DataType.TYPE_ACTIVITY_SEGMENT), 60, bleScanCallbacks)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Log.d("TAG_F", "onComplete: " + task.isSuccessful());
}
});
Here I also tried all of this data types DataType.TYPE_STEP_COUNT_DELTA, DataType.TYPE_HEART_RATE_BPM
This shows me that my scan is successful. But on callback after 60 seconds I get onScanStopped
:
private BleScanCallback bleScanCallbacks = new BleScanCallback() {
@Override
public void onDeviceFound(BleDevice bleDevice) {
Log.d("TAG_F", "onDeviceFound: " + bleDevice.getDataTypes());
}
@Override
public void onScanStopped() {
Log.d("TAG_F", "onScanStopped: ");
}
};
BLE Scanner was developed with a vision to help Bluetooth community, developers who wants to build BLE products & applications. BLE Scanner is used by not only developers but also users are using it to find their lost Fitness Trackers and other Bluetooth Smart Devices.
May 20, 2021 at 15:07. raw data includes everything advertised, but in your case, only the manufacturer data are available, type 0xFF.
To find BLE devices, you use the startScan() method. This method takes a ScanCallback as a parameter. You must implement this callback, because that is how scan results are returned.
onScanStopped()
is called when the timeout specified in
startBleScan(List<DataType> dataTypes, int timeoutSecs, BleScanCallback callback)
expired.
Try to increase/decrease the time (60') specified in your method:
Fitness.getBleClient(this, GoogleSignIn.getLastSignedInAccount(this))
.startBleScan(Arrays.asList(DataType.TYPE_ACTIVITY_SEGMENT), 60, bleScanCallbacks)
Android Documentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With