首页 > 解决方案 > 检查蓝牙 IsEnabled 使我的应用程序崩溃

问题描述

我正在创建一个 AndroidBluetoothService 来处理来自我的 Xamarin 应用程序的蓝牙交互。

在这个服务中,我使用了一个私有类变量:

    private BluetoothAdapter ServiceAdapter;

然后我在构造函数中初始化它:

        ServiceAdapter = BluetoothAdapter.DefaultAdapter;

然后,当我检查它是否已启用时:

            bool enabled = ServiceAdapter.IsEnabled;

我收到一个导致我的应用程序崩溃的异常:

System.NotSupportedException
  Message=Could not activate JNI Handle 0xbef405b8 (key_handle 0xdb031b8) of Java type 'crc6402525acf0275fedf/MainActivity' as managed type 'ArduinoSerial.MainActivity'.

其中 ArduinoSerial 是我的项目/应用程序的名称,而 MainActivity 是正在运行的活动。我已经对代码进行了注释和取消注释,以准确找出哪一行正在爆炸。不知道如何从这里开始,但我现在跳过这条线,只是假设它已启用。

标签: xamarin.androidbluetoothandroid-bluetooth

解决方案


看来问题是由于应用程序没有蓝牙权限引起的。我添加了

到 AndroidManifest.xml 并解决了问题。

为一个简单的问题创建一个钝错误的方法。:(希望这对其他人有所帮助。


推荐阅读