2

私はオープン ソースの歩数計用の BLE アプリを作成していますが、これまでのところ 1 つの厄介な問題でうまく機能しています: BLE サービスの BluetoothGattCallback void メソッド "onConnectionStateChange" では、パラメーター "int newState" は 2 つのうちの 1 つしか指定できません。ここに記載されているように、値、STATE_DISCONNECTED または STATE_CONNECTED」:

BluetoothGattCallback ドキュメント

問題は、BLE デバイスを切断して接続を再試行すると機能するが、接続状態のときにフィードバックがないことです。画面は静的なままで、切断された状態から接続された状態にジャンプします。これには 3 秒から 15 秒かかります。

したがって、私の質問は、BluetoothGattCallback の onConnectionStateChange メソッドに直接アクセスし、その中に「BluetoothProfile.STATE_CONNECTING」の値を渡して、状態「STATE_CONNECTING」の「else if」ステートメントのコード行を実行できるかどうかです。もしそうなら、どのように?

onConnectionStateChange と connect メソッドを添付しました。これらは、開発者の Web サイトで提供されているサンプルの心拍数モニター アプリで提供されているものとほとんど変更されていません。私の唯一の変更は、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