Calibrate your compass by moving your device in a figure 8 motion as shown below.

∞

DeviceOrientation Control: Quaternions