A method for real-time compensation of magnetometers embedded on smartphones