Search code examples
javaandroidc++headphones

Android send/receive bytes from headphone/usb jack?


Simple question, is there any way to read bytes/data from the headphone jack of an android phone? I know HTC made an app that lets headphones act as an antenna and gets Radio that way, but do i have to use native C++ for that or what? What i want to do is attach a double throw switch to the headphone jack and i want my phone to detect if the switch is pressed or not. Any way to do this??? I can tell this won't be an easy feat, but i've probably been through far worse.

Edit: even if it was the USB jack, i wudnt mind that either. I just want to attach a switch to my phone and use a program to detect if it's on or off


Solution

  • As the headphone jack typically includes a contact for a microphone to support headsets you could use digital/analog/digital conversion to transmit data. (That is how hardware extensions utilizing the headset jack like Square probably work).

    But for a switch like you describe I would either go Bluetooth (which can just act like a simple serial connection in code; but you would need some custom but not terribly complicated hardware for this) or try Arduino for Android, which is designed exactly for this use case and uses USB (but I don't have personal experience with it).

    No need for C++ or native in any case, everything is available via Java APIs.