@Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {

    startTime = 0;
    firstRun = true;
    isRunning = false;

    magneticFieldBias = new MagneticFieldBias();

    try {
      dataFileWriter =
          new DataFileWriter(
              FOLDER_NAME,
              ExtraFunctions.arrayToList(DATA_FILE_NAMES),
              ExtraFunctions.arrayToList(DATA_FILE_HEADINGS));
    } catch (IOException e) {
      e.printStackTrace();
    }

    sensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
    sensorMagneticField = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED);

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
    alertDialogBuilder
        .setMessage(DIALOG_MESSAGE)
        .setNeutralButton(
            "Start Calibration",
            new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
                // this onClick() will be overridden during onStart()
              }
            });

    return alertDialogBuilder.create();
  }
  @Override
  public void onSensorChanged(SensorEvent event) {

    if (firstRun) {
      startTime = event.timestamp;
      firstRun = false;
    }

    if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED) {
      magneticFieldBias.calcBias(event.values);

      // storing data to file
      ArrayList<Float> dataValues = ExtraFunctions.arrayToList(event.values);
      dataValues.add(0, (float) (event.timestamp - startTime));
      dataFileWriter.writeToFile("Magnetic_Field_Uncalibrated", dataValues);
    }
  }