2010-12-09 79 views
2

我已經嘗試了其他評論中的所有建議,但無濟於事,希望有人能幫助我。我一直在爲這個問題掙扎三天。我很確定我的UUID是正確的,我知道在清單中啓用了藍牙訪問。android.bluetooth.BluetoothSocket無法連接

我試圖將我的android應用程序連接到在Fedora中運行的python服務器。它一直在間歇地工作,而現在還沒有完成。我通常收到的android異常是沿着..的線。當btSocket.connect();在下面的代碼中執行。

12-09 05:08:42.331: ERROR/BluetoothService(676): java.io.IOException: Service discovery failed 

12-09 05:27:00.757: ERROR/BluetoothService(729): java.io.IOException: Service discovery failed 

這是應該採取一切照顧我的Android藍牙Class。當主應用程序類收到套接字已連接的消息時,線程啓動。我的藍牙類基於http://www.anddev.org/viewtopic.php?p=35487#35487

package spin.halo; 

import java.io.*; 
import java.lang.reflect.InvocationTargetException; 
import java.lang.reflect.Method; 
import java.util.UUID; 

import android.bluetooth.*; 
import android.os.Handler; 
import android.util.Log; 

public class BluetoothService extends Thread{ 

    private static final String TAG = "BluetoothService"; 
    private static final boolean D = true; 
    private BluetoothAdapter mBluetoothAdapter = null; 
    private BluetoothSocket btSocket = null; 
    private OutputStream outStream = null; 
    private InputStream inStream = null; 
    private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 

    private static String address; 

    private Handler appHandler; 

    public BluetoothService(Handler h) { 
     if (D) 
      Log.e(TAG, "+++ ON CREATE +++"); 

     appHandler = h; 

     mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
     if (mBluetoothAdapter == null) { 
      Log.e(TAG, "NO BT ADAPTER!"); 
      return; 
     } 

     if (!mBluetoothAdapter.isEnabled()) { 
      Log.e(TAG, "Bluetooth is not enabled!"); 
      return; 
     } 

     if (D) 
      Log.e(TAG, "+++ DONE IN ON CREATE, GOT LOCAL BT ADAPTER +++"); 
    } 

    public void connectToServer() { 
     connectToServer("60:33:4B:25:0D:37"); 
    } 

    public void connectToServer(String serverMacAddress) { 

     address = serverMacAddress; 
     // 
     if (D) { 
      Log.e(TAG, "+ ABOUT TO ATTEMPT CLIENT CONNECT +"); 
     } 

     BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); 
     Log.v(TAG, "REMOTE DEVICE: " + device.toString()); 

     try { 
      btSocket = device.createRfcommSocketToServiceRecord(MY_UUID); 
      Log.v(TAG, "SOCKET: " + btSocket.toString()); 
     } catch (Exception e) { 
      Log.e(TAG, "ON RESUME: Socket creation failed.", e); 
     } 

     /* Discovery may be going on, e.g., if you're running a 
     'scan for devices' search from your handset's Bluetooth 
     settings, so we call cancelDiscovery(). It doesn't hurt 
     to call it, but it might hurt not to... discovery is a 
     heavyweight process; you don't want it in progress when 
     a connection attempt is made.*/ 
     mBluetoothAdapter.cancelDiscovery(); 

     // Blocking connect, for a simple client nothing else can 
     // happen until a successful connection is made, so we 
     // don't care if it blocks. 

     try { 
      btSocket.connect(); 
      Log.e(TAG, "ON RESUME: BT connection established, data transfer link open."); 
      appHandler.sendMessage(appHandler.obtainMessage(ValidationApp.BT_CONNECTION_MADE, "")); 
     } catch (IOException e) { 
      try { 
       Log.e(TAG, "ON RESUME: Could not connect", e); 
       btSocket.close(); 
      } catch (IOException e2) { 
       Log.e(TAG, "ON RESUME: Unable to close socket during connection failure", e2); 
      } 
     } 

     // Create output stream 
     try { 
      outStream = btSocket.getOutputStream(); 
     } catch (IOException e) { 
      Log.e(TAG, "ON RESUME: Output stream creation failed.", e); 
     } 

     // Create input stream 
     try { 
      inStream = btSocket.getInputStream(); 
     } catch (IOException e) { 
      Log.e(TAG, "Input stream creation failed.", e); 
     } 
    } 

    public void write(String message) { 
     if(message.length() > 0) { 
      byte[] msgBuffer = message.getBytes(); 
      try { 
       outStream.write(msgBuffer); 
      } catch (IOException e) { 
       Log.e(TAG, "ON RESUME: Exception during write.", e); 
      } 
     } 
    } 

    public void run() { 
     LineNumberReader mLineReader = new LineNumberReader(new InputStreamReader(inStream)); 
     while(true) { 
      try { 
       String message = mLineReader.readLine(); 

       if(D) {Log.v(TAG, "Bluetooth says: " + message);} 
       Log.v(TAG, appHandler.obtainMessage(ValidationApp.BT_MESSAGE, message).toString()); 
       appHandler.sendMessage(appHandler.obtainMessage(ValidationApp.BT_MESSAGE, message)); 
      } catch (IOException e) { 
       Log.e(TAG, "startListen: ", e); 
      } 
     } 
    } 
} 

我的python代碼的關鍵部分如下。我對這段代碼非常有信心。

# pybluez library 
import bluetooth 

server_socket = bluetooth.BluetoothSocket(bluetooth.RFCOMM) 
client_sockets = [] 

server_socket.bind(("",bluetooth.PORT_ANY)) 
port = server_socket.getsockname()[1] 
uuid = "00001101-0000-1000-8000-00805F9B34FB" 

print "Listening for devices..." 

# advertise service 
server_socket.listen(1) 
bluetooth.advertise_service(server_socket, "Validation Host", 
    service_id = uuid, 
    service_classes = [ uuid, bluetooth.SERIAL_PORT_CLASS ], 
    profiles = [ bluetooth.SERIAL_PORT_PROFILE ], 
) 

# accept incoming connections 
client_sock, client_info = server_socket.accept() 
client_sockets.append(client_sock) 
print "Accepted Connection from ", client_info 

感謝您的關注。

回答

3

你的代碼看起來總體上很好,我假設你只是從一些例子中複製並粘貼它。

在一些Android手機中存在一個錯誤,例如HTC渴望,導致device.createRfcommSocketToServiceRecord方法失敗。我建議以下方法:

1)試圖在 提供兩臺Linux計算機使用pythong腳本之間的聊天(你知道) 由您驗證Linux安裝是否正常工作。

2)嘗試啓動從計算機到Android的連接(使用android-bluetooth-chat-c​​lient-python)請注意,默認的BluetoothChat演示只能在第一次嘗試時才能接受連接。

3)嘗試從Android手機連接到Linux電腦,但使用手動指定下面的代碼

// BUGBUG: Following code is not properly implemented on HTC DESIRE 
// mSocket =     device.createRfcommSocketToServiceRecord(UUID.fromString("6a462dc0-703a-4bf3-a80e-a473a6332c64")); 
// WORKAROUND: Connecting directly to RFCOMM channel 1 
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] { int.class }); 
mSocket = (BluetoothSocket) m.invoke(device, Integer.valueOf(1)); // 1==RFCOMM channel code 

你需要找出哪些是使用

# sdptool browse local 
+0

你的RFCOMM通道號RFCOMM通道號你先生是聖人! – 2011-01-17 04:04:09