【问题标题】:Android 蓝牙:Connect()/Disconnect()
【发布时间】:2011-04-01 08:15:48
【问题描述】:

我目前正在设计一个需要连接到设备、写入/读取数据并可靠关闭连接的应用程序。目前我有写/读稳固。我的断开连接然后重新连接非常不可靠,并且实际上经常使手机崩溃..有时是 Eclipse。我一直在浏览许多文章试图弄清楚它......没有运气......

****连接功能**

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

【问题讨论】:

    标签: android bluetooth connect


    【解决方案1】:

    你用的是什么手机?什么操作系统?看到这个答案: Disconnect a bluetooth socket in Android

    关闭在某些 HTC 2.1update1 手机上实际上无法正常工作

    【讨论】:

    • 好的。我正在使用 HTC Incredible 2.1update1 操作系统。我看到了那个帖子,但我也看到它已经被回答并尝试了这些答案,但没有积极的结果。你知道超人是否/何时应该得到 Froyo?不过谢谢!
    猜你喜欢
    • 2013-03-31
    • 1970-01-01
    • 2013-10-25
    • 1970-01-01
    • 2021-03-18
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多