2013-01-07 9 views
7

我已經創建了一個檢查NFC標籤的有效載荷的應用程序,當應用程序匹配切換藍牙。無限循環的活動時切換藍牙

不幸的是,應用程序似乎要進入一個無限循環,它要求用戶許可操縱藍牙,忽略了選擇,並再次啓動(再次問同樣的問題/活動)。 onActivityResult似乎沒有被調用。從我的控制檯日誌調用

輸出是:

Payload: 'quicktags-togglebluetooth' 
Bluetooth should now be on 

如果我繼續打「是」的許可活動,則藍牙切換indefnitely和控制檯日誌(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> 

的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更多,但問題仍然存在 – Mike

+0

爲什麼你要在' closeApp()'?我想這是你問題的一部分。只需在activity上調用finish(),讓Android在需要時清理進程。當你殺死你的進程時,Android認爲發生了一些不好的事情並嘗試恢復。作爲這種恢復的一部分,它可能是重新傳遞意向,這可能不是你想要的。 –

+0

好吧,我會放棄這一點。我打電話給'closeApp()',因爲這是讓應用看起來不可見的唯一方法。這個想法是應用程序啓動,切換藍牙並關閉得如此之快,以至於用戶唯一可以注意到的就是藍牙現在處於開啓/關閉狀態。 – Mike

回答

1

你爲什麼要殺死進程中closeApp()?我想這是你問題的一部分。只需致電finish()即可,並讓Android在需要時清理您的流程。

當你殺死你的進程,Android的認爲不好的事情發生了,並試圖恢復。作爲這種恢復的一部分,它可能是重新傳遞意向,這可能不是你想要的