Android蓝牙例外

Android蓝牙例外,android,bluetooth,Android,Bluetooth,好的,我再试一次 我在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: "

好的,我再试一次

我在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地址进行连接,并且插座已经准备好,logcat这样说

但我只得到一个错误:

Connection refused
请记住,java代码中的mac地址与上面列出的以下地址不同

这是我的代码:

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

            }
    });  
}
}


我读过好几个地方,它应该通过取消配对并再次配对来工作,但这并不能解决我的问题

好吧,你的问题已经提了一个多月了,但如果你还在寻找答案,这里有一个:

sdptool表示您的RFCOMM通道为9,但在代码中您有:

tmp=(BluetoothSocket)m.invoke(设备,1)

请尝试9,而不是1作为最后一个参数(如果这不起作用,请尝试其他整数,如2到15)

您可能还想退房

这个问题在堆栈上似乎有很多变体,但对“标准”推荐代码(即,用定义和调用“方法m”的行修改的bluetooth聊天示例)的理解不多——我知道,因为我是与此相关的人之一。我正试图将手机连接到MacBook上,收到“连接被拒绝”的信息,直到我意识到我需要在上面的代码行中使用5