安卓BLE onConnectionUpdated()

6
我目前正在尝试确定针对API27的Android应用程序中的当前BLE连接间隔。所有我找到的文档(包括许多SO问题)都说这是不可能的,但是,在调试模式下运行我的应用程序时,会出现以下控制台消息:

D/BluetoothGatt: onConnectionUpdated() - Device=XX:XX:XX:XX:XX:XX interval=9 latency=0 timeout=600 status=0

不幸的是,我在docs中找不到此回调。我是否正确地认为此回调未公开?如果是这样,那么有没有任何方法可以访问当前的连接间隔?谢谢。
1个回答

9
以下是 onConnectionUpdated 的源代码:
            /**
             * Callback invoked when the given connection is updated
             * @hide
             */
            @Override
            public void onConnectionUpdated(String address, int interval, int latency,
                    int timeout, int status) {
                if (DBG) {
                    Log.d(TAG, "onConnectionUpdated() - Device=" + address
                            + " interval=" + interval + " latency=" + latency
                            + " timeout=" + timeout + " status=" + status);
                }
                if (!address.equals(mDevice.getAddress())) {
                    return;
                }
                runOrQueueCallback(new Runnable() {
                    @Override
                    public void run() {
                        final BluetoothGattCallback callback = mCallback;
                        if (callback != null) {
                            callback.onConnectionUpdated(BluetoothGatt.this, interval, latency,
                                    timeout, status);
                        }
                    }
                });
            }

您可以在 BluetoothGatt.java中找到完整的源代码。


此外,服务器上的源代码也可以参考。

 /**
 * Callback indicating the connection parameters were updated.
 *
 * @param device The remote device involved
 * @param interval Connection interval used on this connection, 1.25ms unit. Valid range is from
 * 6 (7.5ms) to 3200 (4000ms).
 * @param latency Slave latency for the connection in number of connection events. Valid range
 * is from 0 to 499
 * @param timeout Supervision timeout for this connection, in 10ms unit. Valid range is from 10
 * (0.1s) to 3200 (32s)
 * @param status {@link BluetoothGatt#GATT_SUCCESS} if the connection has been updated
 * successfully
 * @hide
 */
public void onConnectionUpdated(BluetoothDevice device, int interval, int latency, int timeout,
        int status) {
}

您可以在BluetoothGattServerCallback.java中找到此内容。


只是一个进一步的问题:在Android Studio中,我正在使用Kotlin编程。 尝试覆盖函数时,即使我显式导入了android.bluetooth.BluetoothGattonConnectionUpdated()也无法被识别为回调函数。 我该如何解决这个问题? - amitchone
4
你找不到它的原因是该函数带有@Hide标注,使其对外部类隐藏,唯一的方法是使用反射。 - Annerajb
@Annerajb 好的,我们的代码中无法处理 onConnectionUpdate。 - Xengo
1.25毫秒单位?谁想出来的?=)噢,那就解释了为什么我看到的数字与预期不同。 - Steven Jeuris

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接