I am working on an Android app that is able to control GPIO pins etc of a RaspberryPi. At the moment, I have to enter the IP-Address manually.
Is there some way to add a discovery-function, which finds the RaspberryPi in a network?
I want something which works out of the box when connecting the phone to wifi and connecting the RPi to the same network.
As far as I know all RaspberryPi devices start with the same MAC-Address prefix.
Just try to connect to several ip addresses which your raspi could have. Use port 10000. If your android device ip for instance is 192.168.1.6 then try all ip's from 192.168.1.3 to for instance 192.168.1.25. Don't try the 6. Set a socket connect time out of one second about otherwise you have to wait and wait before a connection times out.
Related
I have a SFTP server in my application provided by the Apache SSHD library, which I can access with no problem via the Wifi network connection, but in this project I need to make this server accessible via an ethernet connection.
I have a computer with the ip 169.254.2.2 and I connect it to the Android device via an USB to ethernet adapter (which upon connection becomes the eth0 interface).
Since it is a rooted device that will run the application, I'm using the su process to set the configuration of the network interface with the following commands:
ip addr add 169.254.2.1/24 dev eth0
ip route change 169.254.2.0/24 via 169.254.2.2 dev eth0
//I'm using route change because as soon as I add the ip
//it creates a route table to 169.254.2.0
The settings above seems to be working because both the ethernet icon starts showing on my device and ifconfig command confirms the assigned ip, but when I try to connect to the SFTP server from the computer the connection times out.
So I tried to ping the computer via the device monitoring the network with Wireshark:
Both the request and the reply got delivered fine, but when I try to do the other way around (the computer ping the device) the ping request times out and in Wireshark I get the following:
Android seems to be refusing or ignoring all the requests via the ethernet adapter. Is there a reason to it and/or another way to set my connection so this don't occur?
Additional devices details:
Moto Z Play with Android 7.1.1;
Moto G with Android 5.1 (ethernet icon doesn't show but the connection works the same)
I am struggling with this for three days now. It is an android client application that connects to the server AIR application through Wifi on local network.
I need my application to connect to the server every time it is launched without asking the user to enter local IP, in case it changed.
It seems there are many ways to make sure the connection is successful, but I want to make sure I can go without the help of RTMFP or PHP or SharedObject stuff.
I am able to get local IP using NetworkInterface and NetworkInfo ANE from Android client. Then I use the variable to
mySocket.connect(ipAddress, 8888);
But there is a problem:
1) When debugging on Android via Wifi
The detected IP 192.168.137.2
2) When debugging on Flash, on computer
The detected IP 192.168.137.3
So, the local IPs are not the same. So client fails to connect. Everything works perfect, if I manually set that IP, but I need a code that works, even if the IP on local network changed.
As I mentioned making a textInput field in case IP changed and ask the user manually enter IP is not an option.
My question is why using "localhost" as host parameter of socket.connect(host, port) does not work? If it did, there would be no need for detecting local IP at all. "localhost" works for me only if the client is running on computer, but not Android.
Is it the problem with Security Policy file? if so, I have no idea how to use that. I can't find any tutorial on that.
You have two devices
1. Android device running an app and acting as a client
2. Computer running an AIR app and acting as a server
Each of those devices will have it's own IP address on the network. In your case android device IP is 192.168.137.2, and computer IP is 192.168.137.3.
In order for client to connect to server there must to be some means of delivering server IP to the client. Normally for an app like this you enter server IP address manually in settings, or have one central place available on the net where server registers it's IP address and client fetches the address from there.
Since you don't want to use any of these options the last resort would be implementing local area network scanning. Where you scan all the segment 192.168.137.* and search for opened port reserved for server. That of course is not good implementation but could work if project is for your own use.
Lastly "localhost" is name mapped to loopback IP 127.0.0.1 which is special address and is used to connect to the same device app is running on. In your case if android app is connecting to localhost it is trying to connect to the android device itself.
I want to have my app able to connect to other devices over the same wifi network and transfer some data, that is phone A and phone B have my app installed and are on the same network. I am able to connect two devices currently with one acting as server and the other being client but this is with a static IP reference to the server device. In the open world this IP will change of course so my problem is how do I get phone A to start up as a server device then have phone B see the server device on the network and connect to it. So the server device would accept this client connection and carry out the needed actions when the connection is made. Is this even possible?
I believe you would have A device listening on port number x, and the client version (device B) looking for that same port number x. This could be wrong.
This is a job for DNS. If you don't have a DNS server available you could invent a discovery protocol in which machine A periodically multicasts the IP & port on which it is listening.
Machine B joins the multicast group. Once machine B knows A's address and port it can open a socket using the code you already have.
I want to start a local network connection using the built-in Wi-Fi hotspot on Android devices.
Is there a way to accomplish it?
And how to communicate between two devices?
EDIT:
I want to do it programmatically. Then I can transfer my own data between devices.
I haven't done this before, so my answer is all theoretical.
I think what you need to do is declare one device the server and start its hotspot manually with a known name.
Start / stop built-in Wi-Fi / USB tethering from code? (According to this question, you can't start hotspot programatically).
Once you have the hotspot set up, you start up the server app and wait for incoming connections. The server app can show you the ip address or hostname.
On the clients, you can do a simple check and see if the hotspot name is around and automatically connect to it if you find it. See this link for connecting to a network:
Using WifiManager to connect to a network
As for your second question, how to communicate - you need to establish a connection between the devices. For this, you need the ip address of the server. The simplest thing to do would be to show the ip address on the server UI and then manually enter it on the clients and press connect. It would then establish a connection to the server on a known port and you can send messages between client and server.
For reference, I found NanoHttpd, which is a webserver for Android. It uses ServerSocket to listen for incoming connections.
Possible answers:
Before ICS and non rooted devices
There is no way you can connect to a device over wifi. There is no API to do. Bluetooth is your best option or user interaction.
Before ICS and rooted devices
I am not sure if someone hacked the code, but when I tried to do an automatic connection to a wifi spot I noticed:
The api is hidden inside the SDK.
The method that does the connection checks the thread who ask for it. If it's not the os thread, it throws an exception.
Using ICS
With ICS there is a new way to connect devices through wifi called Wi-Fi Direct.
Here is a link with some demos.
If you are going to develop your own application for each and every individual terminals(devices) then i think for you socket programming will do the trick .
Yes , android supports socket programming in the same was as java socket programming .
1st google java socket programming tutorials and then you can using the same knowledge in android .
Also do remember to include the uses internet permission in the manifest , actually thats something which ate my brains for a long time :)
You mean communication between wifi enabled devices without any server like p2p? if so its wifi direct which is supported in ICS check this out http://developer.android.com/reference/android/net/wifi/p2p/package-summary.html
Before ICS there is no standard android API for wifi direct, though Galaxy S2 has wifi direct it uses proprietary API's which 3rd party devs can't use.
i dont know programmically create wify LAN,but you can create wify LAN manually,then you can do java tcp/udp program as said by brianestey
"As for your second question, how to communicate - you need to establish a connection between the devices. For this, you need the ip address of the server. The simplest thing to do would be to show the ip address on the server UI and then manually enter it on the clients and press connect. It would then establish a connection to the server on a known port and you can send messages between client and server."
for manual connection follow the steps
take settings/wireless&networks/portable wi-fi hotspot settings from any one of the phone
create new hotspot and turn on wi-fi portable hotspot from there
connect all other device to that hotspot including your pc
you will get ip of each device programmically (includig pc,but i dont know)
I've written an Android app that includes creating a socket connection between 2 mobiles over wifi. The connection works fine at home with my wireless router and WEP security. However, when I tried to run the app at my local coffee shop, I could not make the socket connection. Both droids were connected to the shop's wireless network and the client was using the correct IP address. However, when I tried to make the connection, I received a SocketTimeoutException. I've tried this app at other (home-type with WEP) locations and as long as both mobiles are connected to the wifi network they can set up the socket connection.
Is there something in the configuration of public wifi networks that doesn't allow socket connections to be set up?
It can be that router in this coffe shop allows only specific ports like 80 for http and other ports are blocked by device, maybe you can try setting up your server on the device on such normally open port.