2013-02-28 161 views
2

我無法啓動Java服務器(使用bluecove 2.1.1 on Windows 7 x64, external bluetooth dongle)和Android客戶端(OS版本2.3.6)之間的藍牙連接。Android BluetoothSocket :: connect()拋出異常

設備發現正常工作,但無法連接到PC上運行的服務(BluetoothSocket::connect() throw an exception)。下面是服務器和客戶端(我用的bluetooth dongleMAC address降低代碼長度)的一個非常原始的版本:

Java服務器

import java.io.*; 
import javax.microedition.io.*; 
import javax.bluetooth.*; 

public class RFCOMMServer { 

public static void main(String args[]) { 
    try { 

     StreamConnectionNotifier service = (StreamConnectionNotifier) Connector 
       .open("btspp://localhost:" 
         + new UUID("0000110100001000800000805F9B34FB", 
           false).toString() + ";name=helloService"); 

     StreamConnection conn = (StreamConnection) service.acceptAndOpen(); 
     System.out.println("Connected"); 

     DataInputStream in = new DataInputStream(conn.openInputStream()); 
     DataOutputStream out = new DataOutputStream(conn.openOutputStream()); 

     String received = in.readUTF();   // Read from client 
     System.out.println("received: " + received); 

     out.writeUTF("Echo: " + received);  // Send Echo to client 

     conn.close(); 
     service.close(); 
    } catch (IOException e) { 
     System.err.print(e.toString()); 
    } 
} 
} 

Android客戶端

import java.io.DataInputStream; 
import java.io.DataOutputStream; 
import java.util.UUID; 
import android.app.ListActivity; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.bluetooth.BluetoothSocket; 
import android.os.Bundle; 
import android.os.Handler; 
import android.widget.LinearLayout; 
import android.widget.ArrayAdapter; 

public class AndroidBluetoothEchoClientActivity extends ListActivity { 
LinearLayout layout; 

private ArrayAdapter<String> mArrayAdapter; 

final Handler handler = new Handler(); 

final Runnable updateUI = new Runnable() { 
    public void run() { 
     mArrayAdapter.add(bluetoothClient.getBluetoothClientData()); 
    } 
}; 

BluetoothClient bluetoothClient; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    mArrayAdapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_list_item_1); 
    this.setListAdapter(mArrayAdapter); 
    bluetoothClient = new BluetoothClient(handler, updateUI); 
    bluetoothClient.start(); 
} 
} 

class BluetoothClient extends Thread { 

BluetoothAdapter mBluetoothAdapter; 
private String data = null; 

final Handler handler; 
final Runnable updateUI; 

public BluetoothClient(Handler handler, Runnable updateUI) { 
    this.handler = handler; 
    this.updateUI = updateUI; 

    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
} 

public String getBluetoothClientData() { 
    return data; 
} 

public void run() { 
    BluetoothSocket clientSocket = null; 
             // Client knows the MAC address of server 
    BluetoothDevice mmDevice = mBluetoothAdapter 
      .getRemoteDevice("00:15:83:07:CE:27"); 

    try { 

     clientSocket = mmDevice.createRfcommSocketToServiceRecord(UUID 
       .fromString("00001101-0000-1000-8000-00805F9B34FB")); 

     mBluetoothAdapter.cancelDiscovery(); 

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

     out.writeUTF("Hello");   // Send to server 

     data = in.readUTF();   // Read from server 
     handler.post(updateUI); 
    } catch (Exception e) { 
    } 
} 
} 

這是問題的核心(我猜):

BluetoothDevice mmDevice = mBluetoothAdapter 
      .getRemoteDevice("00:15:83:07:CE:27");//normally get device 

    try { 

     clientSocket = mmDevice.createRfcommSocketToServiceRecord(UUID 
       .fromString("00001101-0000-1000-8000-00805F9B34FB"));//here also no problem 

     mBluetoothAdapter.cancelDiscovery(); 

     clientSocket.connect();//here throw an exception 

我試着用ref經文:

Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}); 
clientSocket = (BluetoothSocket) m.invoke(device, 1); 

,但它也不能正常工作。

一個多星期我無法解決這個問題,如果您幫助做出決定,我將不勝感激。

這裏是日誌文件:

02-27 08:32:33.656: W/ActivityThread(10335): Application edu.ius.rwisman.AndroidBluetoothEchoClient is waiting for the debugger on port 8100... 
02-27 08:32:33.687: I/System.out(10335): Sending WAIT chunk 
02-27 08:32:33.882: I/System.out(10335): Debugger has connected 
02-27 08:32:33.882: I/System.out(10335): waiting for debugger to settle... 
02-27 08:32:34.085: I/System.out(10335): waiting for debugger to settle... 
02-27 08:32:34.289: I/System.out(10335): waiting for debugger to settle... 
02-27 08:32:34.492: I/System.out(10335): waiting for debugger to settle... 
02-27 08:32:34.687: I/System.out(10335): waiting for debugger to settle... 
02-27 08:32:34.890: I/System.out(10335): waiting for debugger to settle... 
02-27 08:32:35.093: I/System.out(10335): waiting for debugger to settle... 
02-27 08:32:35.296: I/System.out(10335): debugger has settled (1493) 
02-27 08:32:35.390: I/ApplicationPackageManager(10335): cscCountry is not German : SER 
02-27 08:32:35.875: D/dalvikvm(10335): threadid=9: still suspended after undo (sc=1 dc=1) 
02-27 08:32:45.351: D/BluetoothSocket(10335): create BluetoothSocket: type = 1, fd = -1, uuid = [00001101-0000-1000-8000-00805f9b34fb], port = -1 
02-27 08:32:45.351: D/BLZ20_WRAPPER(10335): blz20_init: initializing... 
02-27 08:32:45.351: D/BTL_IFC_WRP(10335): wsactive_init: init active list 
02-27 08:32:45.460: D/BLZ20_WRAPPER(10335): blz20_init: success 
02-27 08:32:45.460: D/BTL_IFC_WRP(10335): wrp_sock_create: CTRL 
02-27 08:32:45.460: D/BTL_IFC_WRP(10335): wrp_alloc_new_sock: wrp_alloc_new_sock sub 1 
02-27 08:32:45.460: D/BTL_IFC_WRP(10335): wrp_sock_create: 43 
02-27 08:32:45.460: D/BTL_IFC_WRP(10335): wrp_sock_connect: wrp_sock_connect brcm.bt.btlif:9000 (43) 
02-27 08:32:45.460: D/BTL_IFC_WRP(10335): wrp_sock_connect: BTLIF_MAKE_LOCAL_SERVER_NAME return name: brcm.bt.btlif.9000 
02-27 08:32:45.460: D/BTL_IFC_WRP(10335): wrp_sock_connect: wrp_sock_connect ret:-1 server name:brcm.bt.btlif.9000 
02-27 08:32:45.460: E/BTL_IFC_WRP(10335): ##### ERROR : wrp_sock_connect: connect failed (Connection refused)##### 
02-27 08:32:45.460: E/BTL_IFC(10335): ##### ERROR : btl_ifc_ctrl_connect: control channel failed Connection refused##### 
02-27 08:32:45.460: D/BTL_IFC_WRP(10335): wrp_close_full: wrp_close (43:-1) [brcm.bt.btlif] 
02-27 08:32:45.460: D/BTL_IFC_WRP(10335): wsactive_del: delete wsock 43 from active list [ad3bc380] 

回答

0

問題解決了,我的設備上簡單的驗證碼,並在PC上是不同的,但Windows顯示在配對設備列表中的設備。我修復設備,它的工作原理。