I already managed to get it working using this method at android 2.3.3 version, but now I am testing using another device with 2.2 version..
And I can find many devices using discovery, but can't connect to other devices using:
BluetoothSocket socket = deviceToConnect.createRfcommSocketToServiceRecord(UUID.fromString(MY_UUID));
On the other side, I am using the proper thread to accept the connection using :
mBluetoothAdapter.cancelDiscovery();
BluetoothServerSocket ss;
try {
ss = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(MainPage.SD_FolderName, UUID.fromString(MY_UUID));
new Thread(new ListenerHandler(ss, this, resultCode)).start();
} catch (IOException e) {
Toast.makeText(this, "There was a problem opening the bluetooth server socket!", Toast.LENGTH_LONG).show();
finish();
}
Then on the ListenerHandler I have this:
BluetoothSocket clientSocket = ss.accept();
But the connection is never stablished, any thoughs?
Thanks in advance!
Related
I have two apps: one acting as a Server in a device with Android 7.1.2 (a camera) ,and another one acting as Client in Android 7.0. (Samsung Galaxy S7). I did the pairing so they both appear in each other discovery method.
Client does:
...
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mmSocket = device.createInsecureRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
...
mBluetoothAdapter.cancelDiscovery();
try {
mmSocket.connect();
} catch (IOException connectException) {
connectException.printStackTrace();
}
...
And Server does:
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mmServerSocket = mBluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord("name",UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
...
BluetoothSocket socket = null;
boolean connected = false;
// Keep listening until exception occurs or a socket is returned.
while (true) {
if(!connected) {
try {
MyLog.d(TAG, "accepting");
socket = mmServerSocket.accept();
} catch (IOException e) {
MyLog.e(TAG, "Socket's accept() method failed");
break;
}
}
...
}
Everything works fine when I test my apps in two normal Android phones, and when I use my server in a phone and my camera as a client.
But if I try to run the server in the camera and the client in the phone then the camera gets stuck in
socket = mmServerSocket.accept();
and the client returns an IOException in
mmSocket.connect();
java.io.IOException: read failed, socket might closed or timeout, read
ret: -1
I guess it's something to do with the camera Bluetooth not accepting connections but I don't know that much about it and everything works fine when the roles are switched and the camera runs the client app.
Any ideas?
Thanks!
I've been planning to make an Android App, that would communicate with this bluetooth rs232 adapter
http://www.nordfield.com/wireless-serial-rs232-bluetooth-adapter
and I'm not sure what should I do with UUID in this case? Will I be able to start a connection with an application, that would look like BluetoothChat sample app from Google?
https://android.googlesource.com/platform/development/+/25b6aed7b2e01ce7bdc0dfa1a79eaf009ad178fe/samples/BluetoothChat/
Well, I've solved the problem already. Actually there is possibility to create Bluetooth connection without knowing device's UUID. It is achieved by this code:
public ConnectThread(BluetoothDevice device) {
mmDevice = device;
BluetoothSocket tmp = null;
try {
Method method;
method = device.getClass().getMethod("createRfcommSocket", new Class[] { int.class } );
tmp = (BluetoothSocket) method.invoke(device, 1);
} catch (Exception e) { }
mmSocket = tmp;
}
All of the rest of connection management is done the same as in BluetoothChat sample app from Google.
Iam trying to connect to a socket by using the connect method. Iam generating a String containing the UUID like this:
MY_UUID = UUID.fromString("45341da0-c9c1-11e1-9b21-0800200c9a66");
Then constructing a BluetoothSocket like this:
BluetoothSocket tmp = null;
I also want to connect to a specific device by its mac-address:
BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice("00:1B:DC:0F:EC:7E");
and then making the bluetoothSocket
try {
tmp = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Exception1: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
Just for to test I added a breakpoint where the tmp object is been initialized inside the try catch. But it's only containing NULL The remote device does support OBEX OPP, but this is just a layer above the RFCOMM in the bluetooth stack, so I think my device should support RFCOMM for connections. Can anybody tell me why my tmp object is set to null?
Is there anyway I can test whenever the socket is created?
I have a device which supports the OBEX Object Push Profile, this profile is based upon the Serial Port Profile. My guess is that I can use the Android Bluetooth Chat example for connecting this device to my Android Phone. But I ran into a problem, regarding the socket.accept() functionality in the android SDK. I try to accomplish to connect my phone with this device like this:
adapter = BluetoothAdapter.getDefaultAdapter();
device = adapter.getRemoteDevice("00:1B:DC:0F:EC:7E");
AcceptThread = new AcceptThread(true, adapter, device);
AcceptThread.start();
The constructor in AcceptThread is coded like this:
public AcceptThread(boolean secure, BluetoothAdapter adapter, BluetoothDevice device) {
BluetoothServerSocket tmp = null;
this.adapter = adapter;
this.device = device;
// Create a new listening server socket
try {
tmp = adapter.listenUsingInsecureRfcommWithServiceRecord(device.getName(), UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
} catch (Exception e) {
Log.e(TAG, ".AcceptThread # listen() failed", e);
}
mmServerSocket = tmp;
}
The problem is when I try to do a connect() as I said before
public void run() {
BluetoothSocket socket = null;
// Listen to the server socket if we're not connected
while (mState != STATE_CONNECTED) {
try {
// This is a blocking call and will only return on a
// successful connection or an exception
Log.d(TAG, "AcceptThread.run: accepting server socket connection");
socket = mmServerSocket.accept(20000);
Log.d(TAG, ".AcceptThread.run # server socket connection accepted");
} catch (Exception e) {
Log.e(TAG, ".run # accept() failed: "+e);
break;
}
}
}
As you can see the ServerSocket accept every incomming connection for 20 seconds or 20000 ms. When the time is up, the app will throw an IOException like this
07-11 10:30:08.355: E/SIMPLECONNECT(1301): .run # accept() failed: java.io.IOException: Connection timed out
which tells me that my device couldnt connect to my android phone. The device doesnt have a connect button on the display, just a send functionalitywhich will send a file to my phone. I believe that this send functionality also do a connect in the background, but I am not sure.
Can anybody pinpoint any solutions for me? I am running my app on a Samsung Galaxy SIII with Android 4.0.4
I finally solved it, the problem is that different Android Versions and different devices seemes to need different sockets. I tryed it with Samsung Galaxy XCOVER, Tab1, Tab2, Nexus, Note, Motorola Defy and HTC Flyer.
The Sockets I used are:
A:
Method m = mmDevice.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
mSocket = (BluetoothSocket) m.invoke(mmDevice, Integer.valueOf(1));
B:
Method m = mmDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[]{int.class});
mSocket=(BluetoothSocket)m.invoke(mmDevice,Integer.valueOf(1));
C:
mSocket=BluetoothAdapter.getDefaultAdapter().getRemoteDevice(mmDevice.getAddress()).createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
Android 4.0.x works for Nexus, Flyer,Tab1 with A,B
Android 4.0.3 works for Tab2 with B
Android 3,6,x works for DEFY with A,B
Android 2.3.6 works for XCOVER with C
I can't find a solution witch works for all devices and I;m not able to find out witch socket will work before I create and use the Socket, especially the XCOVER perform the connect() for all Sockets without throwing an exception, but catch if i try tro write(). So if you want to setup a bloothoh connection wich works for all devices you have to create the sockets, connect an write and then remeber the socket wich works (e.g. in preferences)
I have spent some time researching Android's ability to communicate with bluetooth devices that are designed to communicate over a Bluetooth COM port on a PC. I haven't been able to find a definitive answer, so I thought I'd ask here. I want to make sure that this is possible with Android.
I am new to Bluetooth communications, but the research I've done so far lead me to RFCOMM which somewhat sounded like what I wanted. Unfortunately, I'm still unable to confirm that this is in fact possible.
Any help/resources on this would be greatly appreciated.
Yes, Android can connect to Bluetooth COM ports on PC's. I am currently developing such an application. Here is a code example (Ite requires the bluetooth permissions te be set in the Manifest.xml file):
<uses-permission android:name="android.permission.BLUETOOTH" />
Java:
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter == null) {
// Device does not support Bluetooth
finish(); //exit
}
if (!adapter.isEnabled()) {
//make sure the device's bluetooth is enabled
Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetooth, REQUEST_ENABLE_BT);
}
final UUID SERIAL_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); //UUID for serial connection
mac = "00:15:83:3D:0A:57"; //my laptop's mac adress
device = adapter.getRemoteDevice(mac); //get remote device by mac, we assume these two devices are already paired
// Get a BluetoothSocket to connect with the given BluetoothDevice
BluetoothSocket socket = null;
OutputStream out = null;
try {
socket = device.createRfcommSocketToServiceRecord(SERIAL_UUID);
} catch (IOException e) {}
try {
socket.connect();
out = socket.getOutputStream();
//now you can use out to send output via out.write
} catch (IOException e) {}