I have read this Bluetooth Chat post, and this Transfer file post.And I have two real android devices ,not AVDs, my
intent is to set IP address and port in one device which acts as a Client while the other acts as a Server.
They are using WIFI,and I have connected both of them to PC respectively.Get into adb shell ,and ping each other.It works.
I have written client code like this:
Socket socket = new Socket("192.168.1.142",8888);
InputStream in = socket.getInputStream();
byte[] buffer = new byte[in.available()];
Toast.makeText(this, String.valueOf(in.available()), Toast.LENGTH_LONG).show();
in.read(buffer);
String msg = new String(buffer);
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
and the Server:
ServerSocket server = new ServerSocket(8888);
while(true) {
Socket client = server.accept();
OutputStream out = client.getOutputStream();
String msg = "Hello Android!";
out.write(msg.getBytes());
client.close();
}
I have add this
<uses-permission android:name="android.permission.INTERNET" /> to manifest.
But no response. I have two questions:
1.Why there is no response in my client?
2.How to handle with sqlite3 database ,there must be something different with ordinary text files,but what is the difference?
Any suggestions will be very appreciated.
It would be unwise to transfer a sqlite3 .db between devices that may be differing make/model/manufacturer/etc. Rather, you should dump the schema and content of database to csv or sql and transfer that. You may want to compress the file too before transfer.
As for networking with Android. If you're using an AVD (emulator) then you're going to find it impossible or near impossible. Your proxy also plays a role in networking so you need to be warey of what it allows, how it's currently configured, and how it behaves (bugs, quirks, features). You should use a tool such as wireshark to inspect network comms and make sure that your App is even sending something out before worrying whether that something gets recieved.
Related
I am trying to receive data (strings) from a Bluetooth enabled device whose MAC_ID is known.
I have searched many examples ,but each article is pointing to Bluetooth Chat example, I think in Bluetooth Chat example, application need to be installed on both the devices for them to be connected and exchange strings.Correct me if I am wrong.
But I need to install application only on Receiver device.I have tried installing the application only on one device and tried connecting to the sender device, without success.
Bluetooth is a peer to peer protocol where you need to have application running on both sides. Hence if you want to exchange data very good example would be Bluetooth chat. If you want to download or transfer a file you should either implement obex or FTP profile based applications.
Yes you need to deploy an application on both sides. If you are really restricted in a way that you only can deploy on one side, you have to figure out which standard protocols/bluetooth profiles the other side is capable of. You can figure that out by doing a SDP lookup. For a device, you will then get a list of UUIDs identifying these services. See the bluetooth spec for well known UUIDs. As #7383 pointed out, your are most probably looking for OBEX or FTP.
If you can deploy on both sides, you can write your own app using Blaubot (disclaimer: I wrote it). A simple Blaubot program would do this:
UUID MY_UUID = UUID.fromString("33bb1246-1472-11e5-b60b-1697f925ec7b");
// onCreate() or in a service, we create a blaubot instance
// using Bluetooth to form a network and Bluetooth + NFC to find devices
IBlaubotDevice ownDevice = new BlaubotDevice();
BlaubotUUIDSet uuidSet = new BlaubotUUIDSet(MY_UUID);
BlaubotBluetoothAdapter bluetoothAdapter = new BlaubotBluetoothAdapter(uuidSet, ownDevice);
BlaubotNFCBeacon nfcBeacon = new BlaubotNFCBeacon();
BlaubotBluetoothBeacon bluetoothBeacon = new BlaubotBluetoothBeacon();
this.mBlaubot = BlaubotAndroidFactory.createBlaubot(MY_UUID, ownDevice, adapter, nfcBeacon, bluetoothBeacon);
// start and wait until connected
this.mBlaubot.startBlaubot();
// create a channel and send your file
IBlaubotChannel fileChannel = this.mBlaubot.createChannel(1);
// convert your file to its bytes
File yourFile = // ... however you get it
byte[] fileBytes = ...// ... see http://stackoverflow.com/questions/858980/file-to-byte-in-java
// send it to all connected devices
fileChannel.publish(fileBytes, true);
// to receive it on the other device, do this:
// subscribe to the channel
fileChannel.subscribe(new IBlaubotMessageListener() {
#Override
public void onMessage(BlaubotMessage message) {
// extract your bytes from the message
byte[] fileBytes = message.getPayload();
// .. do something useful or write it to a file again
// to write it to a file
File file = new File(yourFilePath);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
bos.write(fileBytes);
bos.flush();
bos.close();
}
});
This should be all you need. To allow the devices to connect, you have to pair them before or use NFC (just hold them together, when Blaubot is started). If you go with Blaubot let me know if you have problems that can't be solved with the documentation or the android quickstart guide.
I can only guess what your actual scenario looks like. If you have two android phones, this should work. If that is not the case, you should add more informations about the involved devices. Are we really talking about (classic) Bluetooth connections or are you trying to get data from a Bluetooth Low Energy device?
In this case the famous chat example will not help you either.
All the questions here point to classes of the same app or different apps in separate processes yet in the same device. I would like to send data to and from two separate apps in two separate devices. I tried using broadcastreceiver but it didn't work. Here is my snippet to send the data.
addressstring = String.valueOf(acrilocation.getText());
if (addressstring != null && addressstring.length() > 0){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Constants.LOCATION_DATA_EXTRA, addressstring);
intent.setType("text/plain");
sendBroadcast(intent);
} else{
Toast.makeText(getApplicationContext(), "Enter valid location address", Toast.LENGTH_SHORT).show();
}
but when I receive the data in my other app using the following code snippet, It fails. When I debug the app I get null exception.
Intent intent = getIntent();
String action = intent.getAction();
String data = intent.getStringExtra(Intent.EXTRA_INTENT);
String type = intent.getType();
useraddress.setText(data);
startActivity(intent);
Is there another way to achieve this? I mean to send data to and from another app which is installed in another device?
Connecting over networks that accept incoming socket connections
The usual way to do this between Android devices (or between any peer devices) is to use sockets.
You set up one or both devices to 'listen' for connections on a socket and then accept a connection from the other when they want to communicate (or you can have a dedicated client and server and the client always initiates the connections).
Once the connection is established you can send messages back and forth.
There are many examples of Android client server socket applications, but one I found useful was:
Android Server/Client example - client side using Socket (and its companion server side blog article - link included in the client blog)
Note that you may have to add your own 'protocol' on top of this - for example if you are sending a file of unknown length without any special 'end' character, you may want to add a byte (or several byte to represent an int, long etc) at the start to indicate the length of the transmission so the receiving side knows when it has received everything (or that it has not received everything in case of an error).
Connecting over networks which do not allow incoming connections (e.g. most 3G/4G)
In these scenarios, while there is nothing theoretically stopping sockets working, in practice many mobile operators will not allow incoming socket connections. In addition you would need to find the public IP address of the Mobile, which is possible but is extra complexity. If your solution will only ever run on a single operators network you can experiment and see if it works, but if not you may find it better and easier to use a server in the 'middle':
Device A connectes to server
Device B connectes to server
Device A asks server for addresses of connected devices and 'discovers' device B
Device A send a message for device B. It actually sends the messages to the server with an indication that it is to be sent to device B
The server notifies device B that a message is available for it (using some sort of message notification like Google Cloud Messaging for example, or simply by the devices polling regularly to see if they have any messages).
Device B retrieves the messages from the server
The above will work on pretty much any network that allows connectivity to the internet. It does have the disadvantage of requiring a server but it is likely a necessary approach over most mobile networks.
If you want the two instances of your Android app on two different devices located on the different parts of the world to communicate with each other directly without the server, then the best way to do it is to use Tor Hidden Services. Tor Hidden Services allow the apps to bypass the firewall or NAT (if Tor is not blocked, of course), and the devices can easily communicate with each other without the need for a central server. Here, I will try to give some code examples that you can try. The best library suitable to this stuff is this.
Step 1: Add dependencies to your gradle.build in app module:
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
dependencies {
compile 'com.github.jehy:Tor-Onion-Proxy-Library:0.0.7'
compile 'org.slf4j:slf4j-api:1.7.7'
compile 'org.slf4j:slf4j-android:1.7.7'
}
Step 2: Add permissions (Internet permissions or whatever) to your manifest file.
Step 3(i): Now we will just write the classic Client-Server programs in Java but with added Android and Tor flavor. To test this properly, try creating two different apps. One app will be the server and the other app will be a client. Preferably, you can even install the two apps on different phones.
In this example, we will try to send "Hello from Tor client" string from client app to server app.
For the server side: You can try this function inside any Activity and AsyncTask.
void server(Context context){
//For comments and documentation, visit the original repo
//https://github.com/thaliproject/Tor_Onion_Proxy_Library
String fileStorageLocation = "hiddenservicemanager";;
com.msopentech.thali.toronionproxy.OnionProxyManager onionProxyManager =
new com.msopentech.thali.android.toronionproxy.AndroidOnionProxyManager(context, fileStorageLocation);
int totalSecondsPerTorStartup = 4 * 60;
int totalTriesPerTorStartup = 5;
try {
boolean ok = onionProxyManager.startWithRepeat(totalSecondsPerTorStartup, totalTriesPerTorStartup);
if (!ok)
System.out.println("Couldn't start tor");
while (!onionProxyManager.isRunning())
Thread.sleep(90);
System.out.println("Tor initialized on port " + onionProxyManager.getIPv4LocalHostSocksPort());
int hiddenServicePort = 8080;
int localPort = 9343;
String onionAddress = onionProxyManager.publishHiddenService(hiddenServicePort, localPort);
System.out.println("Tor onion address of the server is: "+onionAddress);
ServerSocket serverSocket = new ServerSocket(localPort);
while(true) {
System.out.println("Waiting for client request");
Socket receivedSocket = serverSocket.accept();
ObjectInputStream ois = new ObjectInputStream(receivedSocket.getInputStream());
String message = (String) ois.readObject();
//Here we will print the message received from the client to the console.
/*You may want to modify this function to display the received
string in your View.*/
System.out.println("Message Received: " + message);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
Step 3(ii): For the client side try this function
//Inputs:
//'String onionAddress' should be the one obtained in server() function.
//It will be printed in the console and it will possibly remain the same
//even if the app restarts, because all the data/cache will be stored locally.
//Also, when you run the code for the first time, Tor will take about 1 or 2 mins
//to bootstrap. In the subsequent runs, Tor will start relatively faster as the
//data will be cached. 'int hiddenServicePort' is the port at which the hidden
//service has started on the server. In our example code, it is 8080. So, pass that here
void client(Context context, String onionAddress, int hiddenServicePort){
String fileStorageLocation = "clientmanager";
com.msopentech.thali.toronionproxy.OnionProxyManager onionProxyManager =
new com.msopentech.thali.android.toronionproxy.AndroidOnionProxyManager(context, fileStorageLocation);
int totalSecondsPerTorStartup = 4 * 60;
int totalTriesPerTorStartup = 5;
try {
boolean ok = onionProxyManager.startWithRepeat(totalSecondsPerTorStartup, totalTriesPerTorStartup);
int socksPort=onionProxyManager.getIPv4LocalHostSocksPort();
if (!ok)
System.out.println("Couldn't start tor in client");
while (!onionProxyManager.isRunning())
Thread.sleep(90);
System.out.println("Client Tor initialized on port " + socksPort);
System.out.println("Client is waiting for the server to get ready");
Thread.sleep(2000);
Socket clientSocket =
Utilities.socks4aSocketConnection(onionAddress, hiddenServicePort, "127.0.0.1", socksPort);
ObjectOutputStream oos = new ObjectOutputStream(clientSocket.getOutputStream());
oos.writeObject("Hello from Tor client\n");
System.out.println("Client has sent the message");
oos.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
It's done. Run your apps and test it. If you get stuck, try consulting here.
So, now your apps can communicate without any central server. Tor Hidden Services are so awesome in these use cases.
You can also use IP6, then you can do a direct socket connection from one phone to the another. I got latency as low as 60ms between two phones on different 4G operators (in the same country though). Note that you have to send some data to avoid getting down switch to lower speed to get such low latency. 10 concurrent ping was enough for me.
The listen side doesn't need any change at all, the client side just has to use an IP6-address:
s = new Socket("2a10:811:21c:22a1:7683:ae1:18c7:9827", 9343);
IP6 seems to be supported by many operators. If not, tor can be a good fallback, if latency isn't a problem.
what is the best approach of connecting android to server and ip and corresponding port? THis connection doesn't need to be all the time, but I am assuming I will send and recive files (in byte arrays or streams).
Thanks
Since the Android Development Tools are native to Java, you can use simple Java Socket APIs to accomplish this goal (see ServerSocket and Socket).
Server Code
You must start by opening a ServerSocket on your host computer by defining a port to listen on:
ServerSocket ss = new ServerSocket([some_port]);
Then you must begin listening for a client by calling ss.accept(). This method will block until a client connects:
Socket my_socket = ss.accept();
You now have a socket on your server that you can manipulate as you wish (probably through the use of ObjectInputStream and ObjectOutputStream):
ObjectOutputStream out = new ObjectOutputStream(my_socket.getOutputStream());
ObjectInputStream in= new ObjectInputStream(my_socket.getInputStream());
Client Code
You must establish a connection with the server that you have just created. You will do this by initializing a socket and passing in the IP address of your server (usually localhost for most testing purposes) and the port number on which your server is currently listening:
Socket socket = new Socket("localhost", [some_port]);
Again, establish some streams for communication:
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
And there you have it! You can now easily communicate with a server from an Android device. It is much simpler than you would think.
Please note, however, that this architecture implements TCP, which is much slower than UDP and will not work for any type of fast-paced data intensive games, but should accomplish your goals given your description above.
I'm trying to send data from my Android app to my PC over TCP.
The code is as follows:
Socket socket = new Socket("10.0.78.75", 50505);
OutputStream out = socket.getOutputStream();
PrintWriter output = new PrintWriter(out);
mStatusText.setText("Sending Data to PC");
output.println("Hello from Android");
mStatusText.setText("Data sent to PC");
socket.close();
mStatusText.setText("Socket closed");
I don't get any errors at all while doing this, however, the server application (written in C#) does not get any data.
It sees the client connect to it, and sees that data is being sent, however, the data string comes out empty... And thoughts on why this is happening?
PS: The server code is copied from the following site and has been tested with a C# TCP client.
http://www.switchonthecode.com/tutorials/csharp-tutorial-simple-threaded-tcp-server
Try putting an out.flush();out.close(); after the println(..);
I had the same problem and Haphazard's solutions wasn't good enough for me. I think that you should use (in this case) output.flush(); and output.close(); instead of out.flush(); and out.close();. And you have to remember about internet permission in AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
after <uses-sdk> and before <application>
Total guess this one, but have you tried calling flush() on the output stream before closing?
in my app i want to receive some message form the server and based on that i want to display pop up message and for this i want to do socket communication in android.
When i am try to read response form the server using socket.getInputstream i will get error
"request time out :Address family not supported by the protocol"
Here is my code.
Socket socket = new Socket("localhost",62000));
boolean isconnect = socket.isConnected();
Log.e("Socket Connection ", String.valueOf(isconnect));
// Read and display the response message sent by server application
//
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
String message = (String) ois.readObject();
System.out.println("Message: " + message);
ois.close();
socket.close();
Unfortunately, this exception is caught and reported by Android, and your app doesn't get to see the stacktrace as far as I know (and it's reported at the debug level).
I'm pretty sure the cause of this exception is that an outside machine is trying to access Android and the port is closed (so the connection is refused).
Make sure:
You have a server running on the right port in Android
You turn on port forwarding for that port (e.g. you can have the service running on port 10000 in the Android emulator, and have your computer's port 20000 forward to that port)
Your client is accessing Android using 0.0.0.0 via the forwarded port (20000, not 10000)
You correctly specify TCP or UDP (might break things if it's the wrong one)
Hope this helps!