Connecting android device with a handsfree car radio via bluetooth - android

my question is connected with android device and being able to communicate with another device via bluetooth.
On one hand I have an android device with android 2.3.7 and on the other hand I have a PIONEER DEH-6400BT. I can successfully connect the two devices and do everything that is explained when using radio handsfree bluetooth communication.
The thing I am trying to do is the following:
I would like to connect to the radio via bluetooth(using bluetooth socket) and to be able to do anything with the radio that is connected with the radio's abilities to list contacts/add contact/show that a contact is dialing/or just showing some kind of string on the radio display.
In order to achieve this I started with the BluetoothChat example plus using the InsecureBluetooth class and at the beginning I started connecting with an ordinary bluetooth handsfree device. Surprisingly I achieved connecting with the device over Bluetoothsocket and I received the first message that starts the whole communication protocol (AT+BRSF:27) after which all communication broke and I did not receive any answer after sending anything. So this gives me some hope that I can connect with a device but I do not know how to continue.
This is the most relevant code:
/**
* This thread runs while listening for incoming connections. It behaves
* like a server-side client. It runs until a connection is accepted
* (or until cancelled).
*/
private class AcceptThread extends Thread {
// The local server socket
private final BluetoothServerSocket mmServerSocket;
public AcceptThread() {
BluetoothServerSocket tmp = null;
// Create a new listening server socket
try {
tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
} catch (IOException e) {
Log.e(TAG, "listen() failed", e);
}
mmServerSocket = tmp;
}
public void run() {
if (D) Log.d(TAG, "BEGIN mAcceptThread" + this);
setName("AcceptThread");
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
socket = mmServerSocket.accept();
Log.i(TAG, ">>>>>>>>>>>>>>>>>>> SOCKEET accept() <<<<<<<<<<<<<<<<<<<<<<");
} catch (IOException e) {
Log.e(TAG, "accept() failed", e);
break;
}
// If a connection was accepted
if (socket != null) {
synchronized (BluetoothChatService.this) {
switch (mState) {
case STATE_LISTEN:
case STATE_CONNECTING:
// Situation normal. Start the connected thread.
connected(socket, socket.getRemoteDevice());
break;
case STATE_NONE:
case STATE_CONNECTED:
// Either not ready or already connected. Terminate new socket.
try {
socket.close();
} catch (IOException e) {
Log.e(TAG, "Could not close unwanted socket", e);
}
break;
}
}
}
}
if (D) Log.i(TAG, "END mAcceptThread");
}
public void cancel() {
if (D) Log.d(TAG, "cancel " + this);
try {
mmServerSocket.close();
} catch (IOException e) {
Log.e(TAG, "close() of server failed", e);
}
}
}
/**
* 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;
public ConnectThread(BluetoothDevice device) {
mmDevice = device;
BluetoothSocket tmp = null;
// Get a BluetoothSocket for a connection with the
// given BluetoothDevice
try {
// tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
tmp = InsecureBluetooth.createRfcommSocket(mmDevice, 10, false);
} catch (IOException e) {
Log.e(TAG, "create() failed", e);
}
mmSocket = tmp;
}
public void run() {
Log.i(TAG, "BEGIN mConnectThread");
setName("ConnectThread");
// Always cancel discovery because it will slow down a connection
mAdapter.cancelDiscovery();
// Make a connection to the BluetoothSocket
try {
// This is a blocking call and will only return on a
// successful connection or an exception
mmSocket.connect();
} catch (IOException e) {
connectionFailed();
// Close the socket
try {
mmSocket.close();
} catch (IOException e2) {
Log.e(TAG, "unable to close() socket during connection failure", e2);
}
// Start the service over to restart listening mode
BluetoothChatService.this.start();
return;
}
// Reset the ConnectThread because we're done
synchronized (BluetoothChatService.this) {
mConnectThread = null;
}
// Start the connected thread
connected(mmSocket, mmDevice);
}
You can notice that a line is commented out (tmp = device.createRfcommSocketToServiceRecord(MY_UUID);), which is the regular way of creating a BluetoothSocket but that way I could not connect even with the regular handsfree device. I have also tried using the reflection-way but that did not work either.
I have also tried any combination of paired/unpaired connected/disconnected status with no luck.
I crave for any tips.

Related

How to ensure a Bluetooth connection is closed when it was started in another activity?

The APP I am developing connects to a Bluetooth Printer (the microFlash 2te for those curious) .
The Bluetooth connection is managed by a BluetoothConnectionManager class with a message handler at activity level to handle the information from the BluetoothChatService.
When I leave the activity in a controlled environment I make sure to disconnect the Bluetooth connection by stopping the bluetoothConnectionManager which stops all threads.
However when the user exits with the back button I cannot ensure the threads are stopped. This causes issues if I try and connect the socket as the Bluetooth device is already connected.
Is there away I can find and use or kill the bluetoothConnectionManager created by another activity? Or is there a way to ensure that a bluetoothConnectionManager stops when an activity is closed?
Attached is my connect thread class inside the BluetoothConnectionManager:
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device) {
mmDevice = device;
BluetoothSocket tmp = null;
// Get a BluetoothSocket for a connection with the
// given BluetoothDevice
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
Log.e(TAG, "create() failed", e);
}
mmSocket = tmp;
}
public void run() {
Log.i(TAG, "BEGIN mConnectThread");
setName("ConnectThread");
// Always cancel discovery because it will slow down a connection
mAdapter.cancelDiscovery();
//stop();
// Make a connection to the BluetoothSocket
try {
// This is a blocking call and will only return on a
// successful connection or an exception
d("About to connect to socket...");
mmSocket.connect();
} catch (IOException e) {
connectionFailed();
// Close the socket
try {
mmSocket.close();
} catch (IOException e2) {
d(e2.getMessage().toString());
Log.e(TAG,
"unable to close() socket during connection failure",
e2);
}
return;
}
// Reset the ConnectThread because we're done
synchronized (BluetoothConnectionManager.this) {
mConnectThread = null;
}
// Start the connected thread
connected(mmSocket, mmDevice);
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "close() of connect socket failed", e);
}
}
}

android bluetooth connect with remote device

I want connect from my app in android device to remote device (paired). remote device is a module HC-05. my cod is :
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device) {
// Use a temporary object that is later assigned to mmSocket,
// because mmSocket is final
BluetoothSocket tmp = null;
mmDevice = device;
// Get a BluetoothSocket to connect with the given BluetoothDevice
try {
// MY_UUID is the app's UUID string, also used by the server
// code
tmp = mmDevice.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
}
mmSocket = tmp;
}
#Override
public void run() {
// Cancel discovery because it will slow down the connection
ba.cancelDiscovery();
try {
mmSocket.connect();
} catch (IOException e) {}
// Do work to manage the connection (in a separate thread)
// manageConnectedSocket(mmSocket);
// connected();
tv1.setText("connect");
}
/** Will cancel an in-progress connection, and close the socket */
#SuppressWarnings("unused")
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
}
}
}
but get error in line mSocket.connect().
when run my app then get message : unfortunately (app name) has stopped.
please help.
Do not silently ignore IOException. If you get IOException that means you. for any reason, could not create socket. mSocket then remains null and so you get the exception.
Might be you do not have bluetooth permission in manifest.

Method undefined: manageConnectedSocket()

We want to remote a roboter via Bluetooth. Now we are using the android example. we want to connect as client. The code before this part is working. Now we get an error at:
manageConnectedSocket(mmSocket);
"The method is undefined". What can we do to solve this problem ? Thanks for answers.
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device) {
// Use a temporary object that is later assigned to mmSocket,
// because mmSocket is final
BluetoothSocket tmp = null;
mmDevice = device;
// Get a BluetoothSocket to connect with the given BluetoothDevice
try {
// MY_UUID is the app's UUID string, also used by the server code
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) { }
mmSocket = tmp;
}
public void run() {
// Cancel discovery because it will slow down the connection
mBluetoothAdapter.cancelDiscovery();
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect();
} catch (IOException connectException) {
// Unable to connect; close the socket and get out
try {
mmSocket.close();
} catch (IOException closeException) { }
return;
}
// Do work to manage the connection (in a separate thread)
manageConnectedSocket(mmSocket);
}
/** Will cancel an in-progress connection, and close the socket */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
According to this, manageConnectedSocket doesn't exist.
manageConnectedSocket() is a fictional method in the application that
will initiate the thread for transferring data, which is discussed in
the section about Managing a Connection.
And you haven't defined it in your code. You need to create the method yourself, or follow the rest of the Android tutorial.

Connect two Android devices using bluetooth programmatically

I am doing a bluetooth project in which i want to connect two devices using blutooth programaticaly.
I am following the guidelines and codes of tf developer.android.com.Can any one help me to resolve this issue?Here is the code i have tried.
Can anyone also tell me the from where the constructor receives the devices object?
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
<!-- can anyone tell me from where device object comes here --!>
public ConnectThread(BluetoothDevice device) {
// Use a temporary object that is later assigned to mmSocket,
// because mmSocket is final
BluetoothSocket tmp = null;
mmDevice = device;
// Get a BluetoothSocket to connect with the given BluetoothDevice
try {
// MY_UUID is the app's UUID string, also used by the server code
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) { }
mmSocket = tmp;
}
public void run() {
// Cancel discovery because it will slow down the connection
mBluetoothAdapter.cancelDiscovery();
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect();
} catch (IOException connectException) {
// Unable to connect; close the socket and get out
try {
mmSocket.close();
} catch (IOException closeException) { }
return;
}
// Do work to manage the connection (in a separate thread)
manageConnectedSocket(mmSocket);
}
/** Will cancel an in-progress connection, and close the socket */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
Thanks inadvance
Here's some of my code that accomplishes the task.
/**
* Searches the list of paired devices for the device. Returns
* if found, throws Exception otherwise.
*
* #param sTargetDeviceName
* #return BluetoothDevice
*/
private BluetoothDevice findDevice(String sTargetDeviceName)
throws Exception {
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> devices = adapter.getBondedDevices();
for (BluetoothDevice device : devices) {
String sDeviceName = device.getName().trim();
if (sDeviceName.startsWith(sTargetDeviceName)) {
return device;
}
}
throw new Exception("Device " + sTargetDeviceName + " not found");
}
To use the above code, the device must already be paired.
Please see the documentation:
http://developer.android.com/guide/topics/wireless/bluetooth.html
specifically the "Finding Devices" section.

Linux Bluetooth not finding Android Service with UUID

I am trying to write a program on an embedded system running GUMSTIX(Linux) to connect and talk to an Android 2.x device over bluetooth. The GUMSTIX is the client and the Android is the server. I am trying to find the channel number that my Android service uses so that the GUMSTIX can connect to it but for some reason my routine isn't returning a channel number because it doesn't seem to find a service with a matching UUID.
I suspect that the UUID provided to the GUMSTIX routine and the UUID on the Android device are not actually the same number. Android requires a 128 bit UUID :
From the Android Documentation:
UUID is an immutable representation of
a 128-bit universally unique
identifier (UUID).
There are multiple, variant layouts of
UUIDs, but this class is based upon
variant 2 of RFC 4122, the Leach-Salz
variant. This class can be used to
model alternate variants, but most of
the methods will be unsupported in
those cases; see each method for
UUID used in android:
public static final String UUID_STRING = "00000000-0000-0000-0000-00000000ABCD";
private static final UUID MY_UUID = UUID.fromString(UUID_STRING);
C Code in GUMSTIX Look for comment indicating where it fails
int main(int argc , char **argv)
{
//Android wants a 128 bit UUID why are we only giving a 32 bit UUID
uint32_t svc_uuid_int[] = { 0 , 0 , 0 , 0xABCD } ;
int status ;
bdaddr_t target ;
uuid_t svc_uuid ;
sdp_list_t *response_list , *search_list , *attrid_list ;
sdp_session_t *session = 0;
uint32_t range = 0x0000ffff ;
uint8_t port = 0;
if(argc < 2)
{
fprintf(stderr , "usage: %s <bt_addr>\n" , argv [ 0 ] ) ;
exit ( 2 ) ;
}
str2ba ( argv[1] , &target ) ;
// connect to the SDP server running on the remote machine
session = sdp_connect ( BDADDR_ANY, &target, SDP_RETRY_IF_BUSY );
// printf("session %u\n",session);
sdp_uuid128_create( &svc_uuid, &svc_uuid_int ) ;
search_list = sdp_list_append( 0, &svc_uuid ) ;
attrid_list = sdp_list_append( 0, &range ) ;
// get a list of service records that have UUID 0xabcd
response_list = NULL ; //ERROR: response_list SHOULD GET INITIALIZED BUT IT STAYS NULL CAUSING THE PROGRAM TO NEVER ENTER THE FOR LOOP BELOW.
status = sdp_service_search_attr_req(session , search_list , SDP_ATTR_REQ_RANGE , attrid_list, &response_list ) ;
printf("status %d\n",status);
if( status == 0 )
{
sdp_list_t *proto_list = NULL ;
sdp_list_t *r = response_list ;
// go through each of the service records
for ( ; r ; r = r->next )
{
sdp_record_t *rec = (sdp_record_t * ) r->data ;
// get a list of the protocol sequences
if( sdp_get_access_protos( rec, &proto_list ) == 0 )
{
// get the RFCOMM port number
port = sdp_get_proto_port( proto_list , RFCOMM_UUID ) ;
sdp_list_free( proto_list, 0 );
}
sdp_record_free( rec ) ;
}
}
sdp_list_free( response_list, 0 );
sdp_list_free( search_list, 0 );
sdp_list_free( attrid_list, 0 );
sdp_close( session ) ;
if( port != 0 )
{
printf( "found service running on RFCOMM port %d\n" , port ) ;
}
return 0;
}
EDIT:
Android code for the acceptThread(accepts connections), ConnectThread(completes the connection), and ConnectedThread(maintains the connection, establish handler)
/**
* This thread runs while listening for incoming connections. It behaves
* like a server-side client. It runs until a connection is accepted
* (or until canceled).
*/
private class AcceptThread extends Thread {
// The local server socket
private final BluetoothServerSocket mmServerSocket;
public AcceptThread() {
BluetoothServerSocket tmp = null;
// Create a new listening server socket
try {
tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
} catch (IOException e) {
Log.e(TAG, "listen() failed", e);
}
mmServerSocket = tmp;
}
public void run() {
if (D) Log.d(TAG, "BEGIN mAcceptThread" + this);
setName("AcceptThread");
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
if(D) Log.i("prism", "Waiting to connect************");
socket = mmServerSocket.accept();
if(D) Log.i("prism", "We have accepted connection and are connected***************");
} catch (IOException e) {
Log.e(TAG, "accept() failed", e);
break;
}
// If a connection was accepted
if (socket != null) {
synchronized (BluetoothServer.this) {
switch (mState) {
case STATE_LISTEN:
case STATE_CONNECTING:
// Situation normal. Start the connected thread.
connected(socket, socket.getRemoteDevice());
break;
case STATE_NONE:
case STATE_CONNECTED:
// Either not ready or already connected. Terminate new socket.
try {
if (D) Log.i("prism", "Bluetooth already connected, abandoning request from " + socket.getRemoteDevice().getName());
socket.close();
} catch (IOException e) {
Log.e(TAG, "Could not close unwanted socket", e);
}
break;
}
}
}
}
if (D) Log.i(TAG, "END mAcceptThread");
}
public void cancel() {
if (D) Log.d(TAG, "cancel " + this);
try {
mmServerSocket.close();
} catch (IOException e) {
Log.e(TAG, "close() of server failed", e);
}
}
}
/**
* 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;
public ConnectThread(BluetoothDevice device) {
mmDevice = device;
BluetoothSocket tmp = null;
// Get a BluetoothSocket for a connection with the
// given BluetoothDevice
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
Log.e(TAG, "create() failed", e);
}
mmSocket = tmp;
}
public void run() {
Log.i(TAG, "BEGIN mConnectThread");
setName("ConnectThread");
// Always cancel discovery because it will slow down a connection
mAdapter.cancelDiscovery();
// Make a connection to the BluetoothSocket
try {
// This is a blocking call and will only return on a
// successful connection or an exception
Log.i(TAG, "mmSocket.connect() is initiaiting in the ConnectThread");
mmSocket.connect();
Log.i(TAG, "mmSocket.connect() complete...");
} catch (IOException e) {
Log.e(TAG, "Connection attempt failed, closing the socket");
connectionFailed();
// Close the socket
try {
mmSocket.close();
} catch (IOException e2) {
Log.e(TAG, "unable to close() socket during connection failure", e2);
}
// Start the service over to restart listening mode
BluetoothServer.this.start();
return;
}
// Reset the ConnectThread because we're done
synchronized (BluetoothServer.this) {
mConnectThread = null;
}
// Start the connected thread
connected(mmSocket, mmDevice);
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "close() of connect socket failed", e);
}
}
}
/**
* This thread runs during a connection with a remote device.
* It handles all incoming and outgoing transmissions.
*/
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
Log.d(TAG, "create ConnectedThread");
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the BluetoothSocket input and output streams
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "temp sockets not created", e);
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];
int bytes;
// Keep listening to the InputStream while connected
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(Bluetooth.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}
}
The Android code was adopted from the Bluetooth Chat example here
Do you have any service (typically over RFCOMM/SPP) with UUID { 0 , 0 , 0 , 0xABCD } running on the Android device ?
You will probably (programmatically create the service with the specified UUID and have it running on the device to be able to connect to it.
Quoting from Android Documentation:
**
public BluetoothServerSocket listenUsingRfcommWithServiceRecord (String name, UUID uuid)
Create a listening, secure RFCOMM Bluetooth socket with Service Record.
A remote device connecting to this socket will be authenticated and communication on this socket will be encrypted.
Use accept() to retrieve incoming connections from a listening BluetoothServerSocket.
The system will assign an unused RFCOMM channel to listen on.
The system will also register a Service Discovery Protocol (SDP) record with the local SDP server containing the specified UUID, service name, and auto-assigned channel. Remote Bluetooth devices can use the same UUID to query our SDP server and discover which channel to connect to. This SDP record will be removed when this socket is closed, or if this application closes unexpectedly.
Use createRfcommSocketToServiceRecord(UUID) to connect to this socket from another device using the same UUID.
**

Categories

Resources