2011-03-18 205 views
0

我已經使用藍牙連接的BluetoothChat示例程序的BluetoothChatService類。我已經像Android藍牙連接與串口設備

改性它

私有靜態最後UUID MY_UUID = UUID.fromString( 「00001101-0000-1000-8000-00805F9B34FB」);

將其連接到串行端口設備。

我的樣品測試Android設備是NexusOne,HTC Desire,LG Optimus,摩托羅拉Droid。

每當我使用我的應用程序將其連接到硬件時,它都會使用NexusOne正確連接和斷開連接。但是,當他們連接時使用其他Android設備時,有時甚至在嘗試100次後都無法連接。有時當我斷開連接時,應用程序斷開連接,但硬件上的藍牙指示燈亮起,表示連接仍處於開啓狀態。我想知道如果我的編碼錯誤,或硬件錯誤,或Android操作系統藍牙庫錯誤。我沒有面對NexusOne的這個問題。我一直無法確定問題出在哪裏的確切位置。

有人可以指出我應該採取什麼行動來解決這個問題嗎?給出


代碼 「無法連接裝置」 吐司消息

/**

* Constructor. Prepares a new BluetoothChat session. 

* @param context The UI Activity Context 

* @param handler A Handler to send messages back to the UI Activity 

*/ 


public BluetoothChatService(Context context, Handler handler) { 
    mAdapter = BluetoothAdapter.getDefaultAdapter(); 
    mState = STATE_NONE; 
    mHandler = handler; 
} 

/** 
* Set the current state of the chat connection 
* @param state An integer defining the current connection state 
*/ 
private synchronized void setState(int state) { 
    if (D) Log.d(TAG, "setState() " + mState + " -> " + state); 
    mState = state; 

    // Give the new state to the Handler so the UI Activity can update 
    mHandler.obtainMessage(BluetoothChat.MESSAGE_STATE_CHANGE, state, -1).sendToTarget(); 
} 

/** 
* Return the current connection state. */ 
public synchronized int getState() { 
    return mState; 
} 

/** 
* Start the chat service. Specifically start AcceptThread to begin a 
* session in listening (server) mode. Called by the Activity onResume() */ 
public synchronized void start() { 
    if (D) Log.d(TAG, "start"); 

    // Cancel any thread attempting to make a connection 
    if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;} 

    // Cancel any thread currently running a connection 
    if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;} 

    setState(STATE_LISTEN); 

    // Start the thread to listen on a BluetoothServerSocket 
    if (mSecureAcceptThread == null) { 
     mSecureAcceptThread = new AcceptThread(true); 
     mSecureAcceptThread.start(); 
    } 
    if (mInsecureAcceptThread == null) { 
     mInsecureAcceptThread = new AcceptThread(false); 
     mInsecureAcceptThread.start(); 
    } 
} 

/** 
* Start the ConnectThread to initiate a connection to a remote device. 
* @param device The BluetoothDevice to connect 
* @param secure Socket Security type - Secure (true) , Insecure (false) 
*/ 
public synchronized void connect(BluetoothDevice device, boolean secure) { 
    if (D) Log.d(TAG, "connect to: " + device); 

    // Cancel any thread attempting to make a connection 
    if (mState == STATE_CONNECTING) { 
     if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;} 
    } 

    // Cancel any thread currently running a connection 
    if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;} 

    // Start the thread to connect with the given device 
    mConnectThread = new ConnectThread(device, secure); 
    mConnectThread.start(); 
    setState(STATE_CONNECTING); 
} 

/** 
* Start the ConnectedThread to begin managing a Bluetooth connection 
* @param socket The BluetoothSocket on which the connection was made 
* @param device The BluetoothDevice that has been connected 
*/ 
public synchronized void connected(BluetoothSocket socket, BluetoothDevice 
     device, final String socketType) { 
    if (D) Log.d(TAG, "connected, Socket Type:" + socketType); 

    // Cancel the thread that completed the connection 
    if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;} 

    // Cancel any thread currently running a connection 
    if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;} 

    // Cancel the accept thread because we only want to connect to one device 
    if (mSecureAcceptThread != null) { 
     mSecureAcceptThread.cancel(); 
     mSecureAcceptThread = null; 
    } 
    if (mInsecureAcceptThread != null) { 
     mInsecureAcceptThread.cancel(); 
     mInsecureAcceptThread = null; 
    } 

    // Start the thread to manage the connection and perform transmissions 
    mConnectedThread = new ConnectedThread(socket, socketType); 
    mConnectedThread.start(); 

    // Send the name of the connected device back to the UI Activity 
    Message msg = mHandler.obtainMessage(BluetoothChat.MESSAGE_DEVICE_NAME); 
    Bundle bundle = new Bundle(); 
    bundle.putString(BluetoothChat.DEVICE_NAME, device.getName()); 
    msg.setData(bundle); 
    mHandler.sendMessage(msg); 

    setState(STATE_CONNECTED); 
} 

/** 
* Stop all threads 
*/ 
public synchronized void stop() { 
    if (D) Log.d(TAG, "stop"); 

    if (mConnectThread != null) { 
     mConnectThread.cancel(); 
     mConnectThread = null; 
    } 

    if (mConnectedThread != null) { 
     mConnectedThread.cancel(); 
     mConnectedThread = null; 
    } 

    if (mSecureAcceptThread != null) { 
     mSecureAcceptThread.cancel(); 
     mSecureAcceptThread = null; 
    } 

    if (mInsecureAcceptThread != null) { 
     mInsecureAcceptThread.cancel(); 
     mInsecureAcceptThread = null; 
    } 
    setState(STATE_NONE); 
} 

/** 
* Write to the ConnectedThread in an unsynchronized manner 
* @param out The bytes to write 
* @see ConnectedThread#write(byte[]) 
*/ 
public void write(byte[] out) { 
    // Create temporary object 
    ConnectedThread r; 
    // Synchronize a copy of the ConnectedThread 
    synchronized (this) { 
     if (mState != STATE_CONNECTED) return; 
     r = mConnectedThread; 
    } 
    // Perform the write unsynchronized 
    r.write(out); 
} 

/** 
* Indicate that the connection attempt failed and notify the UI Activity. 
*/ 
private void connectionFailed() { 
    // Send a failure message back to the Activity 
    Message msg = mHandler.obtainMessage(BluetoothChat.MESSAGE_TOAST); 
    Bundle bundle = new Bundle(); 
    bundle.putString(BluetoothChat.TOAST, "Unable to connect device"); 
    msg.setData(bundle); 
    mHandler.sendMessage(msg); 

    // Start the service over to restart listening mode 
    BluetoothChatService.this.start(); 
} 

/** 
* Indicate that the connection was lost and notify the UI Activity. 
*/ 
private void connectionLost() { 
    // Send a failure message back to the Activity 
    Message msg = mHandler.obtainMessage(BluetoothChat.MESSAGE_TOAST); 
    Bundle bundle = new Bundle(); 
    bundle.putString(BluetoothChat.TOAST, "Device connection was lost"); 
    msg.setData(bundle); 
    mHandler.sendMessage(msg); 

    // Start the service over to restart listening mode 
    BluetoothChatService.this.start(); 
} 
+1

什麼是硬件?你有Android藍牙日誌,你可以發佈顯示問題。你的代碼是做什麼的?請張貼您希望我們查看的代碼部分 – 2011-03-18 15:55:48

+0

它是一個定製的硬件,不管日誌是否顯示任何錯誤跟蹤。當我嘗試除NexusOne之外連接它們時,我會收到Toast消息「無法連接設備」。我在我的問題中添加了代碼段,這導致了這個Toast消息。 – 2011-03-19 08:34:24

回答

0

那麼代碼有一些錯誤...他們解決了一次,我仔細閱讀了活動的完整生命週期並據此實施。給出的課程很好。

0

是否使用了 「定製硬件」 編寫的服務器代碼的UUID ,與Android手機上使用的UUID相匹配?也許Nexus One可能是使用Gingerbread(?)的唯一手機(在你的手機中),這意味着比以前的Android版本更高的API級別。瀏覽與藍牙有關的薑餅的變更日誌。這可能是原因。