Java 切换蓝牙时的无限活动循环

Java 切换蓝牙时的无限活动循环,java,android,bluetooth,Java,Android,Bluetooth,我已经创建了一个应用程序来检查NFC标签的有效负载,当它与应用程序匹配时,就会切换蓝牙 不幸的是,该应用程序似乎进入了一个无限循环,它请求用户允许操作蓝牙,忽略选择并再次启动(再次询问相同的问题/活动)onActivityResult似乎未被调用 我的控制台日志调用的输出为: Payload: 'quicktags-togglebluetooth' Bluetooth should now be on 如果我继续在权限活动中点击“是”,则蓝牙会自动切换,控制台日志(logcat)如下所示: P

我已经创建了一个应用程序来检查NFC标签的有效负载,当它与应用程序匹配时,就会切换蓝牙

不幸的是,该应用程序似乎进入了一个无限循环,它请求用户允许操作蓝牙,忽略选择并再次启动(再次询问相同的问题/活动)
onActivityResult
似乎未被调用

我的控制台日志调用的输出为:

Payload: 'quicktags-togglebluetooth'
Bluetooth should now be on
如果我继续在权限活动中点击“是”,则蓝牙会自动切换,控制台日志(logcat)如下所示:

Payload: quicktags-togglebluetooth
Bluetooth should now be on
Bluetooth should now be off
Bluetooth should now be on
Bluetooth should now be off
Bluetooth should now be on
Bluetooth should now be off
等等

AndroidManifest列出了正确的权限,请参见以下内容:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.getquicktags.qt"
    android:versionCode="1"
    android:versionName="1.0" android:installLocation="auto">

    <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="14" />

    <uses-permission android:name="android.permission.NFC" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-feature android:name="android.hardware.nfc" android:required="true" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

         <activity
            android:name=".CardActivity"
            android:label="@string/app_name" >

            <!-- Handle a collectable card NDEF record -->
            <intent-filter>
                <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
                <data android:mimeType="application/vnd.getquicktags.qt"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
         </activity>

    </application>

</manifest>
您可以看到,根据logcat,
onActivityResults
,因此不会调用
closeApp

我正在测试Nexus7。标签很好,我已经测试过各种NFC阅读器

当扫描标签时,logcat会出现一些错误,但它们对我来说似乎没有多大意义。见下文:

01-07 00:18:41.595: E/bt-btif(5830): btif_enable_service: current services:0x100020
01-07 00:18:41.605: E/bt-btif(5830): btif_enable_service: current services:0x140020
01-07 00:18:41.605: E/bt-btif(5830): btif_enable_service: current services:0x140020
01-07 00:18:42.415: E/bt-btif(5830): Calling BTA_HhEnable
01-07 00:18:42.415: E/btif_config.c(5830): ## btif_config_get assert section && *section && key && *key && name && *name && bytes && type failed at line:186 ##
01-07 00:18:42.415: E/bt-btif(5830): btif_storage_get_adapter_property service_mask:0x140020
01-07 00:18:42.435: E/btif_config.c(5830): ## btif_config_get assert section && *section && key && *key && name && *name && bytes && type failed at line:186 ##
01-07 00:18:42.445: E/bt_h4(5830): vendor lib postload completed
01-07 00:18:42.545: E/BluetoothServiceJni(5830): SOCK FLAG = 1 ***********************
01-07 00:18:42.605: E/BluetoothServiceJni(5830): SOCK FLAG = 0 ***********************
01-07 00:18:42.715: E/BtOppRfcommListener(5830): Error accept connection java.io.IOException: read failed, socket might closed, read ret: -1
01-07 00:18:42.915: E/bt-btif(5830): BTA AG is already disabled, ignoring ...
01-07 00:18:42.935: E/bt-btif(5830): btif_disable_service: Current Services:0x140020
01-07 00:18:42.945: E/bt-btif(5830): btif_disable_service: Current Services:0x100020
01-07 00:18:42.945: E/bt-btif(5830): btif_disable_service: Current Services:0x100020

非常感谢你在这方面的帮助。正如您可能想象的那样,这让我非常恼火:)

为什么要在
closeApp()中终止进程?我想这是你问题的一部分。只需在活动中调用
finish()
,让Android在需要时清理您的进程


当你终止你的进程时,Android认为发生了不好的事情并试图恢复。作为此次恢复的一部分,它可能会重新交付您可能不想要的意图。

正如@NFC guy指出的,这比NFC更像是一个蓝牙问题,但问题仍然存在。您为什么要在
closeApp()中终止进程?我想这是你问题的一部分。只需在活动中调用
finish()
,让Android在需要时清理您的进程。当你终止你的进程时,Android认为发生了不好的事情并试图恢复。作为这次复苏的一部分,它可能是重新交付的意图,这可能不是你想要的。好吧,我来试试。我正在调用
closeApp()
,因为这是让应用看起来不可见的唯一方法。这个想法是让应用程序启动、切换蓝牙并快速关闭,用户唯一能注意到的是蓝牙现在已经打开/关闭。幸运吗?请让我知道,如果这是工作。仍然没有尝试它,将在一个小时左右。如果它有效,我会让你知道,这样你就可以把它写进一个答案:)
01-07 00:18:41.595: E/bt-btif(5830): btif_enable_service: current services:0x100020
01-07 00:18:41.605: E/bt-btif(5830): btif_enable_service: current services:0x140020
01-07 00:18:41.605: E/bt-btif(5830): btif_enable_service: current services:0x140020
01-07 00:18:42.415: E/bt-btif(5830): Calling BTA_HhEnable
01-07 00:18:42.415: E/btif_config.c(5830): ## btif_config_get assert section && *section && key && *key && name && *name && bytes && type failed at line:186 ##
01-07 00:18:42.415: E/bt-btif(5830): btif_storage_get_adapter_property service_mask:0x140020
01-07 00:18:42.435: E/btif_config.c(5830): ## btif_config_get assert section && *section && key && *key && name && *name && bytes && type failed at line:186 ##
01-07 00:18:42.445: E/bt_h4(5830): vendor lib postload completed
01-07 00:18:42.545: E/BluetoothServiceJni(5830): SOCK FLAG = 1 ***********************
01-07 00:18:42.605: E/BluetoothServiceJni(5830): SOCK FLAG = 0 ***********************
01-07 00:18:42.715: E/BtOppRfcommListener(5830): Error accept connection java.io.IOException: read failed, socket might closed, read ret: -1
01-07 00:18:42.915: E/bt-btif(5830): BTA AG is already disabled, ignoring ...
01-07 00:18:42.935: E/bt-btif(5830): btif_disable_service: Current Services:0x140020
01-07 00:18:42.945: E/bt-btif(5830): btif_disable_service: Current Services:0x100020
01-07 00:18:42.945: E/bt-btif(5830): btif_disable_service: Current Services:0x100020