Skip to content

Commit

Permalink
Centralize check for being connected
Browse files Browse the repository at this point in the history
  • Loading branch information
weliem committed Oct 22, 2023
1 parent f4ca842 commit ef6f7f6
Showing 1 changed file with 5 additions and 50 deletions.
55 changes: 5 additions & 50 deletions blessed/src/main/java/com/welie/blessed/BluetoothPeripheral.java
Original file line number Diff line number Diff line change
Expand Up @@ -1153,11 +1153,6 @@ public boolean readCharacteristic(@NotNull final UUID serviceUUID, @NotNull fina
public boolean readCharacteristic(@NotNull final BluetoothGattCharacteristic characteristic) {
Objects.requireNonNull(characteristic, NO_VALID_CHARACTERISTIC_PROVIDED);

if (notConnected()) {
Logger.e(TAG, PERIPHERAL_NOT_CONNECTED);
return false;
}

if (doesNotSupportReading(characteristic)) {
String message = String.format("characteristic <%s> does not have read property", characteristic.getUuid());
throw new IllegalArgumentException(message);
Expand Down Expand Up @@ -1230,11 +1225,6 @@ public boolean writeCharacteristic(@NotNull final BluetoothGattCharacteristic ch
Objects.requireNonNull(value, NO_VALID_VALUE_PROVIDED);
Objects.requireNonNull(writeType, NO_VALID_WRITE_TYPE_PROVIDED);

if (notConnected()) {
Logger.e(TAG, PERIPHERAL_NOT_CONNECTED);
return false;
}

if (value.length == 0) {
throw new IllegalArgumentException(VALUE_BYTE_ARRAY_IS_EMPTY);
}
Expand Down Expand Up @@ -1314,11 +1304,6 @@ private boolean internalWriteCharacteristic(@NotNull final BluetoothGattCharacte
public boolean readDescriptor(@NotNull final BluetoothGattDescriptor descriptor) {
Objects.requireNonNull(descriptor, NO_VALID_DESCRIPTOR_PROVIDED);

if (notConnected()) {
Logger.e(TAG, PERIPHERAL_NOT_CONNECTED);
return false;
}

return enqueue(new Runnable() {
@Override
public void run() {
Expand Down Expand Up @@ -1350,11 +1335,6 @@ public boolean writeDescriptor(@NotNull final BluetoothGattDescriptor descriptor
Objects.requireNonNull(descriptor, NO_VALID_DESCRIPTOR_PROVIDED);
Objects.requireNonNull(value, NO_VALID_VALUE_PROVIDED);

if (notConnected()) {
Logger.e(TAG, PERIPHERAL_NOT_CONNECTED);
return false;
}

if (value.length == 0) {
throw new IllegalArgumentException(VALUE_BYTE_ARRAY_IS_EMPTY);
}
Expand Down Expand Up @@ -1432,11 +1412,6 @@ public boolean setNotify(@NotNull final UUID serviceUUID, @NotNull final UUID ch
public boolean setNotify(@NotNull final BluetoothGattCharacteristic characteristic, final boolean enable) {
Objects.requireNonNull(characteristic, NO_VALID_CHARACTERISTIC_PROVIDED);

if (notConnected()) {
Logger.e(TAG, PERIPHERAL_NOT_CONNECTED);
return false;
}

// Get the Client Characteristic Configuration Descriptor for the characteristic
final BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CCC_DESCRIPTOR_UUID);
if (descriptor == null) {
Expand Down Expand Up @@ -1500,11 +1475,6 @@ private void adjustWriteTypeIfNeeded(@NotNull final BluetoothGattCharacteristic
* @return true if the operation was enqueued, false otherwise
*/
public boolean readRemoteRssi() {
if (notConnected()) {
Logger.e(TAG, PERIPHERAL_NOT_CONNECTED);
return false;
}

return enqueue(new Runnable() {
@Override
public void run() {
Expand Down Expand Up @@ -1538,11 +1508,6 @@ public boolean requestMtu(final int mtu) {
throw new IllegalArgumentException("mtu must be between 23 and 517");
}

if (notConnected()) {
Logger.e(TAG, PERIPHERAL_NOT_CONNECTED);
return false;
}

return enqueue(new Runnable() {
@Override
public void run() {
Expand Down Expand Up @@ -1570,11 +1535,6 @@ public void run() {
public boolean requestConnectionPriority(@NotNull final ConnectionPriority priority) {
Objects.requireNonNull(priority, NO_VALID_PRIORITY_PROVIDED);

if (notConnected()) {
Logger.e(TAG, PERIPHERAL_NOT_CONNECTED);
return false;
}

return enqueue(new Runnable() {
@Override
public void run() {
Expand Down Expand Up @@ -1615,11 +1575,6 @@ public boolean setPreferredPhy(@NotNull final PhyType txPhy, @NotNull final PhyT
Objects.requireNonNull(rxPhy);
Objects.requireNonNull(phyOptions);

if (notConnected()) {
Logger.e(TAG, PERIPHERAL_NOT_CONNECTED);
return false;
}

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
Logger.e(TAG, "setPreferredPhy requires Android 8.0 or newer");
return false;
Expand Down Expand Up @@ -1658,11 +1613,6 @@ public void run() {
* in {@link BluetoothPeripheralCallback#onPhyUpdate}
*/
public boolean readPhy() {
if (notConnected()) {
Logger.e(TAG, PERIPHERAL_NOT_CONNECTED);
return false;
}

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
Logger.e(TAG, "setPreferredPhy requires Android 8.0 or newer");
return false;
Expand Down Expand Up @@ -1710,6 +1660,11 @@ public boolean clearServicesCache() {
* @return true if the command was successfully enqueued, otherwise false
*/
private boolean enqueue(Runnable command) {
if (notConnected()) {
Logger.e(TAG, PERIPHERAL_NOT_CONNECTED);
return false;
}

final boolean result = commandQueue.add(command);
if (result) {
nextCommand();
Expand Down

0 comments on commit ef6f7f6

Please sign in to comment.