I was wondering what the simplest program for sending an object from my Android phone to my computer wirelessly (via LAN) would be. I have created Java RMI programs with a server and multiple clients, so I have a grasp of the concept. However with android I'm just not sure where to start.
What I am aiming to do is send some sort of information (could simply be text) to my computer and my computer will do an action. I have the GUI interface's and the actions to be carried out all worked out, just the sending of some sort of information is getting me.
Could anyone help me out?
I would say it's not much different from sending data between regular computers. Basically you have the same options. Unless you have some special requirements, the most straight forward solution would be to just set up a ordinary server / socket.
A tutorial on the basics (including example code): http://www.ryerson.ca/~dgrimsha/courses/cps841/serverSockets.html
Related
I am trying to do some wacky home automation which will require me to send a signal from my Android phone to my Pi 3 in order to execute a script to control a motor using the GPIO pins.
The only part I'm stumped on is the best way to connect the Android and Pi.
I've read so many different things and it's all overwhelming, the amount of differing answers I've seen.
So far I'm leaning towards using Jsch in my app to ssh into the Pi and execute the command, but I have been told this is silly.
Can anybody explain to me why this is a bad idea and explain to me a better one? Ideally the phone app would be able to connect over both local network and other networks.
Nothing is wrong with SSH, but people typically use web servers on the Pi plus HTTP requests on the Android side. Or you can run your own protocol via a raw socket connection.
SSH commands might be more secure if you use SSH keys.
Otherwise, you'd be exposing your commands to anyone snooping on your internet traffic, and random people will be controlling your devices
Either way, if you want access both internal and external to home, you can do more research to see if you can "port forward" your router
The advantage of using HTTP for this sort of thing is that it potentially creates its own user interface. What I mean by this is that, if the interface on the Pi is a Web server, then you have a way to provide an HTML/JavaScript interface to your Android device, and thus avoid the need to create an Android app at all -- the user just needs a browser. The whole user interface is managed from the Pi.
I've used this approach for motor control on the Pi a fair bit. On the Pi I use a C program that embeds the libmicrohttpd webserver engine. The program can serve out ordinary HTML pages to create the user interface on the browser, or respond to particular HTTP GET requests that result from the user clicking buttons or manipulating sliders or whatever. You can do some really sophisticated stuff by sending JavaScript functions that make their HTTP requests outside the normal HTTP request/response flow, so you can (for example) have a browser display that updates dynamically (e.g., display sensor values from the Pi).
Moreover, it's easy-ish to provide some kind of security using SSL and HTTP authentication. I prefer C, but there are webserver libraries for Python that work on the Pi as well.
To my way of thinking, the only time it's worth considering something more complex than this is when you need a user interface on Android that can't be implemented in HTML/JavaScript.
I'm sure there are many different ways to do what you want. I prefer to do most of the work on the Pi, because I find writing Android apps deeply unrewarding. On the other hand, if you like developing for Android and have plenty of experience doing so, the approach you suggested -- sending commands to the Pi over SSH -- could work perfectly well. It would just mean doing most of the work in Android.
Yes, ssh is silly solution. I suggest to develop rest api webservice, host it on your PI and invoke it from your Android app
I'm totally new to network sockets so any help is appreciated.
I'd like to know how to setup a network socket on an Android device and use it to pass data generated by an App to another App on the device.
This is because the App receiving the Data will at some point have to be able to receive data from an external source, so I'd like to build in the basics of this before I get to that point.
Any example code etc. would be great and I'll edit my question as I go if required.
I don't think using sockets can solve your problem. Only one app can be active at a given time, and the connection won't work. Unless you create some sort of service, but this approach seems wrong.
I believe you have 2 basic options in front of you:
A server that both apps communicate with.
Writing information to the disk in a globally readable place.
The server option is better IMO.
Im a little new to the android development, I believe I have the basics down but I am wondering the best way to communicate between two phones running the same app. I am looking for something that would be close to instant. For an example, if you sent a message or somekind of variable or string it would appear on the other phones app providing the app was open on both phones. Would be great if I could be pointed in the correct direction here, Thank you!
Assuming you have access to a server that both devices can connect to, the best way to handle this is to set up a socket and have both devices connect to it. That way the messages can be send back and forth and be pushed through immediately (rather than the devices polling for any new thing to do intermittently).
You can learn more about using sockets on android here: http://developer.android.com/reference/java/net/Socket.html
If you're unfamiliar with how to write a server socket, you can write something that works somewhat similarly with a system that just has each device leave a message and have the other device come looking for it, but as I said, that is much less "real time" since then you have to have the devices constantly pestering the server to see if there's anything new to do.
There are also kludgy ways to create a fake socket behavior whereby the http connection never closes and you just keep sending data down the pipe, but if you can avoid it and just use a socket, you should.
I have a task to integrate a Bluetooth device into my application. Now my requirement is very specific. The device has a specific communication protocol which relies on certain ACKs but I figured it would make my development much easier if there was a program that let me test communication with the device.
I was wondering if there was a program for Linux, or perhaps a simple Android program which let me communicate with any Bluetooth device with a series of pings using data I enter and simply log the responses.
EDIT: I might not have been clear enough in my question.
I have a scale that I need to integrate into my application, and the scale has a protocol similar to this:
Get a specific byte string from device
Transfer data
Transfer packet for disconnection
Receive acknowledgement for disconnect packet
Disconnect
Now when I have to issue a POST request from my application, I usually build a test script online so I can test if the POST works properly.
I was wondering if there was something similar I could do with the device.
Thanks
Your question is not clear enough. To communicate with a BT device, you need to know what profile your device supports. Depending on that, you can find a way in Android or Linux or windows to communicate with the device.
The simplest way is to open an RFCOMM channel from android/PC and transfer data to and from the device. For this the device has to support the serial port profile (SPP). If you want to do this in Android, look for the BluetoothChat example from google.
If you want to use a PC/mac/linux look at the Bluez python module. It's really simple to use. There are plenty of other options too..
I was looking for something like SENA BTerm. It lets me connect to any device and send whatever data I wish.
It is an extremely useful tool for testing my code.
http://www.sena.com/download/manual_bterm/overview.html is where you can find it.
I created a really simple Java program that creates a java.net.ServerSocket on an open port, x, and waits for a connection using accept (). I want to use telnet/ssh to connect to the program via port x so that I can communicate with said program. My problem is after I connect to the port, my program recognizes and accepts the connection, but ssh freezes. I'm assuming this is because I don't have the proper response. I just wanna know what I should be doing next. How do I respond?
I could use a library, but I'd like to understand what should come next. However, I would still really appreciate someone telling me if I really should be using a library. Also, I know some basic networking concepts like TCP/IP, OSI, and that data gets wrapped, sent, then unwrapped, but that's the extent of my networking knowledge.
SSH is a very specific protocol, involving encryption. Do you not just want to telnet to the socket, which uses plain-text by default?
ssh is probably much too complicated for what you want to achieve. It freezes because it waits for a response from your server that it probably never gets. ssh has also complicated security requirements that you probably don't want to implement for your simple server.
Why don't you start with something very basic on the client side as in this tutorial. From there on, you can still add features and functionality as needed.
Edit:
Sending commands with telnet:
Client Side:
Just issue telnet host port, there you type say Hello World and hit Enter.
Server side:
You receive a stream of bytes from your client. First you have to parse it as a String. Then you could simply split this String by looking for the first whitespace character. The string before that is your 'command', the part after the whitespace is your 'payload'.
In the example this would give you 'say' as the command and 'Hello World' as the payload.
Then compare the 'command' with a list of known commands, and based on what command you have you can then execute it with the payload as an argument.
Unless you are an genius at cryptography and networks, I doubt you can implement an SSH server. I don't know what your exact requirements are but you may take a look at this: http://www.sshtools.com/en/maverick-sshd/ It's a sshd in JAVA(but I haven't used it personally, so can't tell you much)