У меня проблема только в моем устройстве Oneplus, где как Pixel Nokia отлично работает.
Вот моя проблема. У меня есть устройство BLE, и я подключаюсь к нему через приложение, используя следующий код.
public boolean connect(final String address) {
if (mBluetoothAdapter == null || address == null) {
Log.e(TAG, "BluetoothAdapter not initialized or unspecified address.");
return false;
}
String mBluetoothDeviceAddress = Util.getDeviceAddress(this);
// Previously connected device. Try to reconnect.
if (mBluetoothGatt != null && Util.isBLEDeviceConnected(this, mBluetoothDeviceAddress)) {
Log.d(TAG, "Already connected");
broadcastUpdate(ACTION_GATT_CONNECTED, mBluetoothDeviceAddress);
return true;
} else if (mBluetoothGatt != null) {
Log.d(TAG, "GATT Connection is Required");
mBluetoothGatt.connect();
return true;
}
if (!mIsConnecting) {
final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
if (device == null) {
Log.w(TAG, "Device not found. Unable to connect.");
return false;
}
// We want to auto connect to the device, so we are setting the autoConnect
// parameter to true.
Log.d(TAG, "Trying to create a new connection.");
mBluetoothGatt = device.connectGatt(this, true, mGattCallback);
mIsConnecting = true;
} else {
Log.d(TAG, "Already connection is in progress");
}
return true;
}
@Override
public void onDestroy() {
super.onDestroy();
disconnect();
close();
unregisterReceiver(bluetoothReceiver);
unregisterReceiver(mGattDisconnectReceiver);
}
public void disconnect() {
Log.d(TAG, "Disconnecting Gatt " + mBluetoothGatt);
if (mBluetoothGatt == null) {
Log.w(TAG, "BluetoothGatt not initialized");
return;
}
mBluetoothGatt.disconnect();
mIsConnecting = false;
}
public void close() {
Log.d(TAG, "Closing Gatt " + mBluetoothGatt);
if (mBluetoothGatt == null) {
Log.w(TAG, "BluetoothGatt not initialized");
return;
}
mBluetoothGatt.close();
mIsConnecting = false;
mBluetoothGatt = null;
}
Сопряжение работает нормально, пока я не выключу/не выключу телефон. Если я перезагружу телефон в следующий раз, когда я попытаюсь выполнить сопряжение/подключение, он не будет работать. В нем написано "Пытаюсь создать новое соединение", как указано в моем коде, и написано в журналах ниже.
2019-03-01 09:01:08.083 9983-9983/com.wrkspot.employee.dev D/BLEService: Trying to create a new connection.
2019-03-01 09:01:08.084 9983-9983/com.wrkspot.employee.dev D/BluetoothGatt: connect() - device: C1:B4:70:12:4B:23, auto: true
2019-03-01 09:01:08.084 9983-9983/com.wrkspot.employee.dev D/BluetoothGatt: registerApp()
2019-03-01 09:01:08.085 9983-9983/com.wrkspot.employee.dev D/BluetoothGatt: registerApp() - UUID=a18ee742-4543-473c-8789-37a22845a96c
2019-03-01 09:01:08.087 9983-10064/com.wrkspot.employee.dev D/BluetoothGatt: onClientRegistered() - status=0 clientIf=8
Я застрял, и мне действительно нужно несколько предложений. Эта проблема возникает только на моем Oneplus 3T и в нескольких других телефонах, которые я тестировал, работает нормально.
Когда эта проблема возникает, я должен переустановить приложение и перезагрузить телефон. Только тогда я могу снова соединиться/соединиться.
Автосоединение со значением true работает только в том случае, если устройство Android находится в кеше или устройство подключено. При перезагрузке телефона кеш очищается. Таким образом, вы должны сканировать его снова....