【问题标题】:Bluetooth socket freeze phone蓝牙插座冻结电话
【发布时间】:2013-03-01 01:45:06
【问题描述】:

我正在为 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 断开),它可以正常工作。任何想法可能是一个问题或如何解决它?谢谢。

【问题讨论】:

  • 听起来像是固件错误,不是吗?
  • @CodePainters:固件或 IDE 错误。我发现了一个相同的主题:stackoverflow.com/questions/4408287/…。因此,如果我在 onDestroy 回调中关闭 BT,一切正常。
  • IDE?不太可能。无论如何,Android 充满了错误......
  • 我个人会尝试使用有根设备进行定期调试 - 从顶部到 strace 再到 gdb。这就是开源的力量,当然,如果你有时间的话。
  • 好吧,如果我直接从手机运行这个应用程序,就可以了。但是如果我从 Eclipse 运行或调试它,手机就会死机。确实只有少数开发人员使用 BT API,因此可能会有很多未隐藏的错误...

标签: java android sockets bluetooth rfcomm


【解决方案1】:

我也在使用 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);
        }
    }
}

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2013-01-05
      • 1970-01-01
      • 2021-08-07
      • 2011-03-03
      • 1970-01-01
      • 1970-01-01
      • 2011-05-23
      • 2011-08-27
      相关资源
      最近更新 更多