0

I want to connect Mi Band 3 with my application through Google Fit SDK. I am facing issue to connect band through bluetooh. as given in Offical Google Fit Documentation - BLE Sensor I am using following code in Kotlin

 var bleScanCallbacks: BleScanCallback = object : BleScanCallback() {
        override fun onDeviceFound(device: BleDevice?) {
            // A device that provides the requested data types is available
        }

        override fun onScanStopped() {
            // The scan timed out or was interrupted
        }
    }

    var response: Task<Void> = Fitness.getBleClient(
        this,
        GoogleSignIn.getLastSignedInAccount(this)!!
    )
        .startBleScan(
            Arrays.asList(DataType.TYPE_STEP_COUNT_DELTA),
            1000, bleScanCallbacks
        )

but it showing that

'getBleClient(Activity, GoogleSignInAccount): BleClient!' is deprecated. Deprecated in Java

and

'BleScanCallback' is deprecated. Deprecated in Java

It suggesting to use BluetoothManager. I am beginner in android development. Java solution will also work.

James Z
  • 12,209
  • 10
  • 24
  • 44
ameya
  • 201
  • 2
  • 16
  • If you are targeting Android 8.0+, you should consider using Companion Device Pairing API to pair your device. For more infos: https://developer.android.com/guide/topics/connectivity/companion-device-pairing – matdev Sep 11 '20 at 13:44
  • Thanks @matdev, but at this moment I connected my band directly but facing issue to connect it with Google Fit, but this will help me in upcoming modules. – ameya Sep 11 '20 at 13:49
  • well a method being deprecated does not mean it does not work. What is your issue apart from this message ? – matdev Sep 11 '20 at 15:28
  • I tried using that, but I am not getting accurate data as well as I required data frequently, but it is read data from sensors only once. – ameya Sep 11 '20 at 17:11
  • Does your app actually need to connect to the Mi Band to read the info you need or scanning its advertisement data is enough ? If advertisement data are enough, have you tried scanning continuously ? – matdev Sep 14 '20 at 12:55
  • Yes yes, I want to connect to mi band. I am from python background so I am beginner in app development. Can you suggest how I can proceed? – ameya Sep 14 '20 at 13:02

1 Answers1

0

After the BLE scanner has found your device, you can connect to its GATT server.

To connect to a GATT server on a BLE device, you use the connectGatt() method. This method takes three parameters: a Context object, autoConnect (boolean indicating whether to automatically connect to the BLE device as soon as it becomes available), and a reference to a BluetoothGattCallback:

BluetoothGatt bluetoothGatt = device.connectGatt(this, false, gattCallback);

This connects to the GATT server hosted by the BLE device, and returns a BluetoothGatt instance, which you can then use to conduct GATT client operations such as discovering services, reading and writing on BLE characteristics.

For a BluetoothGattCallback sample code and other infos: https://developer.android.com/guide/topics/connectivity/bluetooth-le

matdev
  • 4,115
  • 6
  • 35
  • 56