Im using a bluetooth printer which i connect to my device using BlueToothSocket, etc. I've been successful with most of the devices but as of this week I have a new device which i just cant get the printer to connect to (open the socket that is, since it's successfully paired). I've used this two methods (used first one with both secure and insecure way):
mmSocket = device.createRfcommSocketToServiceRecord(uuid);
AND
m = device.getClass().getMethod("createRfcommSocket",new Class[] { int.class });
mmSocket = (BluetoothSocket) m.invoke(device, Integer.valueOf(1));
Followed by mmSocket.connect();
The problem is, that neither are working and the error shown is the one stated at the question: java.io.IOException: bt socket connect failed
What'd be causing this? I've tried this on a HTC ONE, a Sony Xperia, a Galaxy Tablet, Motorola phone etc.. But now im using this ACUBE 7 Phablet and it's not working. I have a feeling it's got to be something on the device build that's causing it to fail but cant figure out which way to work something out. Any ideas? Are there any other methods used to initiate a Bluetooth connection programmatically between paired devices?
Related
Problem
How can I differentiate between being unable to establish a Bluetooth connection with a remote Android device because:
scenario 1: the remote device is out of range, or its Bluetooth is disabled.
scenario 2: the remote device is in range, but there is no server socket on the remote device to accept my connection.
What I've tried
I could not differentiate between exceptions thrown when connecting because it threw the identical exceptions in both cases:
java.io.IOException: read failed, socket might closed or timeout, read ret -1
I could not use fetchUuidsWithSdp() to check if my UUID supported by the remote device because it behaves the same way in either scenario...according to the documentation:
This API is asynchronous and {#link #ACTION_UUID} intent is sent, with the UUIDs supported by the remote end. If there is an error in getting the SDP records or if the process takes a long time, {#link #ACTION_UUID} intent is sent with the UUIDs that is currently present in the cache...
it's behaviour also seems sort of unpredictable according to this SO thread.
lastly, I didn't want to use sdpSearch to differentiate between the two, because that was added in API 23, and I want to be able to support down to API 19.
You can determine if a device is in range by trying to connect to a standard UUID that is usually available on Android devices. If the call to connect:
fails, then remote device is out of range or its Bluetooth is disabled.
succeeds, then the remote device is in range and you should close the connection and then try to connect to your app's UUID...if that fails, then there was no listening socket...if it succeeds, then all is well.
Example code:
public BluetoothSocket connect(BluetoothDevice remoteDevice) throws IOException
{
OPP_UUID = UUID.fromString("00001105-0000-1000-8000-00805f9b34fb");
// check if remote device is in range...throw exception if out of range
try
{
BluetoothSocket socket = remoteDevice
.createRfcommSocketToServiceRecord(OPP_UUID);
socket.connect();
socket.close();
}
catch(IOException ex)
{
throw new IOException("out of range",ex);
}
// try to connect to service on remote device...throw exception if UUID
// is not available
try
{
BluetoothSocket socket = remoteDevice
.createRfcommSocketToServiceRecord(MY_UUID);
socket.connect();
return socket;
}
catch(IOException ex)
{
throw new IOException("no listening server socket",ex);
}
}
I used BluetoothDevice.getUuids() to get the UUIDs available on one of my Android devices. It gave me this list:
0000110a-0000-1000-8000-00805f9b34fb - Advanced Audio Distribution Profile (0x110a)
00001105-0000-1000-8000-00805f9b34fb - Object Push Profile (0x1105) // least invasive one...it seems
00001115-0000-1000-8000-00805f9b34fb - Personal Area Networking Profile (0x1115)
0000112f-0000-1000-8000-00805f9b34fb - Phonebook Access (0x112f) // this works!
00001112-0000-1000-8000-00805f9b34fb - Headset - Audio Gateway (0x1112)
0000111f-0000-1000-8000-00805f9b34fb - Handsfree Audio Gateway (0x111f)
00001132-0000-1000-8000-00805f9b34fb - Message Access Profile (0x1132) // this works too!
00000000-0000-1000-8000-00805f9b34fb - Base UUID (0x0000) // couldn't get this to work :(
Standard UUIDs from Bluetooth spec.
I have a ESP8266 chip which is connected to the microcircuit. When chip gets value "200" the light is starting to blink 4 times and than it returns "100" value. I need to make an Android app using Java which will connect to the ESP8266 chip, send data to it and will get value "100". I don't know what library I should use to deal with it. Please, help me, how can I do that? I think it is not the most hard question here.
For your Controller you dont need any Libary. You just can use the serial AT Commands: http://www.electrodragon.com/w/ESP8266
After setting up your ESP like this:
In your App you should deal with TCP-Sockets: https://de.wikibooks.org/wiki/Googles_Android/_TCP-Sockets
Try something like this in an async task:
socket = new Socket();
socket.connect(new InetSocketAddress(ip, port), Connect_Timeout);
DataOutputStream DataOut = new DataOutputStream(socket.getOutputStream());
DataOut.writeBytes(message);
DataOut.flush();
socket.close();
So your ESP is the Server and the App the Client. This should work without problems.
I need alert for device paired to android phone in case when it gets out of range from phone Bluetooth. I worked on it and got something about rss can any one help me out.
do i need to send any character to check connection.
If you have the connection then you are probably blocking on the read() of the InputStream which will throw an IOException when the connection drops. Catch that and handle correctly.
I've been scratching my head on this one all day. I have a Bluetooth socket that listens for devices. This is done with AsyncTask. The first time I try to connect to it from a remote device, I don't have any problems. Now I'm trying to simulate a disconnect. Either by going out of range, shutting off the remote device's Bluetooth adapter, or just remotely closing the connection. Right now the server socket throws an IOException when the connection is lost. I'm using this as my cue to restart the AsyncTask and start listening again. This appears to work fine, all my logcat messages are showing up on the restart so I know the task is running, but the socket wont accept a connection. Thinking this could be a problem with restarting the AsyncTask, I tried just dumping the connection right after its accepted. After resetting them, I still have the same issue, even if I call system.gc().
Heres some code I'm using now to test:
boolean running = true;
while(running){
btAdapter = BluetoothAdapter.getDefaultAdapter();
btServerSocket = btAdapter.listenUsingRfcommWithServiceRecord(NAME, ID);
btSocket = btServerSocket.accept();
btServerSocket.close();
btSocket.close();
btServerSocket = null;
btSocket = null;
btAdapter = null;
System.gc();
sleep(10);
}
I really dont know what I'm doing wrong. Any help is greatly appreciated.
Thanks.
If you are only seeing this problem on Android 2.3.x, you might be running into a problem I just wrote about on another question. That problem also manifests as accept() working the first time, but then fails on successive tries. See my post here for details.
I have a midlet that reads some bytes from google to test, it runs ok on my nokia and sony ericsson cellphones but in "Samsung GT-E2120L cellphone it doesn't work, I get "java.io.IOException TCP Open" Exception when I try to get the connection response code
HttpConnection conn = (HttpConnection) Connector.open("http://www.google.com");
// The exception throws HERE
int rc = conn.getResponseCode();
byte[] buff = new byte[255];
conn.openInputStream().read(buff);
Note: I have credit :)
Note2: I can access to google from native navigator of the "Samsung cellphone"
Note3: I had tried to Connect to the URL with 3 diferent modes: Connector.READ,Connector.WRITE, Connector.READ_WRITE
Note4: My application is not trusted but I have the "javax.microedition.io.Connector.http" API Permissions
Note5: I know that the read buffer[255] is hard coded but it's only for test
Anyone can help me?
Thanks in advance and thanks for reading
"java.io.IOException TCP Open" - is it all that you get from exception?
If you declared javax.microedition.io.Connector.http in MIDlet-Permissions try to delete it. Really you don't need this parameter if your midlet is unsigned.
Some samsung mobiles (D600, E200) have bug: after some time without any correlations with actions were did they became unavailable to use internet from midlets. May be it's your case?
And as commentator said it could be different settings for accessing internet from java.