0

Ok, I will try one more time.

I have a device sdptool in ubuntu the following is stated from my device:

# sdptool browse C0:1B:DC:1F:E2:F1 Browsing C0:1B:DC:1F:E2:F1 ... Service Name: OBEX Object Push Service RecHandle: 0x10000 Service Class ID List: "OBEX Object Push" (0x1105) Protocol Descriptor List: "L2CAP" (0x0100) "RFCOMM" (0x0003) Channel: 9 "OBEX" (0x0008) Profile Descriptor List: "OBEX Object Push" (0x1105) Version: 0x0100 

As you can se the device does support the RFCOMM protocol, and OBEX for file transfer. I have a simple code for my android app which tries to connect to this device over a insecure RFCOMM channel, just for no user interaction. I want to connect to this device, so Iam using the device mac-address for connection, and the socket is ready, logcat says so.

But I only get the error:

Connection refused 

Have in mind that the mac-address in the java code is different from the following listed above.

So here is my 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 = new UUID(0x0100 , 0x1000); // MY_UUID = UUID.fromString("8e1f0cf7-508f-4875-b62c-fbb67fd34812"); connectButton = (Button)findViewById(R.id.button1); connectButton.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { BluetoothSocket tmp = null; BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice("00:1B:DC:0F:EC:7E"); Method m = null; try { m = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class}); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { tmp = (BluetoothSocket) m.invoke(device, 1); } catch (IllegalArgumentException e) { Toast.makeText(getApplicationContext(), "Exception: " + e.getMessage(), Toast.LENGTH_LONG).show(); } catch (IllegalAccessException e) { Toast.makeText(getApplicationContext(), "Exception: " + e.getMessage(), Toast.LENGTH_LONG).show(); e.printStackTrace(); } catch (InvocationTargetException e) { Toast.makeText(getApplicationContext(), "Exception: " + e.getMessage(), Toast.LENGTH_LONG).show(); e.printStackTrace(); } try { tmp.connect(); } catch (IOException e) { Toast.makeText(getApplicationContext(), "Exception: " + e.getMessage(), Toast.LENGTH_LONG).show(); try { tmp.close(); } catch (IOException e1) { Toast.makeText(getApplicationContext(), "Socket closed!" + e.getMessage(), Toast.LENGTH_LONG).show(); } } boolean con = tmp.isConnected(); if(con) Toast.makeText(getApplicationContext(), "Connection was made!", Toast.LENGTH_LONG).show(); else Toast.makeText(getApplicationContext(), "Connection was not made!", Toast.LENGTH_LONG).show(); } }); } 

}

I've read several places that it should work by un-pairing and pair again, but this doesn't solve my problem.

1 Answer 1

2

Well, your question is over a month old, but in case you're still looking for the answer, here's one:

sdptool indicates that your RFCOMM channel is 9, but in your code you have:

tmp = (BluetoothSocket) m.invoke(device, 1);

Instead of 1 as your last argument, try 9 (if that doesn't work, try other integers, like 2 through 15).

You may also want to check out this answer.

There seem to be many variations of this question on Stack, but not a lot of understanding of the "standard" recommended code (i.e., the bluetooth chat sample modified with the lines that define and invoke "Method m")--I know because I'm one of those people who struggled with this. I was trying to connect my phone to my MacBook, and got the "connection refused" message until I realized that I needed to use 5 in the line of code above.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.