Connection between android and PC bluetooth applications - java

I am working on a bluetooth application which involves connection of my android bluetooth application with a server running on computer. The server is also a bluetooth application made using blue-cove api.
The problem i am facing now that i am unable to make connection between my mobile app and that computer server application.
here is my code for android app:
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mySocket = myDevice.createRfcommSocketToServiceRecord(MY_UUID);
mySocket.connect();
toast = Toast.makeText(context, "Connection established", thisClass.duration);
toast.show();
} catch (IOException connectException) {
// Unable to connect; close the socket and get out
try {
mySocket.close();
toast = Toast.makeText(context, "Connection not established", thisClass.duration);
toast.show();
} catch (IOException closeException) { }
return;
}
Where is the problem or what possibly i am missing in it. And also i am having ambiguity in the understanding of socket.connect() method. Kindly help me in this regard.

I just finished working on a similar application and had problems connecting. It would help to see a bit more of your code but the excerpt above looks like the example I was following. I would start by checking that the MY_UUID exists on the PC that you are trying to connect to.
I have posted my example, both the client and server, on my blog at http://digitalhacksblog.blogspot.com/2012/05/android-example-bluetooth-simple-spp.html.
Hope this helps.

Related

How To Improve SQL Connection

Hello every body I'm working on application which is sends data from android app to MS-SQL server and main windows software in C#, which is receives data from MS-SQl server.
The problem is the programs takes too time to build a connection especially in android app some times it crash the app.
By the way the Internet speed some times goes week in our country.
I searched for a solution but not found in internet and I cannot figure out any way to solve it.
And I see the Viber, Watsapp, Massenger ... etc it sends data instantly or synchronously even if Internet speed is week.
So can I get some help and suggestion.
And there is a connection Helper method :
public Connection connections(){
IP="www.examlple.net";
DB="DB_test";
DBUserName="admin";
DBPassword="*****";
StrictMode.ThreadPolicy policy= new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection connection=null;
String connectionURL=null;
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
connectionURL ="jdbc:jtds:sqlserver://"+IP+";DatabaseName="+DB+";integratedSecurity=true;user="+DBUserName+";password="+DBPassword;
connection = DriverManager.getConnection(connectionURL);
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
return connection;
}
For the sake of completeness, with JTDS you can set both a loginTimeout and a socketTimeout on a connection string. Refer to the remarks on these here.
But as others have said, you should go through a web API of some sort. Do you really want to expose your SQL server to the internet?
Also, I just noticed you have specified integrated security=true, and you have also specified a username and password. You can't do that. One is for windows auth (integrated security) and the other is for SQL auth (user and password). You would have to use a username and password. But don't. Don't do this. go through a web API.

Java game socket network router issue

Good day,
I have a java game that I want to play with a friend over network, I have implemented Sockets and tested the game on my pc using localhost as address, but was unable to connect to the external ip of my pal's pc, presumably due to us both being behind routers.
Here is the code of host/client:
CLIENT:
try {
socket = new Socket(inputHostIp(), 5555);
} catch (IOException e) {
e.printStackTrace();
}
SERVER:
try {
hostServer = new ServerSocket(5555);
} catch (IOException e1) {
e1.printStackTrace();
}
listenForUserConnection();
while (true) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
}
Socket socket = null;
try {
socket = hostServer.accept();
} catch (IOException e) {
e.printStackTrace();
continue;
}
joined(socket);
}
The exception I am getting now is
java.net.ConnectException: Connection timed out: connect
On trying to init I/O:
java.net.SocketException: Socket is not connected
java.net.Socket.getInputStream/java.net.Socket.getOutputStream
I have set up port forwarding with the chosen port number (5555) linked to the internal ip on both our machines.
What are my options for getting this to work?
ADDENDUM:
We have also tried using Hamachi to create a virtual LAN, but there seems to be an issue with that - we can’t ping one another even through that, it diagnoses with an issue -
Tunnel:
VPN domain's tap device is down
Local results:
Adapter configuration:
Cannot get adapter config
Traffic test: Cannot complete test
Peer results: [160-056-951]
Adapter configuration: OK
Traffic test: Inbound traffic blocked, check firewall settings
I have tried shutting down firewalls, hamachi issues changed to just ‘cannot get adapter config’, but otherwise no results.
On my pc, however, I got a version of windows that doesn’t seem to display Firewall setting properly, if you think it’s likely an issue, can you tip me on how to test my firewall?

SocketException, connect reset by peer. Weird behaviour when heartbeating server

Working on a client-server android application. The clients are polling the bufferedinputstream (in) for server messages, meanwhile also repeatedly checking if server is reachable so they can terminate instantly when server goes offline.
try {
while (true) {
if (MainActivity.in.available() > 0) {
message = MainActivity.readInput();
break;
}
Thread.sleep(100);
}
} catch (IOException e) {Log.e("myoutput", e.toString()); serverOffline = true; } //server probably offline?
catch (InterruptedException e2) {}
On one of my test devices (android 2.2) this works as I thought, as soon as I close down the server the IOException is triggered. Although on my second test device (android 4.3) the IOException is not triggered (compling with API 8). But I'm starting to think this is not a device thing as I tried running both API 8 and 18 in the emulator and it didn't trigger.
Any ideas?
I'm probably not providing all essential information to get any help from here, please let me know if so and I'll provide!

BluetoothSocket accept() IOException

I do only have this problem with an outgoing connection. I coppied most parts of my sourcecode from the example http://developer.android.com/guide/topics/wireless/bluetooth.html
Therefore I don´t really know whats the cause of this problem is...
First of all the code where the IOException occures:
try {
// This is a blocking call and will only return on a
// successful connection or an exception
socket = mmServerSocket.accept();
} catch (IOException e) {
Log.e(TAG, "Socket Type: " + mSocketType + "accept() failed", e);
break;
}
Every time I connect my phone to my tablet accept() throws an IOException on my phone:
BluetoothChatService Socket Type: nullaccept() failed
BluetoothChatService java.io.IOException: Operation Canceled
I believe this is somehow related to the problem that is described in this blog:
http://www.androiddiscuss.com/1-android-discuss/86844.html
By the way the BluetoothConnection works well, but my phone can´t receive any data.
Edit: I have just realized that a bluetooth connection from my Galaxy S to my Motorola Xoom could not be established with other popular bluetooth apps from the market...
If I connect the other way round, from my tablet to my phone, the tablet just reboots completely.
It could be an authentication issue. Are your devices paired?

Android Sockets - Client side IOException

I'm trying to connect to a simple Java server on my computer (in the future a true server, but I'm just learning how to program with sockets first. When I try to connect, the application on the phone throws an IOException. However, on the emulator, it does NOT.
I do have:
< uses-permission android:name="android.permission.INTERNET"/>
included in the manifest. And here's the code block that's executed when I hit open:
try {
responseField.setText("Opening socket...");
Socket socket = new Socket(getIP(),Integer.parseInt(getPort()));
responseField.setText("Socket opened. Initializing out...");
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
responseField.setText("Done. Initializing in...");
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
responseField.setText("Done.");
} catch (NumberFormatException e1) {
responseField.setText("NumberFormatException");
} catch (UnknownHostException e1) {
responseField.setText("UnknownHostException");
} catch (IOException e1) {
responseField.setText("IOException");
}
Are you making sure that the server end uses a ServerSocket and uses the ServerSocker.accept() method?
So it seems that a weak Wi-Fi signal is causing error. I tried to surf the web (Google, CNN, etc.) afterward, and I could not. So I will just have to test on the emulator for now, or in a stronger signal. Thanks
If you were able to connect to the web before (I am assuming) but not after, then its not a problem with the wifi strength. Also depending on place you are surfing, the wifi router may have been configured not to allow such connections. Try to ping your server IP using a different computer within the same network and see whether you can ping. Emulator will work since the server is running on the localhost.

Categories