私の目標は、Bluetooth Low Energyデバイスと電話機との自動接続を実現することです。私は、サンプルコードに続いて、私は上記のコードはfalse
がautoconnectionするために使用することを意味ラインBLEのconnectGattにautoConnectの正しいフラグはありますか?
// We want to directly connect to the device, so we are setting the autoConnect parameter to false.
mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
を発見しました。しかし、私はhereでAPIを見つけ、それが
BluetoothGatt connectGatt(コンテキストコンテキスト、ブール自動接続、BluetoothGattCallbackコールバック、int型の輸送) 接続GATTへServerがこのデバイスによってホストされていると述べました。
そして、私はまた、二つのフラグ試してみました:true
とfalse
、とだけtrue
が仕事です。私はバージョン> = Android 5.0を使用しています。コードとAPIの間に矛盾がありますか?どの旗が正しいですか?自動接続をしたい場合は、何かメモが必要ですか?あなたがfalseに自動接続パラメータを設定する場合は、「直接接続」を取得するよう
これは、「直接接続」私のコード
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.d(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, true, mGattCallback);
Log.d(TAG, "Trying to create a new connection.");
mBluetoothDeviceAddress = address;
mConnectionState = STATE_CONNECTING;
return true;
}
これは、自動接続の動作に関する有用な情報です。あなたはそれについてのリファレンスを持っていますか? [connectGatt docs](https://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#connectGatt(android.content.Context、%20boolean、%20andandroid.bluetooth.BluetoothGattCallback))はあまり気にしません詳細については、私はこれまでのところ、Androidソースコード( 'BluetoothGatt.java'は' .aidl'ファイルに由来する 'IBluetoothGatt'に' clientConnect'を呼び出していますが、何も見つかりませんでしたこれをまだ実装しています) – m01
これは、接続コマンドがかなり多くのレイヤーを通過していることを示しています。 https://android.googlesource.com/platform/system/bt/+/master/stack/gatt/gatt_api.ccでGATT_Connectをチェックすることができます。以下は、aidlインターフェースのもう一方の端です:https://android.googlesource.com/platform/packages/apps/Bluetooth/+/dbaf9cd/src/com/android/bluetooth/gatt/GattService.java – Emil