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.
Related
Is there a way to programmatically get list of apps connecting to the internet and their inbound and outbound connection.
Thinking of doing an app that does this and do not need to root the phone.
If you want a low level solution you can try the netstat approach.
On linux based systems you can read network information from /proc/net/route.
One way to do this is to to include a busybox binary in your app (make sure to look at the licencse) And run busybox netstat, then read the output into your app.
Process process = Runtime.getRuntime().exec("busybox netstat -n");
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream())
process.getOutputStream().close();
Then you can identify which app is using which connection by the process id column, you'll get the procces user which can be identified with an application such as app_23 , see this answer how to get the app name from the pid
If you don't want to include the busybox binary you can try reading the information from procfs yourself.
Luckily someone already did that work for you, see this example.
This information is already available in settings under "DataUsage". You just have to extract it from there; have a look at - TrafficStats.
Similar question - about app level data consumption
I am trying to create a web service for my android application. The web service is supposed to run on UltiDev, the issue i get is when i try to access the web service through a local android emulator. I was advised to change the url of the web service to http://10.0.2.2:portnumber/serviceName but when i attempt to make this change i get an error in UltiDev. It turns out that i have to bind the port first before i can do this, however UltiDev does not allow me to do this. This is the error i get. Sorry i can't post the actual image because i need reputation points first.
Failed to register application because System.ApplicationException: Unable to register application because not all required applications settings are specified: Listen endpoints are either not specified or are already taken by other applications.
at UWS.Configuration.WebAppConfigEntry.ApplyFinalDefaultsAndValidateBeforeSaving()
.....
Could you please clarify whether you are trying to do this programmatically, or using UWS Explorer UI? Please attach a screenshot - it always helps.
In case you use UWS Explorer, the screenshot below shows how to add a port binding. If you click "Specify host name or IP address" radiobutton, you will be able to specify an IP address.
If you do all that, but then you still get the same error, it means your system acts as if all IP+ports are already taken, which often is caused by security software, like firewall+antiviruses.
I am into a new project which requires me to use a USBconnected "Webcam".
The whole fun should run on Android 4.4.
my little story was:
I try multiple apps which do this - all work on both my testing devices
adapting some NDK lib that directly uses /dev/video0. This didnt work due to read-permission that was not granted in a new File("dev/video0").canRead() check. Although my unix permissions are correct, this seems to not work due to some new check on Android 4.4. (the whole thing was suggested here: Connect Android phone to a USB Web camera )
next: discover the UsbAccessory API that supposedly easens a lot of the above.
´find no documentation or anything about how to correctly handle a webcam
I still try, but don't come further than finding no device via
usbManager.getAccessory();
I've also tried to discover devices by filtering for a USB_ATTACHED broadcast but nothing triggers.
So I am starting to ask myself how the hell do others find the devices & communicate with them to get the pictures?
Anyone has sources from which i could learn, or a tutorial or something?
Little update from my side:
- I've gotten access by using the Android USB Host API e.g. UsbDevice instead of UsbAccessory.
- I have the connection and everything setup fine, and can now send binary data to my webcam and supposedly receive.
I can now send controlCommands via connection.controlTransfer(...) or use a "UsbRequest" in order to receive data.
However, I couldn't find any documentation to "make the camera submit pictures" to me. My Endpoint is of type XFER_INT (=interrupts).
I am continuing to try sending out various commands (e.g. binary values) but haven't had any success so far.
I have a system app that sets a forwarding proxy inside android devices. The problem is that since all traffic is redirected to that app, the Data usage screen under settings shows as if all traffic is coming from my app and not other apps.
Reading this: https://source.android.com/devices/tech/datausage/index.html helped me understand that you cannot exclude an app from the data usage unless it is built with the system image. (However, VPNService is able to exclude itself from data usage, so if I could find a hack to do this, it would work for me).
When looking at the android system file /proc/net/xt_qtaguid/ctrl it clearly shows that all socket connections are being tagged with my app's uid.
I looked at qtaguid.c (https://github.com/cgjones/android-system-core/blob/master/libcutils/qtaguid.c) and tried to implement the same mechanism but the problem is that I can't even get the file descriptor from my socketChannel. Since android uses SocketChannelImpl, I tried to use reflection to get the fd, but this did not work:
clientChannel = server.accept();
Class<?> clientChannelImpl = Class.forName("java.nio.SocketChannelImpl");
Method method = clientChannelImpl.getDeclaredMethod("getFD", new Class<?>[0]);
FileDescriptor fd = (FileDescriptor) method.invoke(clientChannel, null);
Class fileDescriptorClass = FileDescriptor.class;
Field field = fileDescriptorClass.getDeclaredField("fd");
field.setAccessible(true);
I guess even if this worked, I have no idea how the TAG is generated so that I can switch it out with every connection. I know the last 4 bytes of this tag is the uid of the app creating the socket in hex, but thats about it.
Is any of this even possible? Thank you!
I guess it's too late, but anyway.
You should tag client socket, the one you use to establish a remote connection, and not a server socket.
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();
}
}