2013-03-13 63 views
8

我正在开发Android应用程序。这个应用程序应该与蓝牙(BT)设备通信(发送一些字节)。我在我的设备(Samsung Galaxy mini)上调试/运行此应用时遇到问题。当我创建BT套接字并停止调试时,电话会冻结,我必须通过取出电池重新启动它。在运行这个应用程序的情况下(从Eclipse)一切正常,但是当我尝试再次运行它时,电话冻结和应用程序未安装。如果我在第二次运行之前尝试卸载此应用程序,手机会再次冻结。这是一个有问题的代码:蓝牙插槽冻结手机

private final BluetoothDevice mmDevice; 
private UUID uuid; 

public ConnectionThread(BluetoothDevice device) { 
    Log.d(TAG, "create ConnectionThread"); 

    uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 
    BluetoothSocket tmp = null; 
    mmDevice = device; 

    try { 
     tmp = mmDevice.createRfcommSocketToServiceRecord(uuid); 
    } catch (IOException e) { } 
    mmSocket = tmp; 
    socketConnected = true; 
} 

这是线程的构造函数。当我评论线

tmp = mmDevice.createRfcommSocketToServiceRecord(uuid); 

手机不冻结,所以问题是与创建套接字(不连接)。每次调试或运行后重新启动手机都非常烦人,我还得做很多工作。

如果我从手机上运行这个应用程序(从Eclipse断开连接),它没有任何问题。任何想法可能是一个问题或如何解决它?谢谢。

+3

听起来像一个固件错误,不是吗? – 2013-03-13 11:15:13

+0

@CodePainters:固件或IDE错误。我发现了一个相同的主题:http://stackoverflow.com/questions/4408287/android-bluetooth-socket-freeze-application。所以如果我在onDestroy回调中关闭BT,一切都可以。 – DanielH 2013-03-13 12:51:28

+1

IDE?不太可能。而且Android无论如何都充满了bug ... – 2013-03-13 12:54:50

回答

0

我正在使用SGSIII mini进行开发。下面的代码很适合我:

private class ConnectThread extends Thread { 
    private final BluetoothSocket mmSocket; 
    private final BluetoothDevice mmDevice; 

    public ConnectThread(BluetoothDevice device) { 
     mmDevice = device; 

     BluetoothSocket tmp = null; 

     // Get a BluetoothSocket for a connection with the 
     // given BluetoothDevice 
     try { 
      //tmp = device.createRfcommSocketToServiceRecord(MY_UUID); 
      tmp = device.createInsecureRfcommSocketToServiceRecord(MY_UUID); 
     } catch (IOException e) { 
      Log.e(LOG_TAG, "create() failed", e); 
     } 
     mmSocket = tmp; 

     Main.myBluetoothSocket = mmSocket; 
     Main.myBluetoothDevice = mmDevice; 
    } 

    @Override 
    public void run() { 
     Log.i(LOG_TAG, "BEGIN mConnectThread"); 
     setName("ConnectThread"); 

     // Always cancel discovery because it will slow down a connection 
     mAdapter.cancelDiscovery(); 

     // Send a failure message back to the Activity 
     Message msg = mHandler.obtainMessage(MESSAGE_TOAST); 
     Log.e(LOG_TAG, "Attempting connection to " + mmSocket.getRemoteDevice().getName()); 
     String ss = "Attempting connection to " + mmSocket.getRemoteDevice().getName(); 
     Bundle bundle = new Bundle(); 
     bundle.putString(TOAST, ss); 
     msg.setData(bundle); 
     mHandler.sendMessage(msg); 

     // Make a connection to the BluetoothSocket 
     try { 
      // This is a blocking call and will only return on a 
      // successful connection or an exception 
      mmSocket.connect(); 
     } catch (IOException e) { 
      Log.e(LOG_TAG, "*+*+*+* Connection Failed"); 
      connectionFailed(); 
      // Close the socket 
      try { 
       mmSocket.close(); 
      } catch (IOException e2) { 
       Log.e(LOG_TAG, "unable to close() socket during connection failure", e2); 
      } 
      // Start the service over to restart listening mode 
      BluetoothCommandService.this.start(); 
      return; 
     } 

     // Reset the ConnectThread because we're done 
     synchronized (BluetoothCommandService.this) { 
      mConnectThread = null; 
     } 

     // Start the connected thread 
     connected(mmSocket, mmDevice); 
    } 

    public void cancel() { 
     try { 
      mmSocket.close(); 
     } catch (IOException e) { 
      Log.e(LOG_TAG, "close() of connect socket failed", e); 
     } 
    } 
} 
0

,我也面临着你可以使用反射法同样的问题,将工作

Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}); 
BluetoothSocket socket = socket = (BluetoothSocket) m.invoke(device, 1);