Ich habe zwei Optionen für eine Verbindung zwischen BLE und dem Android Phone:Welcher Modus ermöglicht eine stabile Verbindung zwischen Bluetooth Low Energy und Android Phone?
connectGatt(this, false, mGattCallback); // If We want to directly connect to the device
connectGatt(this, true, mGattCallback); // If We want to automatically connect (in background) to the device
ich beide Methoden getestet: der erste ist mehr schnellere Verbindung, aber es darf nicht im Hintergrund wieder anschließen. In dieser Frage betrachte ich jedoch eine stabile Verbindung. Welcher ist in zwei Methoden stabiler? Wenn ich eine stabilere Verbindung haben möchte, könnten Sie mir die Methode vorschlagen, dies zu tun? Danke, dass alle
Das ist mein voller Code
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()) {
Log.d(TAG, "Call connect function");
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);
new Handler(getMainLooper()).post(new Runnable() {
@Override
public void run() {
if (device != null) {
Log.d(TAG, "Connect connectGatt");
mBluetoothGatt = device.connectGatt(getApplicationContext(), true, mGattCallback);
}
}
});
mBluetoothDeviceAddress = address;
mConnectionState = STATE_CONNECTING;
return true;
}
Danke. Aber ich teste mit beiden Optionen und autoConnect = false sieht stabiler aus. Ich weiß nicht, ob das stimmt oder nicht. – Jame
Können Sie näher erläutern, wie Sie mit stabileren meinen? Ist die Verbindung aufgebaut und läuft die Stabilität der Verbindung, hängt es nicht davon ab, ob AutoConnect verwendet wurde oder nicht. – Emil
Entschuldigung für die verzögerte Antwort. Stabile Verbindung bedeutet, dass es die Verbindung aufrecht erhält, bis ich die Trennfunktion anrufe. In meinem Fall habe ich jedoch das BLE-Gerät angeschlossen, ca. 3 Sekunden, es wird automatisch getrennt (nur für einige Zeit) – Jame