Calculate the Li-ion battery remaining capacity
Metering a battery capacity is a process of monitoring the incremental energy that goes into the battery while it is being charged and then again monitoring the incremental energy that leaves the battery while it is being discharged.
During the incremental gathering (which actually means periodic sampling) the samples are added to or subtracted from a running accumulator of the energy in the battery.

Initially when a fully charged new battery is deployed this running total is set to represent the rated battery capacity. 

Some battery packs will come with a small monitoring chip installed that performs the incremental accumulation all inside the chip. In the industry these are referred to as a "battery gas gauge chip". These chips monitor the battery pack voltage and current (as a voltage drop across a low resistance element placed in series with the battery) and internally compute that into incremental energy that gets added or subtracted from an internal accumulator register. These chips typically also have a bi-directional single wire serial interface that is used to configure the device and to be able to read out the accumulator value.

If all this is done properly then the accumulator value, whether that be done in software or in hardware by a "gas gauge", can be used to display current battery state.