2010-08-19 223 views
2

我目前正在設計一個應用程序,它需要連接到設備,寫入/讀取數據,並可靠地關閉連接。目前我有寫/讀固體。我的斷開連接然後重新連接非常不可靠,並且經常實際上使手機崩潰。我一直在尋找通過大量文章試圖弄清楚和..沒有運氣..Android藍牙:連接()/斷開()

****連接功能**

public boolean connect() 
{ 
    ConfigData.getInstance(); 
    BluetoothSocket tmp = null; 
    BluetoothDevice device = ConfigData.m_SharedBluetoothDevice; 
    Method m; 
    try { 

    tmp = device.createRfcommSocketToServiceRecord(MY_UUID);//(BluetoothSocket) 
    m.invoke(device, 1); 
    } catch (SecurityException e) { 
    e.printStackTrace(); 
    } catch (IllegalArgumentException e) { 
    e.printStackTrace(); 
    } 
    catch (IOException e) { 
    e.printStackTrace(); 
    } 
     ConfigData.m_SharedBluetoothSocket = tmp; 
     try { 
    ConfigData.m_SharedBluetoothSocket.connect(); 
    ConfigData.bIsBTConnected = true; 
    } catch (IOException e) { 
    try { 
    closeSocket(); 
    m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}); 
    tmp = (BluetoothSocket) m.invoke(device, 1); 
    } catch (SecurityException e1) { 
    e1.printStackTrace(); 
    } catch (NoSuchMethodException e1) { 
    e1.printStackTrace(); 
    } catch (IllegalArgumentException e1) { 
    e.printStackTrace(); 
    } catch (IllegalAccessException e1) { 
    e.printStackTrace(); 
    } catch (InvocationTargetException e1) { 
    e.printStackTrace(); 
    } 
    ConfigData.m_SharedBluetoothSocket = tmp; 
    try { 
    ConfigData.m_SharedBluetoothSocket.connect(); 
    ConfigData.bIsBTConnected = true; 
    } catch (IOException e1) { 
    ConfigData.m_BluetoothException += e1.toString(); 
    ConfigData.bIsBTConnected = false; 
    return false; 

    } 
    e.printStackTrace(); 
    return true; 
    } 
     return true; 
} 

****斷開功能**

public void destroySocket() 
{ 
    try { 
     if(m_InStream != null) 
     { 
     m_InStream.close(); 
     m_InStream = null; 
     } 
     if(m_OutStream != null) 
     { 
     m_OutStream.close(); 
     m_OutStream = null; 
     } 
     if(ConfigData.m_SharedBluetoothSocket != null) 
     { 
     ConfigData.m_SharedBluetoothSocket.close(); 
     ConfigData.m_SharedBluetoothSocket = null; 
     } 
     if(m_InStream == null && m_OutStream == null && ConfigData.m_SharedBluetoothSocket == null) 
    { 
     ConfigData.bIsBTConnected = false; 
    } 
    } catch (IOException e1) { 
    m_InStream = null; 
    m_OutStream = null; 
    ConfigData.m_SharedBluetoothSocket = null; 
    e1.printStackTrace(); 
    } 
} 

因此斷開連接成功並返回一切爲空。問題是,當我在第二次連接嘗試中重新連接數據塊時,它們會坐在那裏或完全崩潰,導致幾次重新啓動。

有沒有人有任何建議嗎?它非常令人沮喪。任何幫助將非常感激!!

謝謝Gig'Em! TxAg

回答

0

你在用什麼手機?什麼OS?看到這個答案: Disconnect a bluetooth socket in Android

緊密實際上是不正確的一些HTC的工作2.1update1手機

+0

好。我正在使用HTC Incredible 2.1update1操作系統。我看到那篇文章,但我也看到它已經回答並嘗試了這些答案,並且沒有任何積極的結果。你知道是否/何時Incredible應該獲得Froyo? 雖然謝謝! – TxAg 2010-08-23 13:51:33