【问题标题】:Solution for BLE scan's SCAN_FAILED_APPLICATION_REGISTRATION_FAILED?BLE 扫描的 SCAN_FAILED_APPLICATION_REGISTRATION_FAILED 解决方案?
【发布时间】:2019-07-22 23:48:27
【问题描述】:

我的 Android 应用程序扫描 BLE 设备,并且从某个点开始失败并出现错误代码 2 (ScanCallback.SCAN_FAILED_APPLICATION_REGISTRATION_FAILED)。我正在使用 Nexus 9、5.0.1 Lollipop。

这个问题在我重新启动应用程序后仍然存在,当我从设置中重新启动蓝牙服务时,我终于可以解决这个问题了。但是这个问题反复出现,我认为我的编码方式错误; BLE相关的API是新的,信息很少。

有没有人知道这个错误的一般解决方案,最好不需要重新启动蓝牙服务?尽管此错误代码记录在 Android API 参考中,但我不知道如何正确处理。

【问题讨论】:

  • 不,我的代码也有类似的问题。到目前为止没有运气
  • 从设置手动重启蓝牙服务也为我解决了这个问题。

标签: bluetooth-lowenergy android-5.0-lollipop android-bluetooth


【解决方案1】:

当你收到错误时

SCAN_FAILED_APPLICATION_REGISTRATION_FAILED

你应该禁用蓝牙适配器

BluetoothAdapter.getDefaultAdapter().disable();

禁用 BluetoothAdapter,触发事件 STATE_TURNING_OFF。触发此事件后,尝试重新连接到 BluetoothAdapter:

case BluetoothAdapter.STATE_OFF:
  Log.d(TAG, "bluetooth adapter turned off");
  handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        Log.d(TAG, "bluetooth adapter try to enable");
        BluetoothAdapter.getDefaultAdapter().enable();
    }}, 500);
  break;

【讨论】:

    【解决方案2】:

    事实证明,蓝牙 LE 需要 AndroidManifest.xml 中的以下 Android 应用程序权限:

    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    
    <!--BLE scanning is commonly used to determine a user's location with Bluetooth LE beacons. -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    
    <!-- if your app targets API level 21 or higher. -->
    <uses-feature android:name="android.hardware.location.gps" />
    
    <!--app is available to BLE-capable devices only. -->
    <uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
    

    除了主要活动:

    // onResume()
    if (ContextCompat.checkSelfPermission(this.getApplicationContext(),
            android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
    } else {
        ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
                REQUEST_LOCATION_ENABLE_CODE);
    }
    

    【讨论】:

    • ACCESS_COARSE_LOCATION 是通过ACCESS_FINE_LOCATION 授予的。无需添加。
    【解决方案3】:

    您应该执行只有成功初始化 BT 适配器的操作。 确保它已准备好创建意图过滤器:

    val filter = IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)
    

    和广播接收器(只有在适配器准备好时才会执行操作):

    val broadcastReceiver = object: BroadcastReceiver() {
                override fun onReceive(context: Context, intent: Intent?) {
                    val action = intent?.action
                    if (action != null && action == BluetoothAdapter.ACTION_STATE_CHANGED) {
                        val state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)
                        when (state) {
                            BluetoothAdapter.STATE_ON -> {
                                if (bluetoothAdapter.isEnabled) {
                                   //perform your task here
                                }
                            }
                            BluetoothAdapter.STATE_OFF -> {}
                        }
                    }
                }
            }
    

    然后注册接收者:

    registerReceiver(broadcastReceiver, filter)
    

    并重新启动适配器(这部分可以用检查替换):

    bluetoothAdapter.disable()
    bluetoothAdapter.enable()
    

    完成!

    【讨论】:

      【解决方案4】:

      我今天遇到了这种情况。虽然在 Android 设置中手动禁用然后启用 BT 并不能解决此问题,但我只能在手动禁用它然后让受问题影响的应用启用 BT 后让它工作。

      然后应用程序会弹出一条 Android 系统消息“一个应用程序正在请求打开 BT 的权限”(我有一个德语 UI,所以它的措辞可能不同),然后当我按下允许时,应用程序终于有正确的访问BT,此错误不再显示。

      听起来像一个错误或什么的。

      【讨论】:

        猜你喜欢
        • 2018-07-30
        • 1970-01-01
        • 1970-01-01
        • 2010-12-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多