【问题标题】:Application doesn't load when Bluetooth Connection thread is called调用蓝牙连接线程时应用程序不加载
【发布时间】:2021-12-18 13:13:25
【问题描述】:

我正在制作一个应该连接到 Arduino HC-06 模块的 Android 应用程序。

我查看了Android Documentation 并尝试让它与提供的代码一起使用。

但是,当我尝试在三星 A32 5G 上运行代码时,所有视图都无法加载(空白屏幕),连接未建立,我从 Logcat 获得以下输出:

2021-11-04 10:25:32.798 15490-15546/com.example.carremote D/StrictMode: StrictMode policy violation: android.os.strictmode.LeakedClosableViolation: A resource was acquired at attached stack trace but never released.

我查看了该消息,对我来说这似乎是由以下代码引起的:

@Override
public void run()
{
    try
    {
        while (true)
        {
            BS = BSS.accept();
            Log.println(Log.INFO,TAG, "[*] Looking for Connection");

            if(BS != null)
            {
                x = new EstablishedBTConnection(BS);
                Log.println(Log.INFO, TAG, "[*] Connection established!");
                x.manageConnection(1);
                BSS.close();
                break;
            }
        }
    } catch (IOException e)
    {
        e.printStackTrace();
        Log.println(Log.INFO, TAG, "[*] Connection failed!");
    }
}

具体由:

BS = BSS.accept();

我不知道为什么这会给我这个消息或为什么连接失败。

如果您需要任何其他代码,我可以提供它们,并且如果这个问题是重复的(我无法找到任何类似的问题),只需用链接写评论,我会删除它。

任何帮助表示赞赏,在此先感谢!

编辑:

这是请求的其余代码:

@Override
protected void onCreate(Bundle savedInstanceState)
{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButtonFunctions(); //Functions to manage the Buttons
        bluetoothFunctionality.run(); //Starts the Bluetooth thread
}

蓝牙线程的构造函数,直接取自 Android 文档。

public BluetoothFunctionality() throws IOException
{
    // Use a temporary object that is later assigned to mmServerSocket
    // because mmServerSocket is final.
    BluetoothServerSocket tmp = null;
    try {
        // MY_UUID is the app's UUID string, also used by the client code.
        tmp = BA.listenUsingRfcommWithServiceRecord(name, UUID);
    } catch (IOException e) {
        Log.e(TAG, "Socket's listen() method failed", e);

    }
    BSS = tmp;
}

EstablishedBTConnection的代码:

public class EstablishedBTConnection
{
private BluetoothSocket BS;
public EstablishedBTConnection(BluetoothSocket BS)
{
    this.BS = BS;
}

public int manageConnection(int action)
{
    if(action == 1)
    {
        try
        {
            BS.getOutputStream().write(50);
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    return 0;
}

}

【问题讨论】:

  • 也分享你的另一段代码。
  • 您从哪里执行 Runnable?在线程内部还是直接调用 run()?
  • @cincy_anddeveloper 我添加了您要求的代码,我从 onCreate() 调用运行。
  • @Danish 我已经添加了我的 onCreate() 函数,你还需要其他代码 sn-ps 吗?

标签: java android android-studio error-handling bluetooth


【解决方案1】:

但是,当我尝试在三星 A32 5G 上运行代码时,所有视图都无法加载(空白屏幕),连接未建立

可能代码会阻塞 UI 线程,直到蓝牙连接建立(需要更多关于如何调用 run 方法的详细信息:它是 Runnable 实现吗?如果是,那么它是如何执行的?)。实际上必须确保提供的代码在新线程中执行。因为如果代码没有在额外的线程中执行,UI 线程(或者也称为主线程)根本无法获得绘制 UI 的控件。 在official doccumentation 中查看有关 Android UI 线程的更多详细信息。 文档中的规则之一:

不要阻塞 UI 线程

为了解决这个问题,应该在后台线程或服务中执行长时间运行的代码(在特定情况下建立蓝牙连接)。例如,在同一文档页面上阅读 worker threads

关于 logcat 输出

StrictMode policy violation: android.os.strictmode.LeakedClosableViolation: A resource was acquired at attached stack trace but never released.

线

BSS.close();

仅在连接成功的情况下执行。但无论如何,所有资源必须释放。更好的方法是使用try-with-resources 语句。应该使用Android Gradle plugin 3.0.0 或更高版本。

还有关于与 Arduino HC-06 模块的连接。您能否提供更多详细信息或代码? BSS 变量到底是什么,它是如何初始化的? EstablishedBTConnection的代码是什么?

【讨论】:

  • 我已经添加了您要求的代码。 BSS 是一个 BluetoothServerSocket,抱歉我的变量名不好。非常感谢您的帮助,我会尽快回复您。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多