无法连接到android上的蓝牙套接字

无法连接到android上的蓝牙套接字,android,bluetooth,bluecove,Android,Bluetooth,Bluecove,我正在尝试通过蓝牙从android客户端向MacOSX发送消息。 我正在Mac OS X Snow Leopard上使用bluecove 2.0.1 Java蓝牙库 服务器代码: public class EchoServer2 { private static final String UUID_STRING = "00001101-0000-1000-8000-00805F9B34FB"; // 32 hex digits private static final String SERVIC

我正在尝试通过蓝牙从android客户端向MacOSX发送消息。 我正在Mac OS X Snow Leopard上使用bluecove 2.0.1 Java蓝牙库

服务器代码:

public class EchoServer2 {
private static final String UUID_STRING = "00001101-0000-1000-8000-00805F9B34FB"; // 32 hex digits
private static final String SERVICE_NAME = "echoserver";

private LocalDevice mLocalDevice;

public EchoServer2() {
    try {
        mLocalDevice = LocalDevice.getLocalDevice();
    } catch(IOException e) {
        System.err.print("Error connection to bluetooth");
    }   
}

public void start() throws IOException {
    StreamConnectionNotifier connectionNotifier =
        (StreamConnectionNotifier) Connector.open(
                "btspp://localhost:" + UUID_STRING +
                ";name=" + SERVICE_NAME + ";authenticate=false");

    System.out.println("Bluetooth Address: " + mLocalDevice.getBluetoothAddress());

    System.out.println("Waiting for a connection...");
    StreamConnection streamConnection = connectionNotifier.acceptAndOpen();

    System.out.println("Found a new device.");

    RemoteDevice device = RemoteDevice.getRemoteDevice(streamConnection);
    System.out.println("New Device connected: " + device.getFriendlyName(false).toString());

    DataInputStream is = streamConnection.openDataInputStream();

    byte[] bytes = new byte[1024];
    int r;
    while((r = is.read(bytes)) > 0) {
        System.out.println(new String(bytes, 0, r));
    }

}
}

Android客户端的代码:

public class MainActivity extends Activity {

private static final String TAG = "MainActivity";

EditText editText;
TextView textView;

String send_msg;
String rcv_msg;

private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); // 32 hex digits

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Log.d(TAG, "onCreate");

    editText = (EditText) findViewById(R.id.edit_msg);
    textView = (TextView) findViewById(R.id.rcv_msg);

    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();

    if(adapter == null) {
        textView.append("Bluetooth NOT Supported!");
        return;
    }

    // Request user to turn ON Bluetooth
    if(!adapter.isEnabled()) {
        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(intent, RESULT_OK);
    }




}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

public void onClick(View view) {
    Log.d(TAG, "onClick");
    new SendMessageToServer().execute(send_msg);
}

private class SendMessageToServer extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... msg) {
        Log.d(TAG, "doInBackground");

        BluetoothSocket clientSocket = null;
        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        mBluetoothAdapter.enable();
        // Client knows the server MAC address 
        BluetoothDevice mmDevice = mBluetoothAdapter.getRemoteDevice("00:25:00:C3:1C:FE");
        Log.d(TAG, "got hold of remote device");
        Log.d(TAG, "remote device: " + mmDevice.getName().toString());

        try {
            // UUID string same used by server 
            clientSocket = mmDevice.createInsecureRfcommSocketToServiceRecord(MY_UUID);

            Log.d(TAG, "bluetooth socket created");

            mBluetoothAdapter.cancelDiscovery();    // Cancel, discovery slows connection

            clientSocket.connect();
            Log.d(TAG, "connected to server");

            DataInputStream in = new DataInputStream(clientSocket.getInputStream());
            DataOutputStream out = new DataOutputStream(clientSocket.getOutputStream());

            out.writeUTF(msg[0]);           // Send message to server
            Log.d(TAG, "Message Successfully sent to server");
            return in.readUTF();            // Read response from server
        } catch (Exception e) {

            Log.d(TAG, "Error creating bluetooth socket");
            Log.d(TAG, e.getMessage());

            return "";
        }

    }

    @Override
    protected void onPostExecute(String result) {
        Log.d(TAG, "onPostExecute");
        rcv_msg = result;
        textView.setText(rcv_msg);
    }

}
公共类MainActivity扩展活动{
私有静态最终字符串TAG=“MainActivity”;
编辑文本编辑文本;
文本视图文本视图;
字符串发送消息;
字符串rcv_msg;
私有静态最终UUID MY_UUID=UUID.fromString(“00001101-0000-1000-8000-00805F9B34FB”);//32个十六进制数字
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(标记为“onCreate”);
editText=(editText)findViewById(R.id.edit_msg);
textView=(textView)findViewById(R.id.rcv_msg);
BluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
if(适配器==null){
append(“不支持蓝牙!”);
返回;
}
//请求用户打开蓝牙
如果(!adapter.isEnabled()){
意向意向=新意向(BluetoothAdapter.ACTION\u REQUEST\u ENABLE);
startActivityForResult(意图、结果_OK);
}
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.activity\u主菜单);
返回true;
}
公共void onClick(视图){
Log.d(标记为“onClick”);
新建SendMessageToServer().execute(发送消息);
}
私有类SendMessageToServer扩展异步任务{
@凌驾
受保护的字符串doInBackground(字符串…msg){
Log.d(标签“doInBackground”);
BluetoothSocket clientSocket=null;
BluetoothAdapter mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter.enable();
//客户端知道服务器的MAC地址
BluetoothDevice mmDevice=mBluetoothAdapter.getRemoteDevice(“00:25:00:C3:1C:FE”);
Log.d(标签“抓到远程设备”);
Log.d(标记,“远程设备:+mmDevice.getName().toString());
试一试{
//UUID字符串与服务器使用的相同
clientSocket=mmDevice.createInsurerCommsocketToServiceRecord(我的UUID);
Log.d(标记“创建蓝牙插座”);
mBluetoothAdapter.cancelDiscovery();//取消,发现会减慢连接速度
clientSocket.connect();
Log.d(标记“连接到服务器”);
DataInputStream in=新的DataInputStream(clientSocket.getInputStream());
DataOutputStream out=新的DataOutputStream(clientSocket.getOutputStream());
out.writeUTF(msg[0]);//向服务器发送消息
Log.d(标记“消息成功发送到服务器”);
返回.readUTF();//从服务器读取响应
}捕获(例外e){
Log.d(标记“创建蓝牙套接字时出错”);
Log.d(标记,例如getMessage());
返回“”;
}
}
@凌驾
受保护的void onPostExecute(字符串结果){
Log.d(标记“onPostExecute”);
rcv_msg=结果;
textView.setText(rcv_msg);
}
}
}

即使客户端和服务器的
UUID
相同,我也无法连接到服务器。 Android引发异常:服务发现失败。 但是,我可以在服务器上打印远程设备(客户端)的名称。因此,
acceptendopen()
无法接受套接字连接


请帮助我理解为什么我无法
clientSocket.connect()
;在android上?

我猜一下,这与你使用的UUID号码有关。它们完全取决于您使用的设备类型。因此,请确保您查找这些内容,并确保它们适用于android设备。当我在做安卓的时候,这让我很难堪。 UUID不是你设定的东西。 这里有一个链接 还是这个

如果不是这样的话。 发现号两端都失败了吗?你能看到两端的设备吗?你可以把名字印在哪一面?
你可能想看看谷歌的蓝牙示例程序。然后用它来开始。

你解决了这个问题吗?我有完全相同的问题,我似乎无法使它工作。