I am trying to communicate with a Bluetooth programmable Microcontroller. The Bluetooth device on the microcontroller communicates (specifically) on Bluetooth Serial COM Port number 4.
QUESTION: How can I get the Android App to read data from this COM port (number 4)?
I know the UUID is a well known unique ID,that works for this device, but I don't think that it has anything to do with specifying the COM port.
static final UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
btSocket = btDevice.createRfcommSocketToServiceRecord( myUUID);
btSocket.connect();
valid.append( btDevice.getName() + "\n" + btDevice.getAddress());
north.append("Socket Connected");
InputStream mmInStream = btSocket.getInputStream();
OutputStream mmOutStream = btSocket.getOutputStream();
byte[] buffer = new byte[10];
int bytes;
StringBuffer str = new StringBuffer();
while (true) {
try {
mmOutStream.write("a".getBytes());
//Reads a # of bytes until the end of stream is reached
bytes = mmInStream.read(buffer);
//Transform to string
str.append(buffer.toString()+"\t"); //Clear the buffer
Log.e("DATA", "THE DATA: "+ str.toString());
south.setText(str.toString());
str.delete(0,str.length());
} catch (IOException e) {
break;
} }}
The COM port is something that exists only on the microcontroller, not the Bluetooth device attached to it. The Bluetooth device does not even know which COM port the microcontroller used to connect to it. The Bluetooth device's connection to the micro is via the TX and RX lines. The fact that they are attached to pins on the micro assigned to a specific COM port is irrelevant and unknown to the Bluetooth device.
I had this problem with a custom bluetooth device I built. Instead of using createRfcommSocketToServiceRecord in your connect thread, try something similar to the following:
public ConnectThread(BluetoothDevice device) throws
SecurityException, NoSuchMethodException, IllegalArgumentException,
IllegalAccessException, InvocationTargetException {
mmDevice = device;
BluetoothSocket tmp = null;
// Force a BluetoothSocket for a connection with the
// given BluetoothDevice
Method m = mmDevice.getClass().getMethod("createRfcommSocket",
new Class[]{int.class});
mmSocket = (BluetoothSocket)m.invoke(mmDevice, Integer.valueOf(1));
}
Where my mmDevice is your btDevice.
This forces a socket connection between the unknown device and the smartphone. From what I've heard, there's an issue in Android connecting "non-similar" devices. Worth a shot.
Related
I am having trouble connecting to a specific UUID of a Bluetooth device:
private void initBt() {
BluetoothAdapter blueAdapter = BluetoothAdapter.getDefaultAdapter();
if (blueAdapter != null) {
blueAdapter.enable();
if (blueAdapter.isEnabled()) {
BluetoothDevice device = blueAdapter.getRemoteDevice("B0:00:00:00:00:F3");
UUID uuid = UUID.fromString("0000fff1-0000-0000-0000-00805f9b34fb");
BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuid);
socket.connect();
outputStream = socket.getOutputStream();
} else {
Log.e("error", "Bluetooth is disabled.");
}
} else {
throw new IOException("BluetoothAdapter failure");
}
}
I want to get an output stream to this UUID so I can send messages to it.
I got the UUID from the App LightBlue.
I cannot pair the device (and I think I don't have to, if LightBlue can send messages to the UUID without manual pairing).
An IOException is thrown on socket.connect(); stating:
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
Where am I going wrong?
Thanks for your help.
UPDATE:
I also tried it with
device.createInsecureRfcommSocketToServiceRecord(uuid);
and also double checked that I have all the permissions to do so (Bluetooth, BluetoothAdmin and AccessFineLocation).
I am trying to create a connection between an Android smartphone (Client) with a Bluetooth app (server) running on a PC.
Below is the code snippet for Client
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private class ConnectThread extends Thread {
private BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device) {
mmDevice = device;
BluetoothSocket tmp = null;
try {
//tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
tmp = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
Log.d(TAG, "create() failed", e);
e.printStackTrace();
}
mmSocket = tmp;
}
public void run() {
mAdapter.cancelDiscovery();
try {
mmSocket.connect();
}
catch (IOException e) {
//Exception caught
//java.io.IOException: read failed, socket might closed or timeout, read ret: -1
...
}
...
}
Below is the server code (Uses BlueCover jar)
private void waitForConnection() {
LocalDevice local = null;
StreamConnectionNotifier notifier;
StreamConnection connection = null;
try {
local = LocalDevice.getLocalDevice();
local.setDiscoverable(DiscoveryAgent.GIAC);
String uuidstr = "00001101-0000-1000-8000-00805F9B34FB";
String uuid_wo_space = uuidstr.replaceAll("-", "");
String url = "btspp://localhost:" + uuid_wo_space + ";authenticate=false;encrypt=false;name=RemoteBluetooth";
notifier = (StreamConnectionNotifier) Connector.open(url);
}
catch (Exception e) {
e.printStackTrace();
return;
}
// waiting for connection
while(true) {
try {
connection = notifier.acceptAndOpen();
Thread processThread = new Thread(new ProcessConnectionThread(connection));
processThread.start();
}
catch(Exception e) {
e.printStackTrace();
return;
}
}
I have read several links suggesting to change the UUID to "00001101-0000-1000-8000-00805F9B34FB", which i have already tried. I have also tried creating a not secure socket using createInsecureRfcommSocketToServiceRecord, which still fails with the same result.
Below is the IOException stack trace
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:900)
at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:912)
at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:531)
Sorry for writing a new answer but this way the information is more visible than the reply. Here's an example list of UUIDs for my device:
UUID: 0000xxxx-0000-1000-8000-00805f9b34fb
i xxxx Status Mode
0 110a N.A.
1 1105 Connected Serial Port Protocol (SPP)
2 1106 Connected File transfer (FTP)
3 1116 N.A.
4 112d Connected Remote SIM mode
5 112f Connected Phone book request
6 1112 Connected
7 111f Connected
8 1132 Connected Message access request
There are usually a set of UUIDs that denote different modes of operation like File Transfer (FTP), Remote SIM, Phone book request etc.
You may query and try all the UUIDs on your interface like this:
device = (from bd in adapter.BondedDevices
where bd.Name == "YourDeviceName"
select bd).FirstOrDefault();
Android.OS.ParcelUuid[] parcel = device.GetUuids();
for (int i = 0; i < parcel.Length; i++) {
socket = device.CreateRfcommSocketToServiceRecord(parcel[i].Uuid);
try {
socket.Connect();
break;
}
catch {
throw new Exception("Unsupported UUID");
}
}
Also make sure no other device is connected to PC/Android and have an open socket because only one socket can be serviced at a time. You can have as many pairs as you want but only one socket running.
The Bluetooth tutorials i read all mentioned that i need to have the same UUID on both sides (Server and Client) to establish a connection between two devices. But what if i dont know the UUID of my Client and if i dont care?
Background information: I have over 1000 microcontrollers with bluetooth. Each microcontroller has a fix and unchangeable UUID. Smartphones should be able to send string messages to that micrcontrollers (single connection, one smartphone is controlling one microcontroller). It should not matter which Smartphone is controlling which microcontroller. So in fact i really dont care about the UUID of the Client.
So my Smartphone is the Server and is opening a listening thread for incoming Bluetooth connections but i have to put in a UUID here:
tempBluetoothServerSocket = bluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
But when i have thousand different UUID's and i really dont care about the UUID what should i put in there? Also the BluetoothSocket:
tempBluetoothSocket = this.bluetoothDevice.createRfcommSocketToServiceRecord(MY_UUID);
How to know which UUID?
So the core question is: How can i connect to any microcontroller?
I've been using this:
// Unique UUID for this application
private static final UUID UUID_ANDROID_DEVICE =
UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");
private static final UUID UUID_OTHER_DEVICE =
UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
And it's uses:
public AcceptThread(boolean isAndroid) {
BluetoothServerSocket tmp = null;
// Create a new listening server socket
try {
if(isAndroid)
tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE, UUID_ANDROID_DEVICE);
else
tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE, UUID_OTHER_DEVICE);
} catch (IOException e) { }
mmServerSocket = tmp;
}
public ConnectThread(BluetoothDevice device) {
mmDevice = device;
BluetoothSocket tmp = null;
// Get a BluetoothSocket for a connection with the
// given BluetoothDevice
try {
if(BluetoothService.this.isAndroid)
tmp = device.createRfcommSocketToServiceRecord(UUID_ANDROID_DEVICE);
else
tmp = device.createRfcommSocketToServiceRecord(UUID_OTHER_DEVICE);
} catch (IOException e) { }
mmSocket = tmp;
}
Which allows my devices to connect to any bluetooth device I've tested with. For the sake of testing, it has only been varying bluetooth barcode scanners. Although I believe this is a generic RFCOMM UUID.
It hasn't failed me yet.
I dowloaded the BluetoothChat sample project here :
https://android.googlesource.com/platform/development/+/master/samples/BluetoothChat?autodive=0%2F
The fact is, when I'm launching the app on two devices (which are NOT paired), it should connect the two devices WITHOUT asking for pairing the two devices, shouldn't it ?
And in fact, when I'm trying to connect the two devices (which are NOT paired), it's asking to pair the devices .
I mean, there is this function in BluetoothChatService.java which should create an insecure socket . But it seems, that it doesn't do his job ?
/**
* This thread runs while attempting to make an outgoing connection
* with a device. It runs straight through; the connection either
* succeeds or fails.
*/
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
private String mSocketType;
public ConnectThread(BluetoothDevice device, boolean secure) {
mmDevice = device;
BluetoothSocket tmp = null;
mSocketType = secure ? "Secure" : "Insecure";
// Get a BluetoothSocket for a connection with the
// given BluetoothDevice
try {
if (secure) {
tmp = device.createRfcommSocketToServiceRecord(
MY_UUID_SECURE);
} else {
tmp = device.createInsecureRfcommSocketToServiceRecord(
MY_UUID_INSECURE);
}
} catch (IOException e) {
Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
}
mmSocket = tmp;
}
Can somebody explain me why it asks to pair the two devices ?
The method createInsecureRfcommSocketToServiceRecord shouldn't ask for pairing unpaired devices , should it ? x)
I'm really confused .
createInsecureRfcommSocketToServiceRecord() has of
"insecure" the key the devices are using to communicate, which is, below Bluetooth 2.1, not encrypted. That is what is "unsecure".
But it does not change the fact that if the MAC address is not already in the pairing database there will always be the prompt.
So yes, it will prompt.
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?