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

发布于 2024-09-14 19:07:31 字数 2561 浏览 4 评论 0原文

我目前正在设计一个应用程序,需要连接到设备、写入/读取数据并可靠地关闭连接。目前,我的写/读能力很稳定。我的断开连接然后重新连接的方式非常不可靠,而且经常会导致手机崩溃,有时还会导致 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();
  }
 }

因此断开连接成功并返回所有内容null。问题是,当我重新连接时,它会在第二次连接尝试时阻塞,要么只是坐在那里,要么完全使手机崩溃,导致多次重新启动。

这里有人有什么建议吗?

I am currently designing an app which needs to connect to a device, write/read data, and close the connection reliably. Currently, I have the write/read solid. My disconnect and then reconnect is terribly unreliable, and often actually crashes the phone and, sometimes, Eclipse.

CONNECT FUNCTION

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;
 }

Disconnect Function

 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();
  }
 }

So the disconnect is successful and returns everything null. The problem is that when I reconnect it blocks at the 2nd connect attempt and either just sits there or completely crashes the phone, causing several reboots.

Does anyone have any advice here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

找回味觉 2024-09-21 19:07:31

你用什么手机?什么操作系统?看这个答案:
在 Android 中断开蓝牙套接字

关闭实际上在某些 HTC 上无法正常工作2.1更新1部手机

What phone are you using? What OS? See this answer:
Disconnect a bluetooth socket in Android

The close is actually not working properly on some HTC 2.1update1 phones

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文