With the gyroscope, you could try to compare the angle on all three axises and deduct the orientation.
The is portrait/landscape will never work on a square screen since it is never portrait or landscape (portrait means the aspect ratio is inferior to 1, landscape means the aspect ratio is superior to 1, which is why on a tablet or mobile there are 2 positions landscape and 2 positions portrait.)