2010-08-19 224 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