Google Fit API BLE连接问题

问题描述

我想通过Google Fit SDK将Mi Band 3与我的应用程序连接。我面临通过蓝牙连接乐队的问题。如Offical Google Fit Documentation - BLE Sensor中所述,我在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
        )

但它表明了

“ getBleClient(活动,GoogleSignInAccount):BleClient!”不推荐使用。在Java中已弃用

'BleScanCallback'已过时。在Java中已弃用

建议使用BluetoothManager。我是android开发的初学者。 Java解决方案也可以。

解决方法

BLE扫描仪找到您的设备后,您可以连接到其GATT服务器。

要连接到BLE设备上的GATT服务器,请使用connectGatt()方法。此方法采用三个参数:Context对象,autoConnect(布尔值,指示是否在可用时尽快自动连接到BLE设备),以及对BluetoothGattCallback的引用: / p>

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

这将连接到由BLE设备托管的GATT服务器,并返回BluetoothGatt实例,然后您可以使用该实例执行GATT客户端操作,例如发现服务,读取和写入BLE特性。

对于BluetoothGattCallback示例代码和其他信息: https://developer.android.com/guide/topics/connectivity/bluetooth-le

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...