8

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: ");
        }
    };
Tanveer Munir
  • 1,936
  • 1
  • 10
  • 25
Vadim Eksler
  • 818
  • 9
  • 22
  • 1
    you get on scan stoped bcz timeout expired, `onScanStopped() Called when the scan is stopped (normally because the timeout expired).` try to increase time out sec – Ashvin solanki Apr 05 '19 at 10:56
  • 1
    In order to use BluetoothLeScanner, you must request the user's permission by declaring either the ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission in your app's manifest file. Without these permissions, scans won't return any results. Also you need to add this to your app manifest: '' – Hadas Apr 10 '19 at 14:03
  • @Hadas yep it is there, in manifest – Vadim Eksler Apr 11 '19 at 07:20
  • 1
    Is onDeviceFound being called? (does your app finds other devices than your mi band 2?) – Hadas Apr 11 '19 at 10:29
  • 1
    Another thought - Many ble devices can have only one connection. So the scanning for the "mi band 2" might fell because it was already connected to a different app/device. – Hadas Apr 11 '19 at 13:17
  • @Hadas no, onDeviceFound not called. About another devices I dont know. About one connection also know. – Vadim Eksler Apr 14 '19 at 05:39
  • This is very odd. If I were you I would try implementing from this documentary: https://developers.google.com/android/reference/com/google/android/gms/fitness/Fitness And if it doesn't work I'd try to make a simple ble connection by this documentary: https://developer.android.com/guide/topics/connectivity/bluetooth-le – Hadas Apr 14 '19 at 06:30
  • @Hadas yep strange... other requests to fitness return results and work as I expect, another thing that I can see my device via manual from second link, but this connection not provide data like fitness. – Vadim Eksler Apr 14 '19 at 06:51

1 Answers1

2

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

Andrea Scalabrini
  • 1,232
  • 2
  • 17
  • 22
  • 1
    same result even if you try to reduce the time? Anyway, it's weird. In the official documentation, timeout is set to 1000. https://developers.google.com/fit/android/ble-sensors?hl=en – Andrea Scalabrini Apr 08 '19 at 06:51
  • That's why I ask this question, I read the docs couple of times and also I tried to check if I see the ble sensor via bluetooth api, and i dont see the reason why fitness does not create the connection... – Vadim Eksler Apr 08 '19 at 06:57