1

I am creating an android gps tracking application and so far have my location uploaded to firebase using the Location Class:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference(path);
                    Location location = locationResult.getLastLocation();
                    if (location != null) {
                        //Save the location data to the database//
                        ref.setValue(location);
                        Log.d(TAG, "Location sent");
                    }

I can also get the battery level:

public float getBatteryLevel(){
        Intent batteryIntent = registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
        if(level == -1 || scale == -1){
            return 50.0f;
        }
        return ((float)level/(float)scale) * 100.0f;
    }

What I would like to do is add the battery level field to the location so it is sent to firebase as one. Can I extend the 'Location' class to include a 'Battery' field and a method to update this field before sending?

dneyed
  • 11
  • 2

1 Answers1

1

As DatabaseReference.set(Object), you can create your custom object (getter,setter) that contains location data and battery data. After that you can set the value to Firebase.

Younes Ben Tlili
  • 263
  • 1
  • 2
  • 15