如何解决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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。