Note that there are some explanatory texts on larger screens.

plurals
  1. POInfinite Activity Loop when toggling Bluetooth
    text
    copied!<p>I have created an app that checks a payload of an NFC tag, and when it matches the app toggles Bluetooth.</p> <p>Unfortunately the app seems to be entering into an infinite loop, where it asks the user for permission to manipulate Bluetooth, ignores the choice and launches again (asking the same question/Activity again). <code>onActivityResult</code> seems to not be getting called.</p> <p>Output from my console log calls is:</p> <pre><code>Payload: 'quicktags-togglebluetooth' Bluetooth should now be on </code></pre> <p>If I continue to hit 'Yes' on the permission Activity then Bluetooth toggles indefnitely, and the console log (logcat) looks like:</p> <pre><code>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 </code></pre> <p>and so on.</p> <p>AndroidManifest lists the correct permissions, please see below:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.getquicktags.qt" android:versionCode="1" android:versionName="1.0" android:installLocation="auto"&gt; &lt;uses-sdk android:minSdkVersion="14" android:targetSdkVersion="14" /&gt; &lt;uses-permission android:name="android.permission.NFC" /&gt; &lt;uses-permission android:name="android.permission.BLUETOOTH" /&gt; &lt;uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /&gt; &lt;uses-permission android:name="android.permission.INTERNET" /&gt; &lt;uses-feature android:name="android.hardware.nfc" android:required="true" /&gt; &lt;application android:icon="@drawable/ic_launcher" android:label="@string/app_name" &gt; &lt;activity android:name=".MainActivity" android:label="@string/app_name" &gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;activity android:name=".CardActivity" android:label="@string/app_name" &gt; &lt;!-- Handle a collectable card NDEF record --&gt; &lt;intent-filter&gt; &lt;action android:name="android.nfc.action.NDEF_DISCOVERED"/&gt; &lt;data android:mimeType="application/vnd.getquicktags.qt"/&gt; &lt;category android:name="android.intent.category.DEFAULT"/&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;/application&gt; &lt;/manifest&gt; </code></pre> <p>The CardActivity.java file, which is what launches this Bluetooth chaos can be found below:</p> <pre><code>package com.getquicktags.qt; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.Intent; import android.nfc.NdefMessage; import android.nfc.NdefRecord; import android.nfc.NfcAdapter; import android.os.Bundle; import android.os.Parcelable; import android.util.Log; import android.bluetooth.*; public class CardActivity extends Activity implements OnClickListener { private static final String TAG = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.card_activity); // see if app was started from a tag and show game console Intent intent = getIntent(); if(intent.getType() != null &amp;&amp; intent.getType().equals(MimeType.NFC_DEMO)) { Parcelable[] rawMsgs = getIntent().getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); NdefMessage msg = (NdefMessage) rawMsgs[0]; NdefRecord cardRecord = msg.getRecords()[0]; String payload = new String(cardRecord.getPayload()); Log.d(TAG, "Payload: '"+ payload +"'"); if(payload.equals("quicktags-togglebluetooth")) { toggleBluetooth(); } } } private void toggleBluetooth() { BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (mBluetoothAdapter == null) { // Device does not support Bluetooth Log.d(TAG, "No Bluetooth on device"); closeApp(); } if (!mBluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, 1); Log.d(TAG, "Bluetooth should now be on"); } else { // Turn it off mBluetoothAdapter.disable(); Log.d(TAG, "Bluetooth should now be off"); closeApp(); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // Close the app Log.d(TAG, "Close the app call"); closeApp(); } private void closeApp() { Log.d(TAG, "And... close it. This is inside closeApp()"); android.os.Process.killProcess(android.os.Process.myPid()); } public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } } </code></pre> <p>You can see that, according to logcat, <code>onActivityResults</code> and thus <code>closeApp</code> do not get called.</p> <p>I am testing on a Nexus 7. Tag is fine, I've tested with various NFC readers.</p> <p>There are some errors from logcat when the tag is scanned, but they don't seem to make much sense to me. See below:</p> <pre><code>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 &amp;&amp; *section &amp;&amp; key &amp;&amp; *key &amp;&amp; name &amp;&amp; *name &amp;&amp; bytes &amp;&amp; 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 &amp;&amp; *section &amp;&amp; key &amp;&amp; *key &amp;&amp; name &amp;&amp; *name &amp;&amp; bytes &amp;&amp; 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 </code></pre> <p>Massive thanks for any help on this. As you can probably imagine, it is driving me mad :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload