From 95e1d1298b566ddd0fe5f1056875fb2be3d68cad Mon Sep 17 00:00:00 2001 From: oliexdev Date: Sun, 22 Aug 2021 10:52:20 +0200 Subject: [PATCH] update Blesses library version to 2.0.11 --- android_app/app/build.gradle | 2 +- .../bluetooth/BluetoothCommunication.java | 50 ++++++++++--------- .../core/bluetooth/BluetoothOKOK.java | 12 ++--- .../BluetoothSettingsFragment.java | 10 ++-- 4 files changed, 38 insertions(+), 36 deletions(-) diff --git a/android_app/app/build.gradle b/android_app/app/build.gradle index c778c8a9..29e7f834 100644 --- a/android_app/app/build.gradle +++ b/android_app/app/build.gradle @@ -148,7 +148,7 @@ dependencies { // Simple CSV implementation 'com.j256.simplecsv:simplecsv:2.6' // Blessed Android - implementation 'com.github.weliem:blessed-android:1.30' + implementation 'com.github.weliem:blessed-android:2.0.11' // CustomActivityOnCrash implementation 'cat.ereza:customactivityoncrash:2.3.0' // AppIntro diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCommunication.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCommunication.java index 3cd29a83..6f27bb40 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCommunication.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCommunication.java @@ -16,6 +16,9 @@ package com.health.openscale.core.bluetooth; +import static android.bluetooth.BluetoothGatt.GATT_SUCCESS; +import static android.content.Context.LOCATION_SERVICE; + import android.Manifest; import android.bluetooth.BluetoothGattCharacteristic; import android.bluetooth.BluetoothProfile; @@ -30,20 +33,19 @@ import androidx.core.content.ContextCompat; import com.health.openscale.R; import com.health.openscale.core.datatypes.ScaleMeasurement; -import com.welie.blessed.BluetoothCentral; -import com.welie.blessed.BluetoothCentralCallback; +import com.welie.blessed.BluetoothCentralManager; +import com.welie.blessed.BluetoothCentralManagerCallback; import com.welie.blessed.BluetoothPeripheral; import com.welie.blessed.BluetoothPeripheralCallback; +import com.welie.blessed.ConnectionState; +import com.welie.blessed.GattStatus; +import com.welie.blessed.HciStatus; +import com.welie.blessed.WriteType; import java.util.UUID; import timber.log.Timber; -import static android.bluetooth.BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT; -import static android.bluetooth.BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE; -import static android.content.Context.LOCATION_SERVICE; -import static com.welie.blessed.BluetoothPeripheral.GATT_SUCCESS; - public abstract class BluetoothCommunication { public enum BT_STATUS { RETRIEVE_SCALE_DATA, @@ -67,7 +69,7 @@ public abstract class BluetoothCommunication { private Handler callbackBtHandler; private Handler disconnectHandler; - private BluetoothCentral central; + private BluetoothCentralManager central; private BluetoothPeripheral btPeripheral; public BluetoothCommunication(Context context) @@ -76,7 +78,7 @@ public abstract class BluetoothCommunication { this.disconnectHandler = new Handler(); this.stepNr = 0; this.stopped = false; - this.central = new BluetoothCentral(context, bluetoothCentralCallback, new Handler(Looper.getMainLooper())); + this.central = new BluetoothCentralManager(context, bluetoothCentralCallback, new Handler(Looper.getMainLooper())); } protected boolean needReConnect() { @@ -84,8 +86,8 @@ public abstract class BluetoothCommunication { return true; } if (btPeripheral != null) { - int state = btPeripheral.getState(); - if (state == BluetoothProfile.STATE_CONNECTED || state == BluetoothProfile.STATE_CONNECTING) { + ConnectionState state = btPeripheral.getState(); + if (state.equals(BluetoothProfile.STATE_CONNECTED) || state.equals(BluetoothProfile.STATE_CONNECTING)) { return false; } } @@ -275,7 +277,7 @@ public abstract class BluetoothCommunication { protected void writeBytes(UUID service, UUID characteristic, byte[] bytes, boolean noResponse) { Timber.d("Invoke write bytes [" + byteInHex(bytes) + "] on " + BluetoothGattUuid.prettyPrint(characteristic)); btPeripheral.writeCharacteristic(btPeripheral.getCharacteristic(service, characteristic), bytes, - noResponse ? WRITE_TYPE_NO_RESPONSE : WRITE_TYPE_DEFAULT); + noResponse ? WriteType.WITHOUT_RESPONSE : WriteType.WITH_RESPONSE); } /** @@ -421,8 +423,8 @@ public abstract class BluetoothCommunication { } @Override - public void onNotificationStateUpdate(BluetoothPeripheral peripheral, BluetoothGattCharacteristic characteristic, int status) { - if( status == GATT_SUCCESS) { + public void onNotificationStateUpdate(BluetoothPeripheral peripheral, BluetoothGattCharacteristic characteristic, GattStatus status) { + if( status.value == GATT_SUCCESS) { if(peripheral.isNotifying(characteristic)) { Timber.d(String.format("SUCCESS: Notify set for %s", characteristic.getUuid())); resumeMachineState(); @@ -433,8 +435,8 @@ public abstract class BluetoothCommunication { } @Override - public void onCharacteristicWrite(BluetoothPeripheral peripheral, byte[] value, BluetoothGattCharacteristic characteristic, int status) { - if( status == GATT_SUCCESS) { + public void onCharacteristicWrite(BluetoothPeripheral peripheral, byte[] value, BluetoothGattCharacteristic characteristic, GattStatus status) { + if( status.value == GATT_SUCCESS) { Timber.d(String.format("SUCCESS: Writing <%s> to <%s>", byteInHex(value), characteristic.getUuid().toString())); nextMachineStep(); @@ -444,14 +446,14 @@ public abstract class BluetoothCommunication { } @Override - public void onCharacteristicUpdate(final BluetoothPeripheral peripheral, byte[] value, final BluetoothGattCharacteristic characteristic, final int status) { + public void onCharacteristicUpdate(final BluetoothPeripheral peripheral, byte[] value, final BluetoothGattCharacteristic characteristic, GattStatus status) { resetDisconnectTimer(); onBluetoothNotify(characteristic.getUuid(), value); } }; // Callback for central - private final BluetoothCentralCallback bluetoothCentralCallback = new BluetoothCentralCallback() { + private final BluetoothCentralManagerCallback bluetoothCentralCallback = new BluetoothCentralManagerCallback() { @Override public void onConnectedPeripheral(BluetoothPeripheral peripheral) { @@ -463,18 +465,18 @@ public abstract class BluetoothCommunication { } @Override - public void onConnectionFailed(BluetoothPeripheral peripheral, final int status) { - Timber.e(String.format("connection '%s' failed with status %d", peripheral.getName(), status )); + public void onConnectionFailed(BluetoothPeripheral peripheral, HciStatus status) { + Timber.e(String.format("connection '%s' failed with status %d", peripheral.getName(), status.value)); setBluetoothStatus(BT_STATUS.CONNECTION_LOST); - if (status == 8) { + if (status.value == 8) { sendMessage(R.string.info_bluetooth_connection_error_scale_offline, 0); } } @Override - public void onDisconnectedPeripheral(final BluetoothPeripheral peripheral, final int status) { - Timber.d(String.format("disconnected '%s' with status %d", peripheral.getName(), status)); + public void onDisconnectedPeripheral(final BluetoothPeripheral peripheral, HciStatus status) { + Timber.d(String.format("disconnected '%s' with status %d", peripheral.getName(), status.value)); } @Override @@ -534,7 +536,7 @@ public abstract class BluetoothCommunication { if (btPeripheral == null) { return false; } - if (btPeripheral.getState() != BluetoothProfile.STATE_DISCONNECTED) { + if (btPeripheral.getState() != ConnectionState.DISCONNECTED) { disconnect(); } if (callbackBtHandler == null) { diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothOKOK.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothOKOK.java index c77877fa..5cff0e4c 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothOKOK.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothOKOK.java @@ -1,15 +1,15 @@ package com.health.openscale.core.bluetooth; +import android.bluetooth.le.ScanFilter; import android.bluetooth.le.ScanResult; import android.content.Context; -import android.bluetooth.le.ScanFilter; import android.os.Handler; import android.os.Looper; import android.util.SparseArray; import com.health.openscale.core.datatypes.ScaleMeasurement; -import com.welie.blessed.BluetoothCentral; -import com.welie.blessed.BluetoothCentralCallback; +import com.welie.blessed.BluetoothCentralManager; +import com.welie.blessed.BluetoothCentralManagerCallback; import com.welie.blessed.BluetoothPeripheral; import org.jetbrains.annotations.NotNull; @@ -28,8 +28,8 @@ public class BluetoothOKOK extends BluetoothCommunication { private static final int IDX_IMPEDANCE_LSB = 11; private static final int IDX_CHECKSUM = 12; - private BluetoothCentral central; - private final BluetoothCentralCallback btCallback = new BluetoothCentralCallback() { + private BluetoothCentralManager central; + private final BluetoothCentralManagerCallback btCallback = new BluetoothCentralManagerCallback() { @Override public void onDiscoveredPeripheral(@NotNull BluetoothPeripheral peripheral, @NotNull ScanResult scanResult) { SparseArray manufacturerSpecificData = scanResult.getScanRecord().getManufacturerSpecificData(); @@ -63,7 +63,7 @@ public class BluetoothOKOK extends BluetoothCommunication { public BluetoothOKOK(Context context) { super(context); - central = new BluetoothCentral(context, btCallback, new Handler(Looper.getMainLooper())); + central = new BluetoothCentralManager(context, btCallback, new Handler(Looper.getMainLooper())); } @Override diff --git a/android_app/app/src/main/java/com/health/openscale/gui/preferences/BluetoothSettingsFragment.java b/android_app/app/src/main/java/com/health/openscale/gui/preferences/BluetoothSettingsFragment.java index efc12ee8..0ee9d2e6 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/preferences/BluetoothSettingsFragment.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/preferences/BluetoothSettingsFragment.java @@ -57,8 +57,8 @@ import com.health.openscale.core.bluetooth.BluetoothCommunication; import com.health.openscale.core.bluetooth.BluetoothFactory; import com.health.openscale.gui.utils.ColorUtil; import com.health.openscale.gui.utils.PermissionHelper; -import com.welie.blessed.BluetoothCentral; -import com.welie.blessed.BluetoothCentralCallback; +import com.welie.blessed.BluetoothCentralManager; +import com.welie.blessed.BluetoothCentralManagerCallback; import com.welie.blessed.BluetoothPeripheral; import java.util.HashMap; @@ -76,7 +76,7 @@ public class BluetoothSettingsFragment extends Fragment { private TextView txtSearching; private ProgressBar progressBar; private Handler progressHandler; - private BluetoothCentral central; + private BluetoothCentralManager central; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -123,7 +123,7 @@ public class BluetoothSettingsFragment extends Fragment { return formatDeviceName(device.getName(), device.getAddress()); } - private final BluetoothCentralCallback bluetoothCentralCallback = new BluetoothCentralCallback() { + private final BluetoothCentralManagerCallback bluetoothCentralCallback = new BluetoothCentralManagerCallback() { @Override public void onDiscoveredPeripheral(BluetoothPeripheral peripheral, ScanResult scanResult) { new Handler().post(new Runnable() { @@ -139,7 +139,7 @@ public class BluetoothSettingsFragment extends Fragment { deviceListView.removeAllViews(); foundDevices.clear(); - central = new BluetoothCentral(requireContext(), bluetoothCentralCallback, new Handler(Looper.getMainLooper())); + central = new BluetoothCentralManager(requireContext(), bluetoothCentralCallback, new Handler(Looper.getMainLooper())); central.scanForPeripherals(); txtSearching.setVisibility(View.VISIBLE);