I've created C# application which is running (almost non-stop) on my windows 7 desktop computer. Now I'm looking for a simple way to tell my desktop application to stop running from my android phone. My initial plan was to have .txt file on my ftp server, so desktop app would check i.e. every hour if .txt file contains command to shut down (pressing a button on android app would change .txt file on ftp server). However even after few hours of tutorials on java I was still unable to figure out working with ftp connection.
What would be easiest way given my lack of java knowledge (I understand I'll have to learn a bit more, but I really don't want to get too deep into java for now)?
The simplest way would be to send a simple udp or tcp message to your windows application.
http://developer.android.com/reference/java/net/DatagramSocket.html
String messageStr="Shutdown!";
int server_port = 8855;
DatagramSocket s = new DatagramSocket();
InetAddress local = InetAddress.getByName("192.168.1.55");
int msg_length=messageStr.length();
byte[] message = messageStr.getBytes();
DatagramPacket p = new DatagramPacket(message, msg_length,local,server_port);
s.send(p);
In your C# Application you simply open a Socket and wait for your packet.
How do I make a UDP Server in C#?
Related
I am in trouble and need your help. I am currently working on a project which requires me to first pick a photo from gallery using an android device (with android studio), extract text and then recognize the text.
I have already achieved the process of extraction and recognizing through Matlab. Now my problem is, how can i transfer a photo which I picked from my android cell phone to MATLAB? How to send the results back to the phone after processing the image?
Please help. A code will be appreciated.
I think the best way to do so will be using a TCP/IP socket. I did a similar project once where I had to receive text from my RPi in MATLAB. Simply connect your Android device and the workstation with MATLAB to the same network and note the IP address of the server(the device on which you will initialize your Server. Preferably make your Android device as the server). Connect the client to the server and read/write the data. Here is an example of what I did. I initialized MATLAB as the client and my other device as Server.
t = tcpip('xxx.xxx.xxx.xx', yyyyy, 'NetworkRole', 'Client'); #Replace xxx.xxx.xxx.xx with IP address and yyyyy with the Port
#Change Client to Server if you want MATLAB to work as server
fopen(t)
dataMat = zeros(1, N);
while 1
data = fread(t); #Change fread to fwrite to transmit data
raw = char(data); ###############################
val = str2num(raw); #### This part converts the ###
val = val'; #### received ASCII values ####
l = length(val); #### to the original data #####
message = 0; ###############################
for i = 1:l
message = val(i)*(10^(l-i)) + message;
end
dataMat(count) = message;
message
end
I'm not sure how to send an image over TCP/IP, but I'm pretty sure it's possible. Click here for TCP/IP with MATLAB blog.
I haven't used Android studio so can't help with TCP/IP there, but if your app is fairly simple, you can create a TCP/IP application with MIT App Inventor(which I have worked on), so can help with that.
Cheers!
in my current project it is a requirement to send a file from a windows computer to an android device over bluetooth without anything on the phone other than it's standard state and of course a paired bluetooth connection. i've looked over pybluez and it seemed simple enough to send files between a client and server architecture (and in fact got it sending between my laptop and desktop rather quickly) but I cannot for the life of me find any way to get python to send a file from the computer to android once the connection is established; my attempts have been grabbing the bluetooth mac address like thing from the device like so
nearby_devices = bluetooth.discover_devices(
duration=8, lookup_names=True, flush_cache=True, lookup_class=False)
and then later trying to send the files like so
port = 1
for addr, name in nearby_devices:
bd_addr = addr
sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((bd_addr, port))
sock.send("download-app")
sock.close()
Of course with the example script given by the pybluez documentation I can seamlessly send files between a client and a server but I am still stuck without a way to send a file to the selected android device (even if I specify it's address and know it's within range)
You're most of the way there...
As you know, you need something to talk to at the other end of your Bluetooth connection. You just need to replace your custom server with a well-known service (generally one of these options).
In my case, my phone supports the "OBEX Object Push" service, so I just need to connect to that and use a suitable client to talk the right protocol. Fortunately, the combination of PyOBEX and PyBluez does the trick here!
The following code (quickly patched together from PyOBEX and PyBluez samples) runs on my Windows 10, Python 2.7 installation and creates a simple text file on the phone.
from bluetooth import *
from PyOBEX.client import Client
import sys
addr = sys.argv[1]
print("Searching for OBEX service on %s" % addr)
service_matches = find_service(name=b'OBEX Object Push\x00', address = addr )
if len(service_matches) == 0:
print("Couldn't find the service.")
sys.exit(0)
first_match = service_matches[0]
port = first_match["port"]
name = first_match["name"]
host = first_match["host"]
print("Connecting to \"%s\" on %s" % (name, host))
client = Client(host, port)
client.connect()
client.put("test.txt", "Hello world\n")
client.disconnect()
Looks like PyOBEX is a pretty minimal package, though, and isn't Python 3 compatible, so you may have a little porting to do if that's a requirement.
I haven't personally explored it but check out this blog -
http://recolog.blogspot.com/2013/07/transferring-files-via-bluetooth-using.html
The author uses the lightblue package as an API for the Obex protocol and send files over the connection. Now the lightblue package appears to be unmaintained. There are other packages like PyObex (which I could not import for whatever reason) which you could also explore as alternatives but lightblue seems to be the way to go.
I have made a Python 3 port of PyOBEX based on the PyOBEX code on bitbucket. I've tested so far only the client functionalities, but I expect the server to be working fine as well, since most of the compatibility issues with Python 3 were due to struct.pack/struct.unpack binary blobs appended to strings that should have all been tackled.
I'm working on a google cardboard project (Unity that will run on android phone)
I created a server in the Unity program that opens like this:
TcpListener listener = new TcpListener(IPAddress.Any, port);
listener.Start();
TcpListener client = listener.AcceptTcpClient();
Everything on backgroundworkers and nice things.
Then I connect from another piece of code running in a PC (WPF C# code). If Im running both programs in the PC I can connect flawlessly but when I compile for android and move the server to the phone, I cannot connect to it from the PC
When I try to perform:
host = Dns.GetHostEntry("192.168.1.8");
I get: Unknown host
The IP is correct (I made the server in the phone show it on screen, and I can ping to it) I think the problem is more related to the thing being in a different machine than to the fat that it is compiled for android but nevertheless.. here you have the full story.
Any help ?
Not sure what it was becauseI moved to UDP messages, and now it works.
Hi Guy's I'm new to programming Android device's I do have python, java, C#, C, C++, PHP, Bash and Visual Basic Experience but I'm new to this block programming, and I haven't done much work with forms. I'm trying to get make an application that posts data to an external IP. I have successsfully wrote a server and a windows based client, clicking buttons in my windows client posts data to the ip 192.168.1.9 port 9999. This is just in the testing phase to remote control a bunch of beaglebone gpio's. So far I've had great success with the windows side. In app inventor for android, However, I've created a series of buttons and tabs, different buttons post text or post and poll for response. The problem I have is that I can directly attach the web connector to 192.168.1.9 but when I add in the port 9999 it tells me the address is incorrect. The method I'm using is
when Screen1.initialize
do set Web1.Url to "http://192.168.1.9:9999"
when Button1.Click
do call Web1.PostText
text > 0
Again, if I type in just the IP of the beaglebone I see its ethernet port go crazy when I click button1. It does nothing when I add in the port. Of course my server is running on 9999 since port 80 is reserved for the internet. Any suggestions?
I would like to suggest you a two-step solution.
Step 1:
Problem>>Develop an android app which is capable to communicate via TCP-IP.
Solution>> I hope you are familiar with MIT-APP Inventor-2. Import an extension called ClientSocket extension V0.4.3 available here to app. Thanks to the developer of the extension.
Step 2:
Problem>>A server responding client request.
Solution>> I have written a Python code.
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 9000 # Reserve a port for your service.
s.bind((host, port)) # Bind to the port
print (host)
s.listen(5)
while True:
c, addr = s.accept() # Establish connection with client.
data=(str(c.recv(1024)))
print data
conn.commit()
c.close()
cur.close()
Hope this will help.
i'm developing a small example about socket connection between android and ios (via wifi), after trying, the connection hasn't been established. Here is what I have done so far, I created a server on ios (used Bonjour to publish the service). I also created a client on android. However, after starting the server on ios, I also got the log:
ServerSocketConnection[3487:c07] Bonjour Service Published: domain(local.) type(_serversocket._tcp.) name(Macmini) port(54065)
Which means the server starting ok.
To the client part(android), I created the client through Socket class, some few lines of code:
Socket s = new Socket("local.", 54065);
OutputStream out = s.getOutputStream();
PrintWriter output = new PrintWriter(out);
output.println("Hello Android!");
BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
String st = input.readLine();
Putting it into AsyncTask to execute. However, I got the UnknowHostException:
08-06 12:45:44.460: W/System.err(873): java.net.UnknownHostException: Unable to resolve host "local.": No address associated with hostname
I'm a newbie to this kind of problem so any ideas what the problem is? I know it's related to the "host" thing but need the way to fix it.
*Note: I run 2 apps on 2 simulators (ios and android) as the same wifi network and same MAC, maybe this is the problem? any help would be appreciated and sorry for my English, it's not my native one.
Use the actual IP address of the iPhone instead of 'local.'.
On Android you can find a phone's IP via Settings -> WiFi -> Advanced. Not sure if iPhone offers the same option.
ps. Also be sure to have the internet permission in your manifest;
<uses-permission android:name="android.permission.INTERNET"></uses-permission>