diff options
author | Alex Blasche <alexander.blasche@theqtcompany.com> | 2014-11-12 11:28:28 +0100 |
---|---|---|
committer | Alex Blasche <alexander.blasche@theqtcompany.com> | 2014-11-14 08:46:12 +0100 |
commit | c01298910e6af5de369ff85e27179cbfd1278b0b (patch) | |
tree | 22aa597ae8b0cd4345fa7b8ee706565ca5a0611b /src/android | |
parent | 03d72f5a961bd1c81e9d599937b4105c35f31ee1 (diff) |
Apply fixes pointed out by Java code analyzer
This covers code optimizations and one minor corner case bug.
Change-Id: I9c4df9462e8610ea37a6e43e20840e537c295684
Reviewed-by: Timur Pocheptsov <Timur.Pocheptsov@digia.com>
Reviewed-by: Alex Blasche <alexander.blasche@theqtcompany.com>
Diffstat (limited to 'src/android')
-rw-r--r-- | src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothLE.java | 55 |
1 files changed, 27 insertions, 28 deletions
diff --git a/src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothLE.java b/src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothLE.java index 45120081..04b211a5 100644 --- a/src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothLE.java +++ b/src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothLE.java @@ -52,16 +52,17 @@ import java.util.UUID; public class QtBluetoothLE { private static final String TAG = "QtBluetoothGatt"; - private BluetoothAdapter mBluetoothAdapter; + private final BluetoothAdapter mBluetoothAdapter; private boolean mLeScanRunning = false; private BluetoothGatt mBluetoothGatt = null; private String mRemoteGattAddress; - private BluetoothDevice mRemoteGattDevice = null; /* Pointer to the Qt object that "owns" the Java object */ + @SuppressWarnings({"CanBeFinal", "WeakerAccess"}) long qtObject = 0; + @SuppressWarnings("WeakerAccess") Activity qtactivity = null; public QtBluetoothLE() { @@ -97,7 +98,7 @@ public class QtBluetoothLE { } // Device scan callback - private BluetoothAdapter.LeScanCallback leScanCallback = + private final BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() { @Override @@ -115,7 +116,7 @@ public class QtBluetoothLE { /* Service Discovery */ /*************************************************************/ - private BluetoothGattCallback gattCallback = new BluetoothGattCallback() { + private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() { public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { if (qtObject == 0) @@ -157,7 +158,7 @@ public class QtBluetoothLE { errorCode = 0; //QLowEnergyController::NoError final List<BluetoothGattService> services = mBluetoothGatt.getServices(); for (BluetoothGattService service: services) { - builder.append(service.getUuid().toString() + " "); //space is separator + builder.append(service.getUuid().toString()).append(" "); //space is separator } break; default: @@ -251,7 +252,7 @@ public class QtBluetoothLE { return; } - int errorCode = 0; + int errorCode; //This must be in sync with QLowEnergyService::ServiceError switch (status) { case BluetoothGatt.GATT_SUCCESS: @@ -346,7 +347,7 @@ public class QtBluetoothLE { int handle = handleForDescriptor(descriptor); - int errorCode = 0; + int errorCode; //This must be in sync with QLowEnergyService::ServiceError switch (status) { case BluetoothGatt.GATT_SUCCESS: @@ -372,15 +373,17 @@ public class QtBluetoothLE { public boolean connect() { - mRemoteGattDevice = mBluetoothAdapter.getRemoteDevice(mRemoteGattAddress); - if (mRemoteGattDevice == null) - return false; + BluetoothDevice mRemoteGattDevice; - mBluetoothGatt = mRemoteGattDevice.connectGatt(qtactivity, false, gattCallback); - if (mBluetoothGatt == null) + try { + mRemoteGattDevice = mBluetoothAdapter.getRemoteDevice(mRemoteGattAddress); + } catch (IllegalArgumentException ex) { + Log.w(TAG, "Remote address is not valid: " + mRemoteGattAddress); return false; + } - return true; + mBluetoothGatt = mRemoteGattDevice.connectGatt(qtactivity, false, gattCallback); + return mBluetoothGatt != null; } public void disconnect() { @@ -392,10 +395,7 @@ public class QtBluetoothLE { public boolean discoverServices() { - if (mBluetoothGatt == null) - return false; - - return mBluetoothGatt.discoverServices(); + return mBluetoothGatt != null && mBluetoothGatt.discoverServices(); } private enum GattEntryType @@ -411,9 +411,9 @@ public class QtBluetoothLE { public BluetoothGattDescriptor descriptor = null; public int endHandle; } - Hashtable<UUID, List<Integer>> uuidToEntry = new Hashtable<UUID, List<Integer>>(100); - ArrayList<GattEntry> entries = new ArrayList<GattEntry>(100); - private LinkedList<Integer> servicesToBeDiscovered = new LinkedList<Integer>(); + private final Hashtable<UUID, List<Integer>> uuidToEntry = new Hashtable<UUID, List<Integer>>(100); + private final ArrayList<GattEntry> entries = new ArrayList<GattEntry>(100); + private final LinkedList<Integer> servicesToBeDiscovered = new LinkedList<Integer>(); /* Internal helper function @@ -434,7 +434,7 @@ public class QtBluetoothLE { int serviceHandle = handles.get(0); try { - GattEntry entry = null; + GattEntry entry; for (int i = serviceHandle+1; i < entries.size(); i++) { entry = entries.get(i); switch (entry.type) { @@ -472,7 +472,7 @@ public class QtBluetoothLE { int serviceHandle = handles.get(0); try { - GattEntry entry = null; + GattEntry entry; for (int i = serviceHandle+1; i < entries.size(); i++) { entry = entries.get(i); switch (entry.type) { @@ -487,7 +487,7 @@ public class QtBluetoothLE { break; } } - } catch (IndexOutOfBoundsException ex) { } + } catch (IndexOutOfBoundsException ignored) { } return -1; } @@ -644,7 +644,6 @@ public class QtBluetoothLE { performServiceDetailDiscoveryForHandle(nextService, true); } catch (IndexOutOfBoundsException ex) { Log.w(TAG, "Expected queued service but didn't find any"); - return; } } } @@ -659,7 +658,7 @@ public class QtBluetoothLE { runningHandle = nextHandle; } - GattEntry entry = null; + GattEntry entry; try { entry = entries.get(nextHandle); } catch (IndexOutOfBoundsException ex) { @@ -669,7 +668,7 @@ public class QtBluetoothLE { return; } - boolean result = false; + boolean result; switch (entry.type) { case Characteristic: result = mBluetoothGatt.readCharacteristic(entry.characteristic); @@ -730,7 +729,7 @@ public class QtBluetoothLE { if (mBluetoothGatt == null) return false; - GattEntry entry = null; + GattEntry entry; try { entry = entries.get(charHandle-1); //Qt always uses handles+1 } catch (IndexOutOfBoundsException ex) { @@ -756,7 +755,7 @@ public class QtBluetoothLE { if (mBluetoothGatt == null) return false; - GattEntry entry = null; + GattEntry entry; try { entry = entries.get(descHandle-1); //Qt always uses handles+1 } catch (IndexOutOfBoundsException ex) { |