gpt4 book ai didi

android - 如何将 Android BLE BluetoothGattCallback 设置为具有状态 BluetoothProfile.STATE_CONNECTING?

转载 作者:行者123 更新时间:2023-11-29 01:39:40 26 4
gpt4 key购买 nike

我正在为一个开源计步器编写一个 BLE 应用程序,到目前为止它运行良好,但有一个恼人的问题:在 BLE 服务的 BluetoothGattCallback void 方法“onConnectionStateChange”中,参数“int newState”只能是两个值之一,STATE_DISCONNECTED 或 STATE_CONNECTED”,如此处所述:

BluetoothGattCallback docs

问题是当我断开连接并重新尝试连接到我的 BLE 设备时,它工作正常,但当它处于连接状态时我没有任何反馈。屏幕保持静止并从断开连接跳转到连接,这可能需要 3 秒到 15 秒。

因此,我的问题是,我能否直接访问 BluetoothGattCallback 的 onConnectionStateChange 方法并在其中传递“BluetoothProfile.STATE_CONNECTING”的值,以便执行状态“STATE_CONNECTING”的“else if”语句中的代码行?如果是,怎么办?

我已经附加了我的 onConnectionStateChange 和连接方法。它们与开发者网站上提供的示例心率监测应用程序中提供的内容基本相同。我唯一的变化是 STATE_CONNECTING 的“else if”。

谢谢。

        @Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
String intentAction;
if (newState == BluetoothProfile.STATE_CONNECTED) {
intentAction = ACTION_GATT_CONNECTED;
mConnectionState = STATE_CONNECTED;
broadcastUpdate(intentAction);
Log.i(TAG, "Connected to GATT server.");
// Attempts to discover services after successful connection.
Log.i(TAG, "Attempting to start service discovery:" +
mBluetoothGatt.discoverServices());
}
else if (newState == BluetoothProfile.STATE_CONNECTING) {
intentAction = ACTION_GATT_CONNECTING;
mConnectionState = STATE_CONNECTING;
Log.i(TAG, "Attempting to connect to GATT server...");
broadcastUpdate(intentAction);
}
else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
intentAction = ACTION_GATT_DISCONNECTED;
mConnectionState = STATE_DISCONNECTED;
Log.i(TAG, "Disconnected from GATT server.");
broadcastUpdate(intentAction);
}

}

public boolean connect(final String address) {
if (mBluetoothAdapter == null || address == null) {
Log.w(TAG, "BluetoothAdapter not initialized or unspecified address.");
return false;
}

// Previously connected device. Try to reconnect.
if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress)
&& mBluetoothGatt != null) {
Log.i(TAG, "Trying to use an existing mBluetoothGatt for connection.");
if (mBluetoothGatt.connect()) {
mConnectionState = STATE_CONNECTING;
return true;
} else {
return false;
}
}

final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
if (device == null) {
Log.w(TAG, "Device not found. Unable to connect.");
return false;
}
// We want to directly connect to the device, so we are setting the autoConnect
// parameter to false.
mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
Log.i(TAG, "Trying to create a new connection.");
mBluetoothDeviceAddress = address;
mConnectionState = STATE_CONNECTING;
return true;
}

最佳答案

原来我需要做的就是自己调用 Gatt 回调并创建一个私有(private)静态 int 以传递给回调方法。

mGattCallback.onConnectionStateChange(mBluetoothGatt, GATT_INDETERMINATE, STATE_CONNECTING);

GATT_INDETERMINATE 在哪里

private static int GATT_INDETERMINATE = 8;

我确保 BluetoothGattCallback 类没有使用 8,因此 8 是一个很好的值。最后,在 onConnectionStateChanged 中,我执行以下操作并广播带有标识操作字符串的适当 Intent 。

    else if (newState == BluetoothProfile.STATE_CONNECTING) {
intentAction = ACTION_GATT_CONNECTING;
mConnectionState = STATE_CONNECTING;
Log.i(TAG, "Attempting to connect to GATT server...");
broadcastUpdate(intentAction);
}

关于android - 如何将 Android BLE BluetoothGattCallback 设置为具有状态 BluetoothProfile.STATE_CONNECTING?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25569278/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com