Reputation: 8981
Iam trying to connect my android device with a bluetooth compatible device. I know the mac-address of this device, as you can see in my code below. I also made several Toasts
just to verify the steps in the code. It seems that I manage to create tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
I assume this because the Toast raises a message saying some bluetooth object address, in this line of code:
String test = tmp.toString();
Toast.makeText(getApplicationContext(), "The bluetooth socket: " +test, Toast.LENGTH_LONG).show();
But the code failes semantically when I do a tmp.connect();
I am working on API level 15, android 4.0
This i my piece of code
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class SimpleConnectAndroidActivity extends Activity {
final static String toast = "IAM HERE";
final static String TAG ="SimpleConnect";
UUID MY_UUID;
BluetoothDevice bd;
BluetoothAdapter ba;
Button connectButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
ba = BluetoothAdapter.getDefaultAdapter();
connectButton = (Button)findViewById(R.id.button1);
connectButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
boolean b = ba.checkBluetoothAddress("00:1B:DC:0F:EC:7E");
BluetoothSocket tmp = null;
//If valid bluetoothAddress
if(b) {
final BluetoothDevice device = ba.getRemoteDevice("00:1B:DC:0F:EC:7E"); //Getting the Verifier Bluetooth
//Trying to create a RFCOMM socket to the device
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
}
try {
tmp.connect();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "No connection was made! Exception: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
//Print out the sockets name
String test = tmp.toString();
Toast.makeText(getApplicationContext(), "The bluetooth socket: " +test, Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(getApplicationContext(), "FALSE, No bluetooth device with this address", Toast.LENGTH_LONG).show();
}
}
});
}
}
I am aware of that this should be done in a separate thread, and yes, I read the Bluetooth Chat example provided by Google.
Can someone help me out?
EDIT
This is the only thing I got from LogCat:
07-06 10:10:22.085: V/BluetoothSocket.cpp(14019): ...fd 63 created (RFCOMM, lm = 26)
EDIT 2
Ok, now got a new error, but its a better error. When i press the connect button, the phone asks for the pin code for pair the devices, but its already paired! After I paired it for the second time, the error says: Connection refused and another exception says, timeout.
Upvotes: 0
Views: 3431
Reputation: 157
you can use this code:Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
mySocket = (BluetoothSocket) m.invoke(device, 1);
Upvotes: 2