Android App continous receiving data via USB - android

I'm developing an Android App which i want to communicate with a device connected via USB. This device is delivering data all the time. These data are visible with a programm on the android linux shell.
My goal is to see those data in my App and after that deliver them via a Service to other Apps.
So my question is: Can i "open" a connection from my Android App to the USB-Port so im continuesly receiveing the data in my app, which are sent by the usb-device? And if yes, how would the code look like?
*Edit
Thank you for your answers, the app itself doesn't run on the commandline any more. So its no executable anymore but a shared library getting load by my android app.
It did before, but i want to be able to initiate the connection using NDK methods in my App to be able to see the data in my App itself. So I've tried to see if a connection is allready open.
I've added some functions to my code, so i can see if the usb-connection is open and i have the permission to that usb device.
UsbManager.hasPermission(device)
returns true, because I'm using an itent filter.
UsbManager.openDevice(device)
returns an UsbDeviceConnection, so i seem to have the access to use that device.
What im not capable of so far is receiving either iniciating the bus connection to by usb-device and of course either getting data input of that device.
Since my native code allready has a while(true)-method which is only using callbacks to send data to my app when actually data getting sent trough my usb-device i want to keep the work done in my c-code.
The only job my app should do is open the bus-connection once and after that be ready for callback from the c-code.
Is that possible?

If you have the app that runs on the command line, you can actually run a command line from within your app. Take a look at the Process class and the ProcessBuilder docs for full details. The example given is below
To run /system/bin/ping to ping android.com:
Process process = new ProcessBuilder()
.command("/system/bin/ping", "android.com")
.redirectErrorStream(true)
.start();
try {
InputStream in = process.getInputStream();
OutputStream out = process.getOutputStream();
readStream(in);
finally {
process.destroy();
}
}

Related

Is it possible to android to lost part of a command writen to a characteristic via BlueTooth via BLE?

I'm developing an adaptation for an android app, to communicate with a remote control, which has some pre defined commands.
I've followed this implementation to do the Bluetooth communication and it's working fine for sometime.
This app should communicate with the remote control every 5 minutes or less, and I've been using the app for almost 6 months now. The last week I've some command clashes problem and looking at the logs I couldn't identify why did that happened. The last time that this had happened the app was running for more than 24h, communicating with the remote control, without any communication issue.
Two of it's commands have some similar characters, the first one that have to be done, to establish the connection.
OK_CONN
And an sniffer command which keeps the pilot awake listening for some sensor data:
N
Looking at the logs I can see the answer for command N, after applying the command OK_CONN.
Is it possible for a Bluetooth command to lost part of it's data, during an established communication or am I doing something wrong when writing to a characteristic? Should I change the command names to avoid this kind of clash?
I'm using android 9, at a Sony XPeria XZ phone.
Edit to clarify #Emil comment
07:02:12.880 [BleThread] writing <OK+CONN> to characteristic
07:02:12.368 [Binder:19249_F] [onCharacteristicChanged():274]:
n command confirme
Looking at the logs I see that the last written command as an ok_conn but it has written only the N, this is been show as the last line, it has confirmed to receive the n command alone, instead of receiving the full data of ok_conn.
By name clashes I mean that maybe the last N of the ok_conn command is been accepted as the command.
I just realized what's going on, you can post that as an answer #Emil, my problem was at the logic that sends the first command, sometimes I send this command and the micro controller is not started yet, that's probably the reason of it getting only part of the command.
Not sure what you mean by name clashes, but Android will always write what you told it to write, without packet loss, as long as you follow the rules to never have more than one outstanding operation (always wait for callback before you send the next operation) and that your data must fit within the maximum length for the corresponding operation.

Problems with HostApduService in processCommandApdu methods

I'm experimenting problems between my NFC reader and my NFC Smarthphone (I am using Host-based Card Emulation). The problem is the following...
My android application is able to receive the APDU to select the AID and I am able to return a result for the reader.
After that, I try to send a specific command to reader and get the response. I receive the call in my override method processCommandApdu and when It return the result, I receive an event in onDeactivated method which reason is DEACTIVATION_LINK_LOSS (my phone is very close of the reader).
Have you any idea about it?
Background: Commands are always initiated by the terminal. Your processCommandApdu() method is there to take that command the terminal sends, and then send back a response in the format that the terminal requires. It depends on what type of application you are creating and what kind of terminal you are using for development.
Answer: [DEACTIVATION_LINK_LOSS] happens when the terminal is done sending commands out and the NFC connection between the device and the terminal is terminated. This has nothing to do with the fact that your phone is close or not, this only has to do with the fact that the terminal is done with it's communication and the connection between the device is no longer there.
Documentation:
https://developer.android.com/reference/android/nfc/cardemulation/HostApduService.html#DEACTIVATION_LINK_LOSS

How to kill a not running process in Android

Does anybody meet this situation before? I force stop an app in the setting dialog in a Android device.
But when I use ps command via adb it is still displayed on the screen. But this process can't be get by the activityManager.getRunningAppProcesses(). This process even exists after I uninstall this app, It happened randomly.
Because I opened a port in my app using ServerSocket, the port can't be released after I stop my app.
The next time I tried to open my app, it failed to using this port again.
I am using a ASUS EeePad and this problem seems only happened on that device.
By the way, the process can't be get by any 3rd party tools such as process manager.
But it really exists with a pid when I use ps via adb shell to list all processes.
Since there is no code .. here are some hints :
1-do you close your connection ??? that will be the main reason to hold the port busy.
2- deal with socket more safely when your app going to pause , stop, or being destroyed .. you can override onpause,onstop, or ondesotry ..and free the port before closing the app.
3- make connection in a separated thread and implement a timer to check if you really need the port or not ( I mean if your app is alive or not ) and based on that kill/leave the thread (connection)
4- in worst cases .. if you have control over the other side of connection ( server, device .. ) try to make your solution more flexible like making range of ports to firstly check then use if they are available.
good luck

Log Just My App and View it Remotely

I have an application/phone which I am developing to be given to people who will be using it in remote areas and not so tech-savvy. I want a mechanism to be able to read their log files remotely. The android app is making use of the logcat logging. I want to be able to just read my logs for my app by some remote mechanism. I was thinking more along the lines of reading the logcat then posting that up periodically to a REST service where I can database these. So question is how do I read the Logcat files programatically from my app?
If you invoke:
Proccess p = Runtime.getRuntime().exec("logcat");
InputStream in = p.getInputStream();
//code to process the InputStream
I honestly do not know the entire process of getting the input stream back, but the code above will allow your app to read the logcat file. You can go here for possible command-line paramaters for "logcat". The exec method runs the string through a linux shell, btw.
You must have the permission READ_LOGS

Web Server on Android phone

I'm trying to set up a web server using the Restlet framework on my Android phone. My idea is to build a game where one phone creates some markers on a map which then can be transferred directly to other phones using rest. At first (and for simplicity) I want to transfer a List of Objects.
Running a server on my computer seems to work fine, but when I transfer the code to my Android application, it won't start the server. Here is the code:
Component serverComponent = new Component();
serverComponent.getServers().add(Protocol.HTTP, 80);
final Router router = new Router(serverComponent.getContext().createChildContext());
router.attach("/gamedata", GameDataResourceServer.class);
serverComponent.getDefaultHost().attach(router);
serverComponent.start();
When the line serverComponent.start(); is executed, this Exception is thrown:
java.net.BindException: Permission denied (maybe missing INTERNET permission), although the internet permission is in the manifest file. Searching for some tutorials didn't help either. The result are either client applications or very complicated scenarios.
Could someone give an example for a simple application?
In Unix-type environments you typically need root access to bind to a TCP port below 1024. You're trying to bind to port 80, and unless you run this code as root the OS will prevent the request.

Categories

Resources