Sending data from Nexus 5X Smartphone to Arduino MEGA 2560


I am trying to build a project that uses the sensors in a smartphone (gyro, acceleration, magnetometer, etc.), sends them to the Arduino MEGA and causes a rotation on the motor (speed and direction are tied to angle).

I found a project that uses wifi to connect the Arduino to the phone, but I want to use a direct connection. GitHub - Nurgak/Smartphone-sensor-data-collection-application: Android application for internal and external sensor reading over WiFi using the USB host feature and a connected device

How would I go about this?
Can I connect the smartphone the Arduino Mega via an OTG cable directly to the USB port?
Would I have to write a code for the phone to send its data to the Arduino?

Any resources you can provide to help out would be very much appreciated!

Thank you!

It is possible to establish wired USB/serial communications between an Android and Arduino.

I've used this app successfully as a terminal between a phone and a USB Serial microcontroller. I point this one out because the author has posted source code for the Android application here using this Android serial driver library from which derive an application specific to one's needs. I've not tried to build this having not (yet) mastered Android Studio.

I've done bluetooth serial apps to Arduino and apps using the Android device sensors using the much simplier AppInventor Android development environment. There exist a number of tutorials for Bluetooth to Arduino. Unfortunately AppInventor does not have a native USB OTG component, but there exist at least a couple of extensions (here and here) that are supposed to add this capability. I've not had success with either of these extensions to date.

The gist then is that what you've proposed is possible, but requires a non-trivial level of Android application development.