I have a problem with android sockets. I am connecting a USB dongle on my PC to an Android phone, a Samsung Galaxy Note II. I am using standard bluetooth sockets creating the socket with createRfcommSocketToServiceRecord.
btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
The server is listening with listenUsingRfcommWithServiceRecord.
tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE,MY_UUID);
The pairing happens fine between the client and the server. I have tried with the PC being the server and and the phone being the client and the other way around. In both cases I get the same exception. The logcat is pasted below.
04-12 06:14:58.229: D/THINBTCLIENT(2613): ON CREATE got remote device6C:F3:73:47:5B:39
04-12 06:14:58.229: D/THINBTCLIENT(2613): ON CREATE Socket created.
04-12 06:15:01.229: E/THINBTCLIENT(2613): socket connect threw an exception.
04-12 06:15:01.229: E/THINBTCLIENT(2613): java.io.IOException: Connection refused
04-12 06:15:01.229: E/THINBTCLIENT(2613): at android.bluetooth.BluetoothSocket.connectNative(Native Method)
04-12 06:15:01.229: E/THINBTCLIENT(2613): at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:204)
04-12 06:15:01.229: E/THINBTCLIENT(2613): at com.example.myfirstapp.taskScreen$7.onClick(taskScreen.java:245)
I googled and someone said that unpairing and pairing again helps. I tried that too and still the exception persists. If anyone has any idea, it will be really appreciated.
Related
I'm making an app in Android studio and I'm getting an IOException which is unpredictable nor have I managed to replicate it.
W/TcpProtocolLayer: Connection to /x.x.x.x:x closed.
W/System.err: java.io.IOException: Software caused connection abort
W/System.err: at sun.nio.ch.FileDispatcherImpl.read0(Native Method)
W/System.err: at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:43)
W/System.err: at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)
W/System.err: at sun.nio.ch.IOUtil.read(IOUtil.java:192)
W/System.err: at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:421)
W/System.err: at x.x.protocol.layer.TcpProtocolLayer.receive(TcpProtocolLayer.java:254)
W/System.err: at x.x.protocol.layer.TcpProtocolLayer.receive(TcpProtocolLayer.java:201)
W/System.err: at x.x.protocol.layer.DatagramProtocolLayer.receive(DatagramProtocolLayer.java:133)
W/System.err: at x.x.protocol.ProtocolStack.receive(ProtocolStack.java:190)
W/System.err: at x.x.protocol.Socket.receive(Socket.java:85)
I've been trying to wrap my head around it and I just can't seem to understand why this would happen, for legal reasons I can't show the code where it's happening but I can describe it.
The phone sends a message to a server and the server responds. This part is always successful and never fails. Then the phone waits for the server to send another message for a confirmation, and this is where it the exception happens. It might throw the exception instantly it might throw it after 10 seconds. I should also say that 90% of the time the code works just fine and no exception is thrown and it processes the information just fine. Then the 10% of the unpredictable attempts this happens and breaks the app completely. I've watched out for it not to be a Async Thread error ,and I've concluded it isn't and the Socket is opened at the moment of this happening. If anyone has any answer I'd be happy to hear it because this problem has been haunting me for a month already and I can't fix it
My app is working fine in AVD but when i try to run my app in my mobile it is showing this error. It is not connecting to server. It is not accessing php file. I have given path for php file is "http://10.0.0.72/myapp/search.php". I'm using wamp server and kept my php file inside www folder.
Logcat Error
java.net.ConnectException: failed to connect to /10.0.0.72 (port 80):
connect failed: ETIMEDOUT (Connection timed out) 06-13 13:19:20.229
13823-14092/com.myapp W/System.err: at
libcore.io.IoBridge.connect(IoBridge.java:114)
Caused by: libcore.io.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
I assume that you run your wamp server in local PC. Connect both your phone and PC (which has the wamp server) to the same network and replace 10.0.0.72 with the private IP of the PC.
Try the url in the phone browser, It should be accessible.
Make sure you have given internet permission in the manifest file.
I am trying to trace a request originating from android and send some data to serversocket at each hop. I am using below code to execute client and send data to ServerSocket at each hop (tomcats having servlet projects) and they are able to send and connect.
However when I try to use same below code in android app running on emulator on same machine. It throws me timeout error.
InetSocketAddress hostAddress = new InetSocketAddress(hostAdress,
SocketServerPORT);
SocketChannel client = SocketChannel.open(hostAddress);
after open execution gives me
java.net.ConnectException: failed to connect to /192.168.0.102 (port 8090): connect failed: ETIMEDOUT (Connection timed out)
01-24 13:25:58.140 3382-3424/app W/System.err: at libcore.io.IoBridge.connect(IoBridge.java:124)
01-24 13:25:58.140 3382-3424/app W/System.err: at libcore.io.IoBridge.connect(IoBridge.java:110)
01-24 13:25:58.140 3382-3424/app W/System.err: .....
what could possibly be wrong here?
my app has
<uses-permission android:name="android.permission.INTERNET" />.
I have also used
Socket socket = new Socket(hostaddress,port)
It throws same exception.
I am new to android development, and have run into a problem I seem unable to solve.
My android app should be able to communicate with my arduino device, sending info
to and retrieving info from my device. I have set up my connection between the two
with a bluetooth device and I am using amarino to facilitate the communication.
Now I can connect with amarino and send data and receive data, now I want to do this from
my app. The problem comes when I enter this line:
private static final String DEVICE_ADDRESS = "07:12:04:16:68:18";
Amarino.connect(this, DEVICE_ADDRESS);
The number is the MAC address of my bluetooth adapter connected to my arduino device.
As soon as I run my app on my phone it just force closes due to unexpected error.
Now as I said, I am a bit of a noob when it comes to android, so I installed logCat to try get some log info out of it. All I got from it was this somewhat unhelpful error message:
com.example.projectgreetv3/.StartingPlace
10-24 15:22:55.140 E/AndroidRuntime(4906): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.projectgreetv3/com.example.projectgreetv3.StartingPlace}: java.lang.ClassNotFoundException: com.example.projectgreetv3.StartingPlace in loader dalvik.system.PathClassLoader[/data/app/com.example.projectgreetv3-1.apk]
10-24 15:22:55.140 E/AndroidRuntime(4906): Caused by: java.lang.ClassNotFoundException: com.example.projectgreetv3.StartingPlace in loader dalvik.system.PathClassLoader[/data/app/com.example.projectgreetv3-1.apk]
10-24 15:22:55.148 W/ActivityManager(539): Force finishing activity com.example.projectgreetv3/.StartingPlace
10-24 15:21:51.726 I/UinboxLogger(4585): [UinboxReceiver] onReceive() >> intent.getData() : com.example.projectgreetv3
10-24 15:21:52.382 I/UninstallAppProgress(4575): Finished uninstalling pkg: com.example.projectgreetv3
10-24 15:22:52.523 D/PackageManager(539): Scanning package com.example.projectgreetv3
I hope someone can help me with this, as I am on wits end.
For more info on:
- Arduino:
- http://www.arduino.cc/
- Amarino:
http://www.amarino-toolkit.net/
If you look a bit further down on your stacktrace (in the logcat) you will probably see that the cause of this crash is an NetworkOnMainThreadException. (If you are running Android 3.0 or higher)
Please make sure that you do all your network communication on a separate thread (Thread/ASyncTask).
http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html
I used apache http client lib in my program. I set both connection timeout and socket timeout for network calls. My understanding is that, if there is connection timeout, the client didn't reach the backend server; if there is socket timeout, the client reached the backend server but the server was not responding or very slow.
I log the following exception trace stack. How can I tell which timeout (connection or socket) caused the exception? Thanks!
java.net.SocketException: The operation timed out
at org.apache.harmony.luni.platform.OSNetworkSystem.connectStreamWithTimeoutSocketImpl(NativeMethod)
at org.apache.harmony.luni.platform.OSNetworkSystem.connectStreamWithTimeoutSocket(OSNetworkSystem.java:130)
at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:247)
at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:534)
at java.net.Socket.connect(Socket.java:1056)
at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:348)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
From the stack trace, you can tell that it timed out while attempting to establish the connection, not while trying to read from the Socket's InputStream.