Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fitness startBleScan successful but scan Stopped with MI Band 2

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: ");
        }
    };
like image 972
Vadim Eksler Avatar asked Mar 31 '19 07:03

Vadim Eksler


People also ask

What does ble scanner do?

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.

What does raw data mean on ble scanner?

May 20, 2021 at 15:07. raw data includes everything advertised, but in your case, only the manufacturer data are available, type 0xFF.

How do I scan my Android phone with BLE?

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.


1 Answers

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

like image 62
Andrea Scalabrini Avatar answered Nov 15 '22 03:11

Andrea Scalabrini