I am making an android app on Android Studio that writes serial data from the app, and transfers it to my Mac laptop via Bluetooth. I have seen code from this very helpful stack overflow answer but I'm unsure where this output goes. The following code is in my OnCreate in my MainActivity.java.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
final UUID SERIAL_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); //UUID for serial connection
String mac = "D0:88:0C:80:42:2B";
BluetoothDevice 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.createInsecureRfcommSocketToServiceRecord(SERIAL_UUID);
System.out.println(socket);
} catch (IOException e) {
e.printStackTrace();
}
try {
socket.connect();
//now you can use out to send output via out.write
for (int i = 0; i < 150; i++){
socket.getOutputStream().write("TESTING BLUETOOTH SERIAL".getBytes());
Thread.sleep(100);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
The android phone and Mac are paired before the launch of the app, and the appropriate permissions are given in manifest.xml. I know that the for loop executes socket.getOutputStream().write(...) but I don't know where to look for the output TESTING BLUETOOTH SERIAL on my Mac laptop. I tried looking at the output of /dev/tty.Bluetooth-Incoming-Port but did not find anything. So how can I find this output?
Related
i'm developing a code which lists all paired devices (working till here) and on click on a device whcih are listed in textviews, i want the programm to connect to device programmatically.
So here is myCode:
final View.OnClickListener onclicklistener = new View.OnClickListener() {
#Override
public void onClick(View view) {
index_connection=view.getId();
try {
btSocket=devices[index_connection].createInsecureRfcommSocketToServiceRecord(myUUID);
} catch (IOException e) {
e.printStackTrace();
}
try {
btSocket.connect();
} catch (IOException e) {
try {
btSocket.close();
Toast.makeText(Connection.this, "Connected to "+devices[index_connection].getName(),Toast.LENGTH_LONG);
} catch (IOException e1) {
Toast.makeText(Connection.this, "Not connected!",Toast.LENGTH_LONG);
e1.printStackTrace();
}
}
}
};
So onclick i'm getting the ID of the Textview which is equal with the index of the device i saved into an array. But now on clicking it does not create any connection (and yes, the other device has Bluetooth activated).
The code throws an Exception if i'm getting the device UUID from the Telephony manager:
myUUID = UUID.fromString(tManager.getDeviceId());
The exception says: java.lang.SecurityException: getDeviceId: The user 10284 does not meet the requirements to access device identifiers.
If i try to create the UUID "fromString" i don't get an exception, but it doesn't connect either.
Best regars,
flar2000
Try this as your UUID, set it like:
private final string MY_UUID = "00001101-0000-1000-8000-00805F9B34FB";
Then you can create your connection using ServiceRecord.
BluetoothDevice mDevice;
Set<BluetoothDevice> devices = BluetoothAdapter.getDefaultAdapter().getBondedDevices();
for (BluetoothDevices device : devices) {
if (device.getAdress().equals("YOUR DEVICE ADRESS HERE")) {
mDevice = device;
break;
}
}
BluetoothSocket mSocket = mDevice.createRfcommSocketToServiceRecord(UUID.fromString(MY_UUID));
mSocket.connect();
Don't forget to declare on your manifest the bluetooth permission.
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
I tried to create a simple android application to connect to my ELM327 device to get some car diagnostic data. But I wasn't able to set up the bluetooth connection b/t my android phone and my ELM327 device.
My code is very simple as below:
public class Bluetooth {
protected BluetoothAdapter mBluetoothAdapter= BluetoothAdapter.getDefaultAdapter();
private ConnectThread mConnectThread = null;
private AcceptThread mAcceptThread = null;
private WorkerThread mWorkerThread = null;
private BluetoothDevice mOBDDevice = null;
private BluetoothSocket mSocket = null;
private String uuid;
Bluetooth() {
mBluetoothAdapter= BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices;
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled())
return;
pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
// There are paired devices. Get the name and address of each paired device.
for (BluetoothDevice device : pairedDevices) {
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress(); // MAC address
//TODO: check whether this is OBD and whether it is connected
//by sending a command and check response
if (deviceName.contains("OBD")) {
mOBDDevice = device;
uuid = device.getUuids()[0].toString();
break;
}
}
}
mBluetoothAdapter.cancelDiscovery();
}
/**
* Start the chat service. Specifically start AcceptThread to begin a session
* in listening (server) mode. Called by the Activity onResume()
*/
public synchronized void connect()
{
try {
// Get a BluetoothSocket to connect with the given BluetoothDevice.
// MY_UUID is the app's UUID string, also used in the server code.
mSocket = mOBDDevice.createRfcommSocketToServiceRecord(UUID.fromString(uuid));
} catch (IOException e) {
Log.e(TAG, "Socket's create() method failed", e);
}
try {
// Connect to the remote device through the socket. This call blocks
// until it succeeds or throws an exception.
mSocket.connect();
} catch (IOException connectException) {
// Unable to connect; close the socket and return.
try {
mSocket.close();
} catch (IOException closeException) {
Log.e(TAG, "Could not close the client socket", closeException);
}
return;
}
}
}
In the mainactivity, I will first new a Bluetooth class then call bluetooth.connect():
mBluetooth = new Bluetooth();
mBluetooth.connect();
When I debug the program, I was able to get my ELM327 bluetooth device by querying all the bonded devices with a name of "OBD". I also was able to get the device's uuid and create a socket using createRfcommSocketToServiceRecord. But in the connect function, mSocket.connect() always fail with a return value of -1 and get a IOexception.
My questions are:
When my android application connect to the ELM327 device, my android phone is the bluetooth client and my ELM327 device is the bluetooth server, is this understanding correct?
Is there a server program running on my ELM327 device listening and accept incoming connection? Is this defined behavior of ELM327 protocol?
Any idea why mSocket.connect()has failed? Any idea on how to look into this issue? Or any obvious error in my program? Thanks.
problem solved. see source codes below:
public synchronized void connect() throws IOException {
try {
// Get a BluetoothSocket to connect with the given BluetoothDevice.
// MY_UUID is the app's UUID string, also used in the server code.
mSocket = mOBDDevice.createRfcommSocketToServiceRecord(UUID.fromString(uuid));
} catch (IOException e) {
Log.e(TAG, "Socket's create() method failed", e);
}
try {
// Connect to the remote device through the socket. This call blocks
// until it succeeds or throws an exception.
mSocket.connect();
} catch (IOException e1) {
Log.e(TAG, "There was an error while establishing Bluetooth connection. Falling back..", e1);
Class<?> clazz = mSocket.getRemoteDevice().getClass();
Class<?>[] paramTypes = new Class<?>[]{Integer.TYPE};
try {
Method m = clazz.getMethod("createRfcommSocket", paramTypes);
Object[] params = new Object[]{Integer.valueOf(1)};
mFallbackSocket = (BluetoothSocket) m.invoke(mSocket.getRemoteDevice(), params);
mFallbackSocket.connect();
mSocket.close();
mSocket = mFallbackSocket;
} catch (Exception e2) {
Log.e(TAG, "Couldn't fallback while establishing Bluetooth connection.", e2);
mSocket.close();
//throw new IOException();
}
}
inputStream = mSocket.getInputStream();
outputStream = mSocket.getOutputStream();
}
I don't know much about Android, although I know about OBD2 and the lot.
It depends on the type of your adapter. If you have a WiFi adapter, you can consider the adapter being the server and you the client. You connect to a socket and then read from it. In the case of a Bluetooth adapter, it's different. If you connect via rfcomm, it's a serial protocol and neither is the server nor the client. If you connect via BTLE, the OBD2 dongle is the Peripheral and you are the Central.
On WiFi adapters, yes. This behavior is not part of ELM327 though. ELM327 only specifies the serial commands. How you transfer these is not part of the spec, since it happens on the layer above (WiFi, rfcomm, BTLE, USB, etc.).
Are you sure that rfcomm works via the socket interface? It's a serial interface, so I would have expected file-like operations.
I checked the developer site and got some help to develop the Bluetooth chat app. I'm not able to connect to other devices. I have used the UUID which was mentioned in the developer site Bluetooth chat example. I know it should be unique but I don't know how to find. Please let me know. I am using Samsung Galaxy J7.
The Bluetooth Chat sample helps how to do a similar app based on the sample.
This application allows two Android devices to carry out two-way text chat over Bluetooth. It demonstrates all the fundamental Bluetooth API capabilites, such as: (1) Scanning for other Bluetooth devices (2) Querying the local Bluetooth adapter for paired Bluetooth devices (3) Establishing RFCOMM channels/sockets (4) Connecting to a remote device (5) Transfering data over Bluetooth
The sample isn't easy to learn you'll need some help by me.
How to make your Bluetooth Chat app based on sample
The Bluetooth connection works in the Client and Server method, even if you're connecting 2 devices, all devices will be both Client and Server (devices connect to others and also accept connections from others).
The UUIDs
UUID stands for Universally Unique Identifier, your UUID must be unique, however, uniqueness isn't always guaranteed and the chances of generating a equal UUID are so low you don't need to worry about the uniqueness. If you want, google "UUID generator" and you'll find sites like this.
Once you generated your UUID, use the same UUID for Client and Server.
Connect to other devices
To connect to other devices, you'll have to enumerate the paired devices in the first place, the BluetoothAdapter contains everything for initial setup. The android.bluetooth.* package contains everything for your app.
BluetoothAdapter is a Singleton, so you can call the method BluetoothAdapter.getDefaultAdapter() many times without making too much instances.
BluetoothAdapter.getDefaultAdapter()
Let's assign mBluetoothAdapter as BluetoothAdapter.getDefaultAdapter().
Make sure Bluetooth is on, if Bluetooth was off, let's turn on.
if (!mBluetoothAdapter.isEnabled()) mBluetoothAdapter.enable();
It will take a few seconds to turn the Bluetooth on.
Enumerate paired devices
Let's enumerate the paired devices, the BluetoothDevice contains information about a specific device, "bonded" means "paired".
List<BluetoothDevice> devices = new ArrayList<>();
for (BluetoothDevice device : mBluetoothAdapter.getBondedDevices()) {
devices.add(device);
}
//Let's add the bonded devices to an ListView
MyBluetoothAdapter adapter = new MyBluetoothAdapter(this, devices);
mListView.setAdapter(adapter);
The MyBluetoothAdapter is an example, you'll need to make your own ArrayAdapter to fit your needs.
Connect to device
When some item is selected, Bluetooth will connect to the device, to prevent UI freeze, the connection will be made in the background using a different Thread.
BluetoothDevice device = yourMethodToGetTheDevice();
ConnectThread mConnectThread = new ConnectThread(device);
mConnectThread.start();
The ConnectThread code is here (I recommend to make an inner class to access parent class method):
The Bluetooth protocol that the sample uses is RFCOMM. THe mUUID is the UUID generated with the website mentioned.
private class ConnectThread extends Thread {
public ConnectThread(BluetoothDevice device) {
try {
mBluetoothSocket = device.createRfcommSocketToServiceRecord(mUUID);
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
}
}
#Override
public void run() {
try {
Log.i(TAG, "Connecting...");
mBluetoothSocket.connect();
new ConnectedThread(mBluetoothSocket);
Log.i(TAG, "Connected");
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
}
}
}
After the connection was established, you'll be able to send data.
Accept connection
To accept others' connections, you'll have to start a background thread that continuously checks for a connection.
private class AcceptThread extends Thread implements Closeable {
private BluetoothServerSocket mBluetoothServerSocket;
private volatile boolean running = true;
public AcceptThread(String name) {
mBluetoothServerSocket = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(name, mUUID);
}
#Override
public void run() {
while (running) {
try {
mBluetoothSocket = mBluetoothServerSocket.accept();
if (mBluetoothSocket != null) {
close();
new ConnectedThread(mBluetoothSocket);
}
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
break;
}
}
}
#Override
public void close() throws IOException {
mBluetoothServerSocket.close();
mBluetoothServerSocket = null;
running = false;
}
}
This code restricts the connection to a single device.
Communication
Once device is connected, the ConnectedThread will start.
private class ConnectedThread extends Thread implements Closeable {
private InputStream in;
private OutputStream out;
private volatile running = true;
public ConnectedThread(BluetoothSocket socket) {
try {
in = socket.getInputStream();
out = socket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
}
}
#Override
public void run() {
//The maximum amount of data to receive is 4KB, if you want to receive more data, you'll have to receive large data by chunks using while loop.
//Usually text isn't as large as 4KB.
byte[] data = new byte[4096];
int length;
while (running) {
try {
length = in.read(data);
String text = new String(data, 0, length);
Log.i(TAG, text);
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
//Connection was lost
break;
}
}
}
public void write(byte[] data) throws IOException {
out.write(data);
}
}
To send data, like text, use this code:
mConnectedThread.write("MY TEXT".getBytes());
To receive data, use the code inside ConnectedThread and handle the String text variable.
I would like to manually connect a bluetooth device with its MAC address because it is faster and I know exactly which MAC to connect.
I use this method to get the BluetoothDevice : http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#getRemoteDevice%28byte[]%29
But the Android doc does not say if Android ensure that the device is in range before creating the BluetoothDevice object.
Do you have this information ?
My code can automatically connect the device, and I would like to check if the target is in range before trying to connect, but without perform a large scan (which can be long...)
When local device connects to remote device using BluetoothSocket, an exception is required.
If remote device isn't in range, It's not found
private class ConnectThread extends Thread {
public ConnectThread(BluetoothDevice device, boolean isSecure, UUID sharedUUID) throws IncorrectSetupException {
try {
//Secure connections requires to get paired before connect
//Insecure connections allows to connect without pairing
if (isSecure) {
mSocket = device.createRfcommSocketToServiceRecord(sharedUUID);
} else {
mSocket = device.createInsecureRfcommSocketToServiceRecord(sharedUUID);
}
} catch (IOException e) {
//Is there some problem with the setup?
}
}
public void run() {
try {
mSocket.connect();
} catch (IOException e) {
//If device is not found, this exception is throwed
}
}
}
I'm struggling with getting consistent bluetooth connections in a star topology. I have one master phone which is a Samsung Galaxy S4 running API 10. All of the phones that connect to the bluetoothserver socket on the S4 are LG Dynamic Tracfones also running API 10.
Over the past few days, I have seen a LOT of conflicting information on the web about what type of connection to use.
This is my current set up:
MASTER CODE
public void acceptConnection() {
.... (enable bt adapter) ...
// initializes a Bluetooth server socket
bluetoothServerSocket = bc.createBluetoothServerSocket();
//connection made to Master, discovery no longer needed
bluetoothAdapter.cancelDiscovery();
BluetoothSocket bluetoothSocket;
// loops until the thread is interrupted or an exception occurs
while (!isInterrupted()) {
try {
// attempts to accept the slave application's connection
bluetoothSocket = bluetoothServerSocket.accept();
} catch (IOException e) {
// prints out the exception's stack trace
e.printStackTrace();
Log.v("Default Thread", "Connection to slave failed.");
// breaks out of the while loop
return;
}
try {
... (enumerate all input and output streams, and all bt sockets) ...
} catch (IOException e) {
// prints out the exception's stack trace
e.printStackTrace();
}
}
This is the method that is called when creating a blueToothServerSocket, and this is where half of my confusion is. How should I listen on the adapter? Currently, I'm doing it insecurely.
public BluetoothServerSocket createBluetoothServerSocket() {
// gets the name of the application
String name = "PVCED";
// gets a common UUID for both the master and slave applications
UUID uuid = UUID.fromString("23ea856c-49da-11e4-9e35-164230d1df67");
// initializes an empty Bluetooth server socket
serverSocket = null;
try {
// creates a Bluetooth socket using a common UUID
serverSocket = bluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord(name, uuid);
} catch (IOException e) {
// prints out the exception's stack trace
e.printStackTrace();
}
return serverSocket;
}
SLAVE CODE
And this is where the other half of my confusion is, how should I create a socket? Currently I'm doing it insecurely.
private BluetoothSocket createBluetoothSocket(Set<BluetoothDevice> pairedDevices) {
// gets a common UUID for both the master and slave applications
UUID uuid = UUID.fromString("23ea856c-49da-11e4-9e35-164230d1df67");
// initialises an empty Bluetooth socket
BluetoothSocket bluetoothSocket = null;
// checks to see if there are any paired devices
if (pairedDevices.size() > 0) {
// loops through each paired device
for (BluetoothDevice device : pairedDevices) {
// checks to see if the name of the paired device is MASTER
if (device.getName().equals("MASTER")) {
try {
master = device;
// creates a Bluetooth socket using a common UUID
//bluetoothSocket = master.createRfcommSocketToServiceRecord(uuid);
//Method m = master.getClass().getMethod("createRfcommSocketToServiceRecord", new Class[] {int.class});
//bluetoothSocket = (BluetoothSocket) m.invoke(master, 1);
bluetoothSocket = master.createInsecureRfcommSocketToServiceRecord(uuid);
} catch(Exception e){
Log.v("Connect Exception", e.getMessage());
}
}
}
}
//check if we paired succesfully to a master, if not, prompt user to do so.
if (master == null){
... (tell user to pair with master via toast) ...
}
return bluetoothSocket;
}
My logcat is often filled with errors such as "Bad File Descriptor", "Unable to start Service Discovery", or "Service Discovery has failed."
What is the best connection scheme to use for my scenario? If you guys need more details on how I'm enabling/disabling bt adapters, or closing bt connections, I can supply more code.