I hope you are fine
I have a app where I want to have a simple connection through the bluetooth with the Arduino board and send simple commands to board To test, I first tried a connection for two Android devices This is my code:
btn_1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
try {
findbt();
Onbt();
}
catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
});
void Onbt() throws IOException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException
{
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
//mysocket = mydevice.createRfcommSocketToServiceRecord(uuid);
try {
mysocket = mydevice.createRfcommSocketToServiceRecord(uuid);
}
catch (Exception e) {
Log.e("tag", "Error creating socket");
}
try {
mysocket.connect();
Log("Connected");
}
catch (IOException e) {
Log(e.getMessage());
try {
Log("trying fallback...");
mysocket = (BluetoothSocket) mydevice.getClass().getMethod("createRfcommSocket", new Class[]{ int.class }).invoke(mydevice, 1);
mysocket.connect();
Log("Connected");
}
catch (Exception e2) {
Log(e2.getMessage());
Log("Couldn't establish Bluetooth connection!");
}
myoustream = mysocket.getOutputStream();
myinputstream = mysocket.getInputStream();
beginlisenfordata();
}
}
private void Log(String message) {
// TODO Auto-generated method stub
TextView txt = (TextView) findViewById(R.id.textView1);
txt.setText(txt.getText() + "\n" + message);
}
void findbt()
{
myblutooth = BluetoothAdapter.getDefaultAdapter();
if (myblutooth == null)
{
text_1.setText("any device not found");
}
if ( !myblutooth.isEnabled())
{
Intent enabledbt = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enabledbt, 0);
}
Set<BluetoothDevice> pairDevices = myblutooth.getBondedDevices();
Iterator iter = pairDevices.iterator();
while (iter.hasNext()) {
Log.i("tag", " device : " + iter.next());
}
// mydevice = myblutooth.getRemoteDevice("10:D5:42:7C:85:08");
mydevice = myblutooth.getRemoteDevice("3C:05:5C:B5:DC:4D");
if (pairDevices.contains(mydevice))
{
text_1.setText("device is ready :" + mydevice.getName());
}
}
But he gave me this error on connect line :
Connection failed: read failed, socket might closed or timeout, read ret: ‑1
I tried any method like fallback , ... but fail Can someone help me?
I am try this solution but not working for me !