Hi,
Thanks for your inquiry.
Your code looks fine because you can get BMX160 accel, gyro and mag data changing. Only when z axis is pointing upward, the mag data outputs 2047 (not sure if the unit is LSB or uT). You may plot mag x/y/z data in the unit of uT in 3D space when you rotate your device slowly and randomly in 3D space to see how the shape looks like.
BMM150 magnetometer is inside BMX160. It has +/-1300uT full scale range on x and y axes with 13-bit resolution. Z axis with +/-2500uT full scale range with 15-bit resolution which means that Z axis raw data should be between -16384LSB and +16383LSB. Please check if there is a strong magnetic source such as screw or a decoupling capacitor that contains Nickel material inside. This may cause mag z axis to be saturated.
Please see the attached two documents for more information about how to convert BMM150 raw data to final uT value.
Thanks.
... View more