Android蓝牙:连接()/断开连接()

Android蓝牙:连接()/断开连接(),android,bluetooth,connect,Android,Bluetooth,Connect,我目前正在设计一个应用程序,它需要连接到设备,写入/读取数据,并可靠地关闭连接。目前我有写/读实体。我的断开然后重新连接是非常不可靠的,而且经常会使手机崩溃。。有时还会发生月蚀。我看了很多文章试图弄明白这一点。。不走运 ****连接函数** public boolean connect() { ConfigData.getInstance(); BluetoothSocket tmp = null; BluetoothDevice device = ConfigData.m_Shar

我目前正在设计一个应用程序,它需要连接到设备,写入/读取数据,并可靠地关闭连接。目前我有写/读实体。我的断开然后重新连接是非常不可靠的,而且经常会使手机崩溃。。有时还会发生月蚀。我看了很多文章试图弄明白这一点。。不走运

****连接函数**

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();
  }
 }
因此,断开连接成功并返回空值。问题是,当我重新连接时,它会在第二次连接尝试时阻塞,或者只是坐在那里,或者完全崩溃手机,导致多次重新启动

这里有人有什么建议吗?这非常令人沮丧。任何帮助都将不胜感激

谢谢,给他们表演!
TxAg

你在用什么电话?什么操作系统?看看这个答案:


在某些HTC 2.1更新版手机上,close实际上无法正常工作

您使用的是什么手机?什么操作系统?看看这个答案:


在某些HTC 2.1更新版手机上,close实际上无法正常工作

好的。我使用的是HTC难以置信的2.1更新版操作系统。我看到了那篇文章,但我也看到它已经得到了回答,并尝试了那些答案,但没有任何积极的结果。你知道《不可思议》会在什么时候拍到弗罗约吗?谢谢你!可以我使用的是HTC难以置信的2.1更新版操作系统。我看到了那篇文章,但我也看到它已经得到了回答,并尝试了那些答案,但没有任何积极的结果。你知道《不可思议》会在什么时候拍到弗罗约吗?谢谢你!