Temperature sticker reading Android study

I have downloaded the app from Android SDK Demo Showroom, I entered the NearableID my sticker. Now I do not know how to read the temperature from the sticker.
Copy the code below:
public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";

private ShowroomManager showroomManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Map<NearableID, Product> products = new HashMap<>();
    // TODO: replace with identifiers of your own nearables
    products.put(new NearableID("0dd4f43ff17b36d5"), new Product("Scarpa",
            "Non toccare la scarpa"));
    products.put(new NearableID("96e8de48cb6a8e8c"), new Product("Cane",
            "Lascialo immediatamente può morderti"));
    showroomManager = new ShowroomManager(this, products);
    showroomManager.setListener(new ShowroomManager.Listener() {
        @Override
        public void onProductPickup(Product product) {
            ((TextView) findViewById(R.id.titleLabel)).setText(product.getName());
            ((TextView) findViewById(R.id.descriptionLabel)).setText(product.getSummary());
            findViewById(R.id.descriptionLabel).setVisibility(View.VISIBLE);
        }
        @Override
        public void onProductPutdown(Product product) {
            ((TextView) findViewById(R.id.titleLabel)).setText("Prendi un oggetto per avere informazioni su di esso:");
            findViewById(R.id.descriptionLabel).setVisibility(View.INVISIBLE);
        }

    });
}

@Override
protected void onResume() {
    super.onResume();

    if (!SystemRequirementsChecker.checkWithDefaultDialogs(this)) {
        Log.e(TAG, "Can't scan for beacons, some pre-conditions were not met");
        Log.e(TAG, "Read more about what's required at: http://estimote.github.io/Android-SDK/JavaDocs/com/estimote/sdk/SystemRequirementsChecker.html");
        Log.e(TAG, "If this is fixable, you should see a popup on the app's screen right now, asking to enable what's necessary");
    } else {
        Log.d(TAG, "Starting ShowroomManager updates");
        showroomManager.startUpdates();
    }
}

@Override
protected void onPause() {
    super.onPause();
    Log.d(TAG, "Stopping ShowroomManager updates");
    showroomManager.stopUpdates();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    showroomManager.destroy();
}

}

Hi @Gianni_Mandolino!

In order to read the temperature values from the sticker, probably the easiest way would be modifying ShowroomManager class a little bit, by adding temperature value to be tracked. You can do it in onNearablesDiscovered callback:

public ShowroomManager(Context context, final Map<NearableID, Product> products) {
    beaconManager = new BeaconManager(context);
    beaconManager.setNearableListener(new BeaconManager.NearableListener() {
        @Override
        public void onNearablesDiscovered(List<Nearable> list) {
            for (Nearable nearable : list) {

              //Handle temperature value
              long temperature = nearable.temperature;

                NearableID nearableID = new NearableID(nearable.identifier);
                if (!products.keySet().contains(nearableID)) { continue; }

                boolean previousStatus = nearablesMotionStatus.containsKey(nearableID) && nearablesMotionStatus.get(nearableID);
                if (previousStatus != nearable.isMoving) {
                    Product product = products.get(nearableID);
                    if (nearable.isMoving) {
                        listener.onProductPickup(product);
                    } else {
                        listener.onProductPutdown(product);
                    }
                    nearablesMotionStatus.put(nearableID, nearable.isMoving);
                }
            }
        }
    });
}

Depending on use case, you can log that value or just pass it to the MainActivity, it’s up to you. :slight_smile:


If I enter your code in the mainActivity, you do not recognize many objects.
example: beaconManager, temperatures, Nearable

My intention was to suggest you updating ShowroomManager class with only one line of code that retrieve temperature value from Nearable object. :slight_smile: Then you take use of that value - simply by logging it in Logcat or pass to another object that would store that value. I promise to be more concise while explaining.

All in all, the easiest way to follow temperature value is updating onNearablesDiscovered callback handling inside ShowroomManager class. You can replace that part with code attached below:

        @Override
        public void onNearablesDiscovered(List<Nearable> list) {
            for (Nearable nearable : list) {

                double temperature =  nearable.temperature;
                Log.d("ShowroomManager", "Nearable: " + nearable.identifier + ", temperature value: " + temperature);

                NearableID nearableID = new NearableID(nearable.identifier);
                if (!products.keySet().contains(nearableID)) { continue; }

                boolean previousStatus = nearablesMotionStatus.containsKey(nearableID) && nearablesMotionStatus.get(nearableID);
                if (previousStatus != nearable.isMoving) {
                    Product product = products.get(nearableID);
                    if (nearable.isMoving) {
                        listener.onProductPickup(product);
                    } else {
                        listener.onProductPutdown(product);
                    }
                    nearablesMotionStatus.put(nearableID, nearable.isMoving);
                }
            }
        }

Then, you will be able to observe it in Android Studio by using Android Monitor. An exemplary logs should be presented like there are on screenshot below:

I put your changes in the showroom class, and it is correct.
Now I want to pass a value of “temperature = nearable.temperature” in MainActivity class and then display it on the phone with the TextView.
In the class MainActivity not recognize me nearable.temperature method