Can I add the android battery level information to my location before sending data to firebase?

45 Views Asked by At

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?

1

There are 1 best solutions below

0
Younes Ben Tlili On

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.