首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BLE扫描的SCAN_FAILED_APPLICATION_REGISTRATION_FAILED解决方案?

BLE扫描的SCAN_FAILED_APPLICATION_REGISTRATION_FAILED解决方案?
EN

Stack Overflow用户
提问于 2014-12-17 00:23:06
回答 4查看 8.4K关注 0票数 19

我的Android应用程序扫描BLE设备,从某一点开始,错误代码2 (ScanCallback.SCAN_FAILED_APPLICATION_REGISTRATION_FAILED).开始失败。我用的是Nexus 9,5.0.1棒棒糖。

即使在我重新启动应用程序之后,这个问题仍在继续,当我从Settings重新启动蓝牙服务时,我最终可以解决这个问题。但是这个问题是反复出现的,我认为我的编码方式是错误的;BLE相关的API是新的,而且很少有信息。

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

EN

回答 4

Stack Overflow用户

发布于 2016-06-08 12:52:16

当你得到错误时

代码语言:javascript
复制
SCAN_FAILED_APPLICATION_REGISTRATION_FAILED

您应该禁用BluetoothAdapter

代码语言:javascript
复制
BluetoothAdapter.getDefaultAdapter().disable();

禁用BluetoothAdapter,则会触发事件STATE_TURNING_OFF。一旦触发此事件,请尝试重新连接到BluetoothAdapter:

代码语言:javascript
复制
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;
票数 6
EN

Stack Overflow用户

发布于 2018-07-22 16:47:15

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

代码语言:javascript
复制
<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"/>

除主要活动外:

代码语言:javascript
复制
// 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);
}
票数 6
EN

Stack Overflow用户

发布于 2018-01-04 14:24:39

您应该只执行BT适配器的初始化操作。要确保它已经就绪,请创建意图筛选器:

代码语言:javascript
复制
val filter = IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)

和广播接收器(,只有在适配器就绪时才执行操作):

代码语言:javascript
复制
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 -> {}
                    }
                }
            }
        }

然后登记接收者:

代码语言:javascript
复制
registerReceiver(broadcastReceiver, filter)

并重新启动适配器(--此部分可以用check替换):

代码语言:javascript
复制
bluetoothAdapter.disable()
bluetoothAdapter.enable()

完成了!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27516399

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档