我的Android应用程序扫描BLE设备,从某一点开始,错误代码2 (ScanCallback.SCAN_FAILED_APPLICATION_REGISTRATION_FAILED).开始失败。我用的是Nexus 9,5.0.1棒棒糖。
即使在我重新启动应用程序之后,这个问题仍在继续,当我从Settings重新启动蓝牙服务时,我最终可以解决这个问题。但是这个问题是反复出现的,我认为我的编码方式是错误的;BLE相关的API是新的,而且很少有信息。
是否有人知道此错误的一般解决方案,最好不需要重新启动蓝牙服务?尽管这个错误代码是在Android引用中记录的,但我不知道如何正确处理它。
发布于 2016-06-08 12:52:16
当你得到错误时
SCAN_FAILED_APPLICATION_REGISTRATION_FAILED您应该禁用BluetoothAdapter
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;发布于 2018-07-22 16:47:15
事实证明,蓝牙LE需要AndroidManifest.xml中的以下安卓应用程序权限:
<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);
}发布于 2018-01-04 14:24:39
您应该只执行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)并重新启动适配器(--此部分可以用check替换):
bluetoothAdapter.disable()
bluetoothAdapter.enable()完成了!
https://stackoverflow.com/questions/27516399
复制相似问题