मैंने एक ऐप बनाया है जो एक एनएफसी टैग का पेलोड चेक करता है, और जब यह ऐप से मेल खाता है तो ब्लूटूथ टॉगल करता है।ब्लूटूथ टॉगल करते समय असीमित गतिविधि लूप
दुर्भाग्यवश ऐप एक अनंत लूप में प्रवेश कर रहा है, जहां यह उपयोगकर्ता को ब्लूटूथ में हेरफेर करने की अनुमति के लिए कहता है, पसंद को अनदेखा करता है और फिर से लॉन्च करता है (उसी प्रश्न/गतिविधि को फिर से पूछता है)। 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
कॉल नहीं किया जाता है।
मैं नेक्सस 7 पर परीक्षण कर रहा हूं। टैग ठीक है, मैंने विभिन्न एनएफसी पाठकों के साथ परीक्षण किया है।
टैग स्कैन होने पर लॉगकैट से कुछ त्रुटियां हैं, लेकिन वे मुझे अधिक समझ में नहीं आती हैं। नीचे देखें:
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
इस पर किसी भी मदद के लिए भारी धन्यवाद। जैसा कि आप शायद कल्पना कर सकते हैं, यह मुझे पागल कर रहा है :)
जैसा कि @ एनएफसी लड़के ने बताया कि यह एनएफसी की तुलना में ब्लूटूथ मुद्दा है, लेकिन समस्या अभी भी – Mike
क्यों है, closeApp() '? मुझे कल्पना है कि यह आपकी समस्या का हिस्सा है। बस गतिविधि पर 'फिनिश()' पर कॉल करें और एंड्रॉइड को अपनी प्रक्रिया को साफ करने के साथ निपटने दें। जब आप अपनी प्रक्रिया को मार देते हैं, तो एंड्रॉइड सोचता है कि कुछ बुरा हुआ और पुनर्प्राप्त करने का प्रयास करता है। इस वसूली के हिस्से के रूप में यह इरादों को फिर से वितरित कर सकता है जो शायद आप नहीं चाहते हैं। –
ठीक है, मैं इसे जाने दूंगा। मैं 'closeApp()' को कॉल कर रहा हूं क्योंकि ऐप को अदृश्य लगने का एकमात्र तरीका था। यह विचार ऐप लॉन्च करने, ब्लूटूथ टॉगल करने और इतनी जल्दी बंद करने के लिए था कि उपयोगकर्ता द्वारा ध्यान देने योग्य एकमात्र चीज यह है कि ब्लूटूथ अब चालू/बंद था। – Mike