I am developing an app for bluetooth remote control a small robot (no, not arduino). The robot has a bluetooth chip (BK3221), which, from what I've gotten to know through their UUIDs works with A2DP and AVRCP protocols (oriented audio).
UUID 1: 0000110b-00000-1000-8000-00805f9b34fb
UUID 2: 0000110e-00000-1000-8000-00805f9b34fb
I can create the connection of the mobile and device through the connect() of Bluetooth A2DP classbut that is what I have, I have the proxy and the connected device but I do not know how to give information .
Moreover I tried program the connection in a basic way with functions for android with RFCOMM (which is supposed to support AVRCP). The robot is a closed system and I don´t know if it worked as a server or client (I assumed it was because the server because it accepts the conexion with the function "conect()" in the previous case). But when I call the connect function generates an exception BluetoothSocket me: "JSR82 connect connection is not created (failed or aborted)". I looked on JSR82 but gave me the feeling of being obsolete ...
If anyone has any idea of something... Thanks
Code of the connection with BluetoothA2dp:
//adaptador == BluetoothAdapter and proxy1 == BluetoothA2dp
protected Boolean doInBackground(BluetoothDevice device) throws RemoteException {
Method connect = getConnectMethod();
final BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {
public void onServiceConnected(int profile, BluetoothProfile proxy) {
if (profile == BluetoothProfile.A2DP) {
proxy1 = (BluetoothA2dp) proxy;
}
}
public void onServiceDisconnected(int profile) {
if (profile == BluetoothProfile.A2DP) {
proxy1 = null; }
}};
adaptador.getProfileProxy(getBaseContext(), mProfileListener, BluetoothProfile.A2DP);
try {
connect.setAccessible(true);
connect.invoke(proxy1, device);
Toast.makeText(getBaseContext(),"Connection OK!", Toast.LENGTH_LONG).show();
} catch (Exception ex) {
Toast.makeText(getBaseContext(),"Connection Error"+ex.getMessage(), Toast.LENGTH_LONG).show();
}
private Method getConnectMethod () {
try {
return BluetoothA2dp.class.getDeclaredMethod("connect", BluetoothDevice.class);
} catch (NoSuchMethodException ex) {
Toast.makeText(getBaseContext(),"Method dont appear", Toast.LENGTH_LONG).show();
return null;
}
}
Related
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
}
}
}
We have a Bluegiga Bluetooth module that is set up to work as hands free device.
After initial pairing, I need my application to initiate connection to it via HFP programmatically. Can this be achieved in Android?
Using hidden api works for me!
// get default bluetooth adapter
BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter();
// get bounded device on Android
Set<BluetoothDevice> devices = mAdapter.getBondedDevices();
if (devices.size() > 0) {
for (Iterator<BluetoothDevice> it = devices.iterator(); it.hasNext();) {
BluetoothDevice device = it.next();
// treat the device the default buletooth device you needed
mCurrentDevice = device;
// break;
}
} else {
return;
}
// another method to get headset(HFP) profile
mAdapter.getProfileProxy(mContext, new BluetoothProfile.ServiceListener() {
#Override
public void onServiceConnected(int profile, BluetoothProfile proxy) {
Log.e("log", "headset proxy connected");
try {
BluetoothHeadset mCurrentHeadset = (BluetoothHeadset) proxy;
// check whether or not current device hfp is connected or not, if not,
// try to connect the channel between phone and device using hidden api
if (mCurrentHeadset.getConnectionState(mCurrentDevice) != BluetoothHeadset.STATE_CONNECTED) {
Method connectMethod = mCurrentHeadset.getClass().getMethod("connect", mCurrentDevice.getClass());
connectMethod.setAccessible(true);
Boolean returnValue = (Boolean) connectMethod.invoke(proxy, mCurrentDevice);
Log.e("log", "headset proxy connected " + returnValue);
}
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void onServiceDisconnected(int profile) {
Log.e(LogTag.TAG, "headset profile disconnected");
}
}, BluetoothA2dp.HEADSET);
Could someone tell me if i can use my android device as a slave while using Bluetooth. I want another device (for example: PC) to act as a Master. I want the PC to connect to the Android device via Bluetooth and then receive messages from the Android device say on the hyper terminal of the PC.
BR,
Suppi
You need to create a RFCOMM connection where your Android device will listen to incomming connections. Here is some sample code of my own. BluetoothServiceEndpoint and BluetoothDeviceConnection are abstraction interfaces, but in your case you do not need them (just use Android API objects). This chunk of code will use an unauthenticated RFCOMM socket (without pairing) if available (only from Gingerbread, but it works in previous versions since I use reflection).
You would call bind() and then accept() to accept connections.
public void bind() throws IOException
{
if (serverSocket != null)
{
throw new IOException("Service already bound");
}
UUID serviceUUID = UUID.fromString(localServiceEndpoint.getUuid()
.toRFC4122String());
boolean boundWithAuthentication = false;
if (!localServiceEndpoint.isAuthenticationRequired())
{
serverSocket = listenUsingInsecureRfcommWithServiceRecord(
localServiceEndpoint.getServiceName(), serviceUUID);
}
if (serverSocket == null)
{
/*
* Si no hemos podido utilizar un socket inseguro (sin
* autenticación) aunque se haya solicitado así usamos uno seguro.
*/
serverSocket = ba.listenUsingRfcommWithServiceRecord(
localServiceEndpoint.getServiceName(), serviceUUID);
boundWithAuthentication = true;
}
int usedChannel = getUsedChannelPrivate(serverSocket);
remoteServiceEndpoint = new BluetoothServiceEndpoint(
BluetoothServiceEndpoint.TYPE_SPP, ba.getAddress().replaceAll(
":", ""), usedChannel, null,
localServiceEndpoint.isAuthenticationRequired());
info("Service bound " + (boundWithAuthentication ? "with" : "without")
+ " authentication");
}
private BluetoothServerSocket listenUsingInsecureRfcommWithServiceRecord(
String serviceName, UUID serviceUUID)
{
BluetoothServerSocket socket = null;
try
{
Method m = ba.getClass().getMethod(
"listenUsingInsecureRfcommWithServiceRecord", String.class,
UUID.class);
socket = (BluetoothServerSocket) m.invoke(ba, serviceName,
serviceUUID);
}
catch (Exception e)
{
warn("Unable to bind service without authentication. This device does not support API level >= 10");
}
return socket;
}
public BluetoothDeviceConnection accept() throws IOException
{
if (serverSocket == null)
{
throw new IOException("Service not bound");
}
BluetoothSocket socket = serverSocket.accept();
return new BluetoothDeviceConnectionImpl(new AndroidNativeConnection(
socket), params, listener, logger);
}