好的,我再试一次。
我在 ubuntu 中有一个设备 sdptool,我的设备说明了以下内容:
# 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
如您所见,该设备确实支持 RFCOMM 协议和用于文件传输的 OBEX。我的 android 应用程序有一个简单的代码,它试图通过不安全的 RFCOMM 通道连接到这个设备,只是为了没有用户交互。我想连接到这个设备,所以我使用设备 mac-address 进行连接,并且套接字已准备好,logcat 是这样说的。
但我只得到错误:
Connection refused
请记住,java 代码中的 mac-address 与上面列出的不同。
所以这是我的代码:
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();
}
});
}
}
我已经阅读了几个地方,它应该通过取消配对并再次配对来工作,但这并不能解决我的问题。