Android send/receive bytes from headphone/usb jack? - java

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

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.

You are not going to be able to send data through the headphone jack into devices. Using the headphones as an antenna is different. It's not actually transmitting data into the phone, it's using the wire as an extension to the internal antenna (attached to a receiver inside the phone which generates the "data")
You should be able to do with the USB for your device though, you should focus on the USB anytime you want to transmit data from outside the phone into it, though you could probably also do it with an IR broadcaster/ bluetooth broadcaster set up instead of a wired switch.

Related

Is there a way to find out what a bluetooth LE device listens to?

I have a bluetooth LE device that is powered by its developer's app.
Sadly it is not maintained anymore and therefore I intend to rewrite the app from scratch.
Additionally it is not possible to pair to the device. It only listens to BLE.
Is it possible in any way to find out, how the device is being controlled?
I tried to decompile the code via JADX but since the manufacturer is chinese and decompilation already comes with its own problems, I thought if it would be possible to find it out without this procedure. (GitHub)App-Decompiled-Code (External)Original-APK-Download-Page
It depends what the device is really. There are number of profiles/services defined in the Bluetooth standard. If the device uses one of them then you could design from the specification rather than the device/app.
A good place to start is to use a generic Bluetooth Low Energy scanning and exploration tool such as nRF Connect to explore the device. Find out the UUIDs of the services and characteristics and compare it to the assigned 16-bit UUIDs values on https://www.bluetooth.com/specifications/assigned-numbers/
If it has a custom BLE profile/service then you can still use the nRF Connect app to explore it. If it is not obvious how to access the data or what commands need to be sent, then looking at the HCI logs on the phone after using the developer's app might be helpful.

How do I stop hogging the microphone

TL;DR: My app is hogging the user's microphone. Can I turn it off automatically whenever another app needs to use the mic?
I have an Android app that has some really cool microphone functionality, similar to Amazon Alexa, that stays on all the time in a background service. The problem is, my app hogs the users' microphone, making it unusable:
However, this is terrible application behavior on my behalf, and I want to do my best to avoid it. Is it possible to be notified when another application requests to use the microphone, so that I can automatically stop my service?
PS: I am using the Pocketsphinx library for continuous background voice recognition.
This is tricky, as I'm not a ware of any API for this. This surely will require system-level APIs to work like an "Ok Google" type of thing.
A viable option would be (from https://stackoverflow.com/a/43623308/603270) to run a Job at regular intervals, checking for foreground apps using android.permission.PACKAGE_USAGE_STATS.
This might suffice. But you could also add things regarding phone calls (how to detect phone call broadcast receiver in android) using android.intent.action.PHONE_STATE or media playback (via Receiver or maybe even MediaPlayer directly).
If you're really wanting to get this thing working, an alternative would be to get an array list of all installed apps on the system and which ones require permission to use the mic or not, then use an accessibility service to monitor the users screen if an app the user just opened requires the mic (which you'll know from the array you just grabbed). From there, disable the mic in your app if their app needs the mic. The background service can then check in intervals of, say, two minutes, to see if the app that required the mic is still open. This is really inefficient. But if you don't care, then this might be a good option.
There is no standard way to inform another app that you want access to the microphone (so that they release the resources and let you access it). You could however send a broadcast to all other apps ("requesting the microphone"), but the other apps would have to implement this feature (and very few or zero developers will do this).
I would recommend you to simply inform the user that the microphone is currently not available, because you can't do anything else.

Is it possible to set pin programmatically for bluetooth pairing?

I have an application (however I'm testing using the sample bluetooth chat from the SDK) where two android devices connects each other, and exchange data.
I already have part of the pairing process hardcoded using the BT address, problem is: the dialog it appears to confirm the pin. Is there a way to make that work programmatically? The only thing that "appeared" to solve my problem was in this question, but that API is completely abandoned, has no documentation and many issues.
As fair as I know the problem is that if the device is not the device database with paired devices, without root access the standard API is always going to prompt the user, but I'm still wondering if there's a way.
Bluetooth autopairing is defined only for some devices. To change this you should rewrite Android framework. So, from the application, it seems to me, this is impossible to do.

Simple application that simulates a bluetooth accessory

I'm looking for a no-brain application that allows me to send a pre-defined list of commands through a bluetooth connection, or an equivalent Java/Scala library that works decently in OSX (64bits).
In short, I want to use my computer to "simulate" a bluetooth accessory, so I must be able to control the pairing, the exchanged code, and so on...
Thoughts?
Simulating jsr-82 can be done with the BlueCove Emulator Module:
http://bluecove.org/bluecove-emu/
http://bluecove.org/ works on OS/X and am sure there wouldn't be any issues with a 64bit OS.
I'd refer you to this Answer: https://askubuntu.com/questions/20195/bluetooth-emulator
You may want to reconsider your question...
To emulate a bluetooth device you would need to develop a bluetooth driver getting it's input from a different source, this would be far more complex than buying a real device.
If you are looking to get some NMEA data though, you may be interested in this:
Most bluetooth enabled phones have apps that do that:
Android: https://market.android.com/details?id=com.cajax.gps2bt2
Any J2ME: http://www.digitalmobilemap.com/bluetooth-gps-simulator-for-j2me-phone
Haven't found one for iPhone, only reference it can be done: http://spench.net/drupal/software/iphone-gps
Hope that helps!
Can you be more exact?
Simulate a Bluetooth accessory for ..?
There are different BT accessories for different smartphones...
Making a BT accessory for iPhone usually requires an authentication chip from Apple...I don't know if a Mac can do it, probably it could - however I'm pretty sure it couldn't describe itself as an accessory.
Accessories usually imply a piece of hardware, in my understanding at least.

How to autoconnect to a bluetooth device when its near programmatically in android?

I am writing an app that creates RFCOMM socket with another device (not necessarily an android), and send some data over it. Right now I can connect and transfer data over the socket, but I also want to be able to stop when the device is far and auto start transfer when the device is near again. How do I know if the device is near again? Any pointers will be greatly appreciated. :)
I'm not aware of a nice, easy Java interface to it. You can connect using the NDK and call the hci_read_rssi() function (you need to write a native java call to this). This requires you to have already connected to the device. It will give you a signal strength reading which is relative to your distance, you will need to tweak this of course.
Many Bluetooth devices will allow you to program their operational mode (ref http://www.sparkfun.com/datasheets/Wireless/Bluetooth/rn-bluetooth-um.pdf). In this mode you can set the device to automatically connect without the need for pairing which could be what you're after. Of course you are limited by the actual device you need to work with.
Good luck!

Categories