2012-12-26 48 views
5

Tôi đang cố gửi tin nhắn từ máy khách Android đến Mac OS X qua bluetooth. Tôi đang sử dụng thư viện bluetooth bluecove 2.0.1 trên Mac OS X Snow Leopard.không thể kết nối với ổ cắm bluetooth trên android

Mã cho Server:

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

} 

}

Mã dành cho Android khách hàng:

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

} 

}

Tôi không thể kết nối với máy chủ mặc dù UUID là giống nhau cho cả máy khách và máy chủ. Android ném ngoại lệ: Khám phá dịch vụ không thành công. Tuy nhiên tôi có thể in tên của thiết bị từ xa (máy khách) trên máy chủ. Do đó acceptAndOpen() không thể chấp nhận kết nối ổ cắm.

Vui lòng giúp tôi hiểu tại sao tôi không thể clientSocket.connect(); trên Android?

+0

Bạn đã giải quyết được vấn đề này chưa? Tôi đang gặp vấn đề chính xác và tôi dường như không thể làm cho nó hoạt động được. – blastervla

Trả lời

1

Tôi sẽ đoán và nói rằng nó có liên quan đến các số UUID bạn đã sử dụng. Chúng chỉ phụ thuộc vào loại thiết bị bạn sử dụng. Vì vậy, hãy chắc chắn rằng bạn nhìn những người lên và rằng họ là chính xác cho các thiết bị Android. Khi tôi đang làm android điều này stumped tôi trong một thời gian dài. UUID không phải là thứ bạn đã đặt. Dưới đây là một liên kết How can I get the UUID of my Android phone in an application? Hoặc này Android - Get Bluetooth UUID for this device

Nếu đó không phải là nó. Khám phá không thành công trên cả hai đầu? bạn có thấy thiết bị ở hai đầu không? Bên nào bạn có thể in tên? Bạn có thể muốn xem qua chương trình mẫu bluetooth của google. Và sử dụng nó để giúp bạn bắt đầu.

Các vấn đề liên quan