Dart Socket.connect fails when phone is connected to ESP32 access point - android

var socket = await Socket.connect(ip, port);
I'm attempting to connect to a local device from my phone (Pixel 4a), using Dart/Flutter. This succeeds when both my phone and the other device are on my router's local network. When I connect them to an ESP32 access point however, this code awaits indefinitely. It doesn't matter what the target device is, I've tried several. I've had other devices connect successfully on the ESP32 access point, so I'm certain it's not the ESP32 firmware. Only my phone refuses to connect when on the ESP32 access point. Unfortunately, I don't have another phone to test, so I don't know if this is Android-specific or model-specific.
The ESP32 access point does not have internet access, but that shouldn't matter for this purpose, as I'm trying to connect local devices. I suspect that something in Dart/Android is refusing to route to the address, despite it being accessible.
What is a possible cause for this refusal to connect? Is there another networking API in the Dart/Flutter environment that could give me more information? Perhaps a lower-level one? Some sort of error code would be far better than indefinite awaiting. Would I need to link to a function in another language to get more details? Java/C?
Thank you.

Related

Accessing an API on an Android device over the Internet

I have a use-case for Android devices that I can't seem to wrap my head around. The gist of it is I want to be able to send an API command from my server to my Android app over the Internet. The solution already works if my server application is hosted on the LAN: each Android device has an IP, and I send the command to each device's IP. What if my server is on the cloud? Is there a way for my server application to connect to my Android device directly without the need for a local server?
In this use-case, the Android devices are connected to network through either WiFi or ethernet, and the network has internet access.
Any suggestion would be wonderful, thank you!
When you are in local area network, both your server and client (Android device) are assigned with virtual IPs so there is no problem with see each other.
But when your move your server application to cloud, it cannot see your Android device anymore since your device is hidden by NAT. Only the gateway(your wifi router) IP can be seen by the server.
Some ways you can try:
Assign a static IP for your Android device, and configure your gateway to make a port forwarding to redirect the network traffic to your device. Then your server can see your device by sending to your-gateway-ip:port. But this is usually not a preferred way, unless you can always have full control of the client side network environment.
Change the application protocol to something like MQTT. In such case both your server and client can connect to this "broker" server so exchanging data is possible regardless of your client is under NAT or not. However, you may need to setup this extra broker server.
Find other third party notification services to do this server to client communication, such as Google Firebase.

How to make an Android application that will connect and read data from an alarm system (local network)?

I need to make an Android app for the problem mentioned in the title. This system already runs on Windows and is connected to the computer via an UTP cable. Now, I need to connect to the local network that I already did by connecting it to the router (I'm not sure if this is enough, but using the Fing app I detected the device in the local network), and build an Android app.
Just to mention I have tried NSD (link below) but I do not think the alarm system supports it.
https://developer.android.com/training/connect-devices-wirelessly/nsd.html
I had a problem defining the type of service. I tried to find all the devices using the code below, but unsuccessfully.
private static final String SERVICE_TYPE = "_services._dns-sd._udp";
Using this type of service I could not find anything, i.e. I did not manage to trigger the method
public void onServiceFound (NsdServiceInfo service)
My question is, should I continue with this approach (is it possible at all)? Otherwise, I need some guidance on how to connect to the device and read data(string type).
For now, I only have the IP address I received using the Fing app.
I found a partial solution for my problem. This is how to connect device and check are data is transferring (in case the someone have similar problem).
You need to do next:
connect device direct to yours computer with Ethernet cable
used WireShark app to find ip address, port and protocol (utp
protocol in my case)
after get device data, disconect device and reconnect directly with yours modem (no need for aditional device like ethernet switch, or to do the port forwarding)
you can use some udp client app on android phone to check if data
are receiving (I used TCP Sender / Receiver). Just set ip address
and port.
My problem was because I used wrong UDP client app. That's the reason it didn't working from the beginning.

Stream data from Android device to desktop computer when they are connected to different networks (Android WiFi, computer Ethernet)?

I am trying to make an android application that will stream live sensor data (e.g. accelerometer, etc) from my android device to my desktop computer.
My android device is connected to a WiFi network and my desktop computer is connected to a network with an Ethernet cable.
There are android applications available that send data from an android device to a PC via WiFi, but both devices must be connected to the same network.
Is there a way to transfer data if the two devices are connected to different networks?
Thank you!
Due to the fact that nearly every network sits behind a firewall (in most cases your home router/nat-device) you need to implement port-forwarding to access network 2 from network 1.
Another aproach would be to set up a VPN (f.i. with SSH) between the 2 hosts.
If your PC is directly connected to the internet, and has a public ip (that means, if you have NOT a RFC 1918 Adress), then you are able to directly connect to that ip by just entering that ip. If not, you need to implement something to get behind the firewall.
Simple answer; to stream data between a PC and an Android device each residing on different networks you need an intermediate server to pass the data through.
Now, if this just a small app that you are doing for your own purpose I suggest using the free version of Firebase (see https://www.firebase.com/). Their sample application that you'll find there, a simple chat service, does pretty much exactly what you need. Simply let the application on your PC (preferably a web app) read the "messages" that you send from your Android device.

Connect Android to another device on bluetooth and send data

I want to create a connection between my Android and a PC (or another device) just like in the Bluetooth menu from settings.
I have manage to get the list of all bt devices in my range and get their MAC addresses.
So, I have the BluetoothDevice, how can i connect to it or pair with it? I saw a lot of stuff about creating a server on PC, but I don't want that, i just wanna connect with the device.
My attempt were to get the UUID of the device and use createRfcommSocketToServiceRecord...but when i try socket.connect() it cannot connect (because I don't have a server). So practically I need to make the same structure that the OS is doing (connect with the device, if the connected device is trying to send i can receive the info and so on).
If I managed to connect with the device, I want to listen all that it is emitting, no matter the data...I haven't researched this topic, but any advice will be nice.
The connection must be made with any device that supports bluetooth, and I don't have access to it (of course the device is giving me the permission to connect).
Grab the Android sample projects by doing this http://developer.android.com/tools/samples/index.html
They have an app called BluetoothChat that pretty much does just what you want.
The thing about the 'server' is just in the connection, one side needs to open a listening port and that side is arbitrarily the 'server'. Once they are connected they are merely peers and you can write/read data till the cows come home.

How do I get my Android app to communicate with the server over a VPN?

I have an android application that talks to a server app running on grails (Groovy on Grails).
Android app basically establishes a connection with this address:
url="http://192.168.2.53:8080/tma/majBtm/androidToDesktop";
It all works fine when both the server and the android phone are connected in the same lan network.
Now the problem arises when I want the phone to talk to the server while the phone is outside the local network (I need it to use edge).
I came across VPN networks which I think might help me out. (the server has dynamic IP)
So what i did was go to dyndns.com and created a new hostname and have the app successfully run on the server. The host I created was - tmagrails.dyndns.com
How do I make my phone connect to this network? What changes am I supposed to make in order to get things working?
Also, I really want to know if this is the right approach. If not, is there any way to get things going for me?
Please advice.
First you need to set up port forwarding so that port 8080 is forwarded to your 192.168.2.53 ip address. A google search on your router model should tell you how to do this. If you want to make your app more secure you will also need to look at SSL and TLS While this won't garantee your app is secure it will stop the network traffic to your app being easily read if you were to access it from an open wifi hotspot.

Categories

Resources