2013-01-07 17 views
7

NFCタグのペイロードをチェックするアプリを作成しました。アプリが一致するとBluetoothをトグルします。Bluetoothを切り替えるときの無限アクティビティループ

残念なことに、アプリは無限ループに入り、Bluetoothを操作する権限をユーザーに求め、選択を無視して再び同じ質問/アクティビティを要求します。 onActivityResultが呼び出されていないようです。私はその後、Bluetoothはindefnitely切り替わり許可活動に「はい」をヒットし続けた場合

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> 

このBluetoothの混乱を起動するものであるCardActivity.javaファイルは、以下見つけることができます:

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 && 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 

    } 
} 

あなたがいることを見ることができます、logcatによると、onActivityResults、したがってcloseAppは呼び出されません。

Nexus 7でテストしています。タグは問題なく、さまざまな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 

大変ありがとうございます。あなたはおそらく想像することができますが、それは私を狂って運転しています:)

+0

@ NFCの人はNFCよりもBluetoothの問題が多いと指摘していますが、問題は依然として続きます – Mike

+0

なぜプロセスを ' closeApp() '?私はこれがあなたの問題の一部だと思います。アクティビティで 'finish()'を呼び出すだけで、Androidが望むときにプロセスをクリーンアップすることができます。あなたのプロセスを殺すと、Androidは何か悪いことを考え、回復しようとします。この回復の一環として、おそらくあなたが望むものではない意図を再配信するかもしれません。 –

+0

さて、私はそれを行ってあげるよ。私はcloseApp()を呼び出しています。アプリを見えないようにする唯一の方法だったからです。このアイデアは、アプリが起動し、ブルートゥースを切り替えてすぐに閉じることができるため、ユーザーが目立つのはBluetoothがオン/オフになったことだけです。 – Mike

答えて

1

なぜあなたはcloseApp()でプロセスを殺していますか?私はこれがあなたの問題の一部だと思います。アクティビティにfinish()と電話するだけで、必要に応じてAndroidのプロセスをクリーンアップすることができます。

あなたのプロセスを強制終了すると、Androidは何か悪いことが起こったと考えて、回復しようとします。この復旧の一環として、おそらくあなたが望んでいない意図を再配信するかもしれない

関連する問題