Method undefined: manageConnectedSocket() - android

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.

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);
}
}
}

How to pair with bluetooth device in android?

Hello Everyone I am working on Bluetooth and I want to pair my device with the finded bluetooth and connect with the paired bluetooth.
I want to know how to do this. And I have also read about client server approach in which we use bluetoothserver socket and bluetooth socket and listenUsingRfcommWithServiceRecord and createRfcommSocketToServiceRecord methods in which we pass mac and uuid.
I want to know where we use this approach and how to find the remote device UUId. Thanks in advance.
You don't need to use the mentioned methods, at least for pairing the devices.
Try using Intents to pair. This code might let you get more familiar with Bluetooth.
public void pairDevice(BluetoothDevice device)
{
String ACTION_PAIRING_REQUEST = "android.bluetooth.device.action.PAIRING_REQUEST";
Intent intent = new Intent(ACTION_PAIRING_REQUEST);
String EXTRA_DEVICE = "android.bluetooth.device.extra.DEVICE";
intent.putExtra(EXTRA_DEVICE, device);
String EXTRA_PAIRING_VARIANT = "android.bluetooth.device.extra.PAIRING_VARIANT";
int PAIRING_VARIANT_PIN = 0;
intent.putExtra(EXTRA_PAIRING_VARIANT, PAIRING_VARIANT_PIN);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
Intent intent = new Intent(BluetoothDevice.ACTION_PAIRING_REQUEST);
intent.putExtra(EXTRA_DEVICE, device);
int PAIRING_VARIANT_PIN = 272;
intent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, PAIRING_VARIANT_PIN);
sendBroadcast(intent);
Intent intent = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS);
startActivityForResult(intent, REQUEST_PAIR_DEVICE);
If you want a better approach on how to connect these paired devices, take a look at Bluetooth on AndroidDevelopers:
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) { }
}
}
Link resources:
http://pastebin.com/N8dR4Aa1
http://developer.android.com/guide/topics/connectivity/bluetooth.html#ConnectingAsAClient

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.

Connecting android device with a handsfree car radio via bluetooth

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.

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.

Categories

Resources