@@ -212,7 +212,7 @@ public void run() {
212212 }
213213 };
214214 private class GattConnectionState {
215- public final HashMap <UUID , String > devInfoValues = new HashMap <>();
215+ public final HashMap <DevInfoCharacteristic , String > devInfoValues = new HashMap <>();
216216
217217 public final HashMap <Byte , ModuleInfoImpl > moduleInfo = new HashMap <>();
218218 public final AndroidBleConnection androidConn ;
@@ -243,7 +243,9 @@ public void checkConnectionReady() {
243243 try {
244244 SharedPreferences .Editor editor = deviceStates .edit ();
245245 JSONObject newCachedState = new JSONObject ();
246- newCachedState .put ("firmware" , devInfoValues .get (DevInfoCharacteristic .FIRMWARE_VERSION .uuid ()));
246+ for (DevInfoCharacteristic it : DevInfoCharacteristic .values ()) {
247+ newCachedState .put (it .key (), devInfoValues .get (it ));
248+ }
247249
248250 JSONObject newModuleState = new JSONObject ();
249251 for (ModuleInfoImpl it : moduleInfo .values ()) {
@@ -274,11 +276,11 @@ public void run() {
274276
275277 public MetaWearBoard .DeviceInformation buildDeviceInfo () {
276278 return new MetaWearBoard .DeviceInformation () {
277- @ Override public String manufacturer () { return devInfoValues .get (DevInfoCharacteristic .MANUFACTURER_NAME . uuid () ); }
278- @ Override public String serialNumber () { return devInfoValues .get (DevInfoCharacteristic .SERIAL_NUMBER . uuid () ); }
279- @ Override public String firmwareRevision () { return devInfoValues .get (DevInfoCharacteristic .FIRMWARE_VERSION . uuid () ); }
280- @ Override public String hardwareRevision () { return devInfoValues .get (DevInfoCharacteristic .HARDWARE_VERSION . uuid () ); }
281- @ Override public String modelNumber () { return devInfoValues .get (DevInfoCharacteristic .MODEL_NUMBER . uuid () ); }
279+ @ Override public String manufacturer () { return devInfoValues .get (DevInfoCharacteristic .MANUFACTURER_NAME ); }
280+ @ Override public String serialNumber () { return devInfoValues .get (DevInfoCharacteristic .SERIAL_NUMBER ); }
281+ @ Override public String firmwareRevision () { return devInfoValues .get (DevInfoCharacteristic .FIRMWARE_VERSION ); }
282+ @ Override public String hardwareRevision () { return devInfoValues .get (DevInfoCharacteristic .HARDWARE_VERSION ); }
283+ @ Override public String modelNumber () { return devInfoValues .get (DevInfoCharacteristic .MODEL_NUMBER ); }
282284 @ Override public String toString () {
283285 return String .format ("{manufacturer: %s, serialNumber: %s, firmwareRevision: %s, hardwareRevision: %s, modelNumber: %s}" ,
284286 manufacturer (), serialNumber (), firmwareRevision (), hardwareRevision (), modelNumber ());
@@ -743,31 +745,64 @@ public void run() {
743745 gattManager .executeNext (GattActionKey .DESCRIPTOR_WRITE );
744746 int newCount = state .nDescriptors .decrementAndGet ();
745747 if (newCount == 0 ) {
746- for (final DevInfoCharacteristic it : DevInfoCharacteristic .values ()) {
747- gattManager .queueAction (new Action () {
748- @ Override
749- public boolean execute () {
750- BluetoothGattService service = gatt .getService (DevInfoCharacteristic .serviceUuid ());
751- BluetoothGattCharacteristic devInfoChar = service .getCharacteristic (it .uuid ());
752-
753- if (devInfoChar != null ) {
754- gattManager .setExpectedGattKey (GattActionKey .CHAR_READ );
755- gatt .readCharacteristic (devInfoChar );
756- return true ;
757- }
748+ String cachedState = deviceStates .getString (gatt .getDevice ().getAddress (), "" );
758749
759- state .devInfoValues .put (it .uuid (), Constant .METAWEAR_R_MODULE );
760- state .checkConnectionReady ();
761- return false ;
762- }
763- });
750+ if (cachedState .isEmpty ()) {
751+ readDeviceInformation (gatt , state );
752+ } else {
753+ try {
754+ JSONObject jsonCachedState = new JSONObject (cachedState );
755+ state .devInfoValues .put (DevInfoCharacteristic .MODEL_NUMBER , jsonCachedState .getString (DevInfoCharacteristic .MODEL_NUMBER .key ()));
756+ state .devInfoValues .put (DevInfoCharacteristic .HARDWARE_VERSION , jsonCachedState .getString (DevInfoCharacteristic .HARDWARE_VERSION .key ()));
757+ state .devInfoValues .put (DevInfoCharacteristic .MANUFACTURER_NAME , jsonCachedState .getString (DevInfoCharacteristic .MANUFACTURER_NAME .key ()));
758+ state .devInfoValues .put (DevInfoCharacteristic .SERIAL_NUMBER , jsonCachedState .getString (DevInfoCharacteristic .SERIAL_NUMBER .key ()));
759+
760+ gattManager .queueAction (new Action () {
761+ @ Override
762+ public boolean execute () {
763+ BluetoothGattService service = gatt .getService (DevInfoCharacteristic .serviceUuid ());
764+ BluetoothGattCharacteristic devInfoChar = service .getCharacteristic (DevInfoCharacteristic .FIRMWARE_VERSION .uuid ());
765+
766+ if (devInfoChar != null ) {
767+ gattManager .setExpectedGattKey (GattActionKey .CHAR_READ );
768+ gatt .readCharacteristic (devInfoChar );
769+ return true ;
770+ }
771+ return false ;
772+ }
773+ });
774+ } catch (JSONException e ) {
775+ readDeviceInformation (gatt , state );
776+ }
764777 }
765778
766779 gattManager .executeNext (GattActionKey .NONE );
767780 }
768781 }
769782 }
770783
784+ private void readDeviceInformation (final BluetoothGatt gatt , final GattConnectionState state ) {
785+ for (final DevInfoCharacteristic it : DevInfoCharacteristic .values ()) {
786+ gattManager .queueAction (new Action () {
787+ @ Override
788+ public boolean execute () {
789+ BluetoothGattService service = gatt .getService (DevInfoCharacteristic .serviceUuid ());
790+ BluetoothGattCharacteristic devInfoChar = service .getCharacteristic (it .uuid ());
791+
792+ if (devInfoChar != null ) {
793+ gattManager .setExpectedGattKey (GattActionKey .CHAR_READ );
794+ gatt .readCharacteristic (devInfoChar );
795+ return true ;
796+ }
797+
798+ state .devInfoValues .put (it , Constant .METAWEAR_R_MODULE );
799+ state .checkConnectionReady ();
800+ return false ;
801+ }
802+ });
803+ }
804+ }
805+
771806 @ Override
772807 public void onReadRemoteRssi (BluetoothGatt gatt , int rssi , int status ) {
773808 gattManager .updateExecActionsState (true );
@@ -803,13 +838,18 @@ public void run() {
803838 }
804839 } else {
805840 String charStringValue = new String (characteristic .getValue ());
806- state .devInfoValues .put (characteristic .getUuid (), charStringValue );
841+ state .devInfoValues .put (DevInfoCharacteristic .uuidToDevInfoCharacteristic (characteristic .getUuid ()),
842+ charStringValue );
843+
807844 if (characteristic .getUuid ().equals (DevInfoCharacteristic .FIRMWARE_VERSION .uuid ())) {
808845 Version deviceFirmware = new Version (charStringValue );
809846
810847 if (deviceFirmware .compareTo (Constant .SERVICE_DISCOVERY_MIN_FIRMWARE ) < 0 ) {
848+ board .receivedModuleInfo (new DummyModuleInfo (InfoRegister .AMBIENT_LIGHT , (byte ) -1 , false ));
849+ board .receivedModuleInfo (new DummyModuleInfo (InfoRegister .BAROMETER , (byte ) -1 , false ));
850+ board .receivedModuleInfo (new DummyModuleInfo (InfoRegister .GSR , (byte ) -1 , false ));
811851 board .receivedModuleInfo (new DummyModuleInfo (InfoRegister .TEMPERATURE , Constant .SINGLE_CHANNEL_TEMP_IMPLEMENTATION , true ));
812- board .receivedModuleInfo (new DummyModuleInfo (InfoRegister .ACCELEROMETER , Constant .SINGLE_CHANNEL_TEMP_IMPLEMENTATION , true ));
852+ board .receivedModuleInfo (new DummyModuleInfo (InfoRegister .ACCELEROMETER , Constant .MMA8452Q_IMPLEMENTATION , true ));
813853 board .receivedModuleInfo (new DummyModuleInfo (InfoRegister .GYRO , (byte ) -1 , false ));
814854 } else {
815855 String cachedState = deviceStates .getString (gatt .getDevice ().getAddress (), "" );
@@ -818,7 +858,7 @@ public void run() {
818858 } else {
819859 try {
820860 JSONObject jsonCachedState = new JSONObject (cachedState );
821- String cachedFirmware = jsonCachedState .getString ("firmware" );
861+ String cachedFirmware = jsonCachedState .getString (DevInfoCharacteristic . FIRMWARE_VERSION . key () );
822862
823863 if (!cachedFirmware .equals (charStringValue )) {
824864 state .discoverModuleInfo ();
@@ -913,7 +953,7 @@ public void executeOnUiThread() {
913953 }
914954
915955 /**
916- * Executes asynchronous tasks on a background thread. This is the default behaviour, to keep minimize
956+ * Executes asynchronous tasks on a background thread. This is the default behaviour, to minimize
917957 * activity on the UI thread.
918958 */
919959 public void executeOnBackgroundThread () {
0 commit comments