2

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

BluetoothGattCallback 文档

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

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

我附上了我的 onConnectionStateChange 和 connect 方法。它们与开发人员网站上提供的示例心率监测器应用程序中提供的内容基本没有变化。我唯一的改变是 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;
}
4

1 回答 1

1

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

mGattCallback.onConnectionStateChange(mBluetoothGatt, GATT_INDETERMINATE, STATE_CONNECTING);

GATT_INDETERMINATE 在哪里

private static int GATT_INDETERMINATE = 8;

我确保 BluetoothGattCallback 类没有使用 8,所以 8 是一个很好的使用值。最后在 onConnectionStateChanged 中,我执行以下操作并通过识别操作字符串广播适当的意图。

    else if (newState == BluetoothProfile.STATE_CONNECTING) {
            intentAction = ACTION_GATT_CONNECTING;
            mConnectionState = STATE_CONNECTING;
            Log.i(TAG, "Attempting to connect to GATT server...");
            broadcastUpdate(intentAction);
        }
于 2014-10-27T20:01:26.137 回答