Проблема с вызовом enableForegroundDispatch NfcAdapter в android q (android 10) - PullRequest
1 голос
/ 05 апреля 2020

У меня есть некоторые проблемы при разработке плагина nf c в Unity.

Когда nf c tagging не вызывается, onNewIntent не вызывается, приложение перемещается в фоновый режим и открывается стандартный просмотрщик тегов nf c.

Работает на Android 9 и ниже, но не android 10.

Я обнаружил подозрительные журналы, используя Logcat.

NfcService: setForegroundDispatch: вызывающая сторона не на переднем плане.

2020-04-05 15:33:45.857 32411-32411/? E/class com.package.product.NFCPlugin: call Activity

2020-04-05 15:33:45.857 32411-32411/? E/class com.package.product.NFCPlugin: intent:Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp= com.package.product/.NFCPlugin bnds=[1131,670][1405,1123] (has extras) }

2020-04-05 15:33:45.857 32411-32411/? E/class com.package.product.NFCPlugin: Skill Launcher Cat: LAUNCHER

2020-04-05 15:33:45.858 32411-32411/? E/class com.package.product.NFCPlugin: onResume Activity

2020-04-05 15:33:45.858 32411-32411/? E/class com.package.product.NFCPlugin: Call Check ForegroundDispatch

2020-04-05 15:33:45.858 7904-7904/? I/[LGHome6]Launcher: rebindModel: rebind = false, flag = 0, currentPage = 4, isLandscape = true, mChangedProfileByMultiWindow = false, mOrientationOfCurrentLayout = 0, mWorkspaceLoading = false, mChangedProfile = true, mIsMirrorMode = false

2020-04-05 15:33:45.858 7506-7542/? E/NfcService: setForegroundDispatch: Caller not in foreground.

2020-04-05 15:33:45.859 32411-32735/? W/System.err: javax.net.ssl.SSLException: Write error: ssl=0x7c105b0c48: I/O error during system call, Broken pipe

2020-04-05 15:33:45.859 32411-32411/? E/class com.package.product.NFCPlugin: Equal Result:true

2020-04-05 15:33:45.860 2437-2462/? V/DesktopModeManager: Notification is not exist.

2020-04-05 15:33:45.860 5005-5005/? I/OpaLayout: Setting opa enabled to true

2020-04-05 15:33:45.863 2437-8224/? D/InputDispatcher: Window went away: Window{f88f9f9 u0 com.lge.launcher3/com.lge.launcher3.LauncherExtension}

Я нашел несколько мест для печати журнала. В результате я нашел часть, которая выводит тот же журнал в Android Code Search.

https://cs.android.com/android/platform/superproject/+/master: packages / apps / Nfc / src / com / android / nfc / NfcService.java ; l = 1053; bpv = 1; bpt = 1? q = NfcService:% 20setForegroundDispatch% 20enable & ss = android% 2Fplatform% 2Fsuperproject

  @Override
        public void setForegroundDispatch(PendingIntent intent,
                IntentFilter[] filters, TechListParcel techListsParcel) 
        {
            NfcPermissions.enforceUserPermissions(mContext);
            if (!mForegroundUtils.isInForeground(Binder.getCallingUid())) 
            {
                Log.e(TAG, "setForegroundDispatch: Caller not in foreground.");
                return;
            }
            // Short-cut the disable path
            if (intent == null && filters == null && techListsParcel == null) 
            {
                mNfcDispatcher.setForegroundDispatch(null, null, null);
                return;
            }

            // Validate the IntentFilters
            if (filters != null) {
                if (filters.length == 0) {
                    filters = null;
                } else {
                    for (IntentFilter filter : filters) {
                        if (filter == null) {
                            throw new IllegalArgumentException("null IntentFilter");
                        }
                    }
                }
            }

            // Validate the tech lists
            String[][] techLists = null;
            if (techListsParcel != null) {
                techLists = techListsParcel.getTechLists();
            }

            mNfcDispatcher.setForegroundDispatch(intent, filters, techLists);
        }

В соответствии с кодом, если регистрируется журнал, обрабатывается остановлено без возврата каких-либо результатов. Таким образом, кажется, что foregroundDispatch не выполняется и переходит.

Я долго пытался решить эту проблему, но не смог найти тот же случай, поэтому я думаю, что это новая проблема в android 10.

Кто-нибудь знает, как это исправить?

Наконец, мне жаль, что это было трудно понять, потому что я использовал сайт перевода, чтобы написать этот вопрос.

Ниже приведен код для плагина.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        m_instance=this;


        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        if (mNfcAdapter == null) 
        {

            finish();            
            return;
        }


        pendingIntent = PendingIntent.getActivity(NFCPlugin.this, 0, new Intent(NFCPlugin.this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);



        IntentFilter _ndfFilter =  new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
        try 
        {      
            _ndfFilter.addDataType("*/*");
        }
        catch (MalformedMimeTypeException e) {
            throw new RuntimeException("Filter fail:", e);
        }

        IntentFilter _ndfFilter_NDEF =  new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);

        try {
            _ndfFilter_NDEF.addDataType("*/*");
        }
        catch (MalformedMimeTypeException e) {
            throw new RuntimeException("Filter fail:", e);
        }        

        IntentFilter _ndfFilter_Tech =  new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
        try {
            _ndfFilter_Tech.addDataType("*/*");
        }
        catch (MalformedMimeTypeException e) {
            throw new RuntimeException("Filter fail:", e);
        }
        mIntentFilter = new IntentFilter[] { _ndfFilter ,_ndfFilter_NDEF,_ndfFilter_Tech}; 

        techListsArray = new String[][] {
            new String[] {NFCPlugin.class.getName()},
            new String[] {TagTechnology.class.getName()},
            new String[] {NfcA.class.getName()},
            new String[] {NfcB.class.getName()},
            new String[] {NfcV.class.getName()},
            new String[] {IsoDep.class.getName()},
            new String[] {Ndef.class.getName()},
            new String[] {NdefFormatable.class.getName()}
            };           
}


@Override
    public void onResume() {
        super.onResume();     
        Log.e(NFCPlugin.class.toString(), "onResume Activity");   
      mNfcAdapter.enableForegroundDispatch(this, pendingIntent, mIntentFilter, techListsArray);  
    }


    @Override
    public void onPause() {
        super.onPause();
        Log.e(NFCPlugin.class.toString(), "onPause Activity");       
        mNfcAdapter.disableForegroundDispatch(this);        
    }

Следующим является содержимое манифеста.

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.package.product"
    xmlns:tools="http://schemas.android.com/tools"
    android:installLocation="preferExternal">
  <uses-permission android:name="android.permission.NFC" >
  </uses-permission>

  <uses-feature
    android:name="android.hardware.nfc"
    android:required="true" >
  </uses-feature>

  <supports-screens
      android:smallScreens="true"
      android:normalScreens="true"
      android:largeScreens="true"
      android:xlargeScreens="true"
      android:anyDensity="true"/>

  <application
      android:theme="@style/UnityThemeSelector"
      android:icon="@mipmap/app_icon"
      android:label="@string/app_name"
       android:networkSecurityConfig="@xml/network_security_config"
      >
    <activity android:name=".NFCPlugin"
                    android:launchMode="singleTask"
            android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
                    android:screenOrientation="landscape"
                    android:label="@string/app_name" >
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>

      <intent-filter>
        <action android:name="android.nfc.action.TAG_DISCOVERED" />
      </intent-filter>
      <intent-filter>
        <action android:name="android.nfc.action.NDEF_DISCOVERED" />
        <category android:name="android.intent.category.DEFAULT" />
      </intent-filter>

      <intent-filter>
        <action android:name="android.nfc.action.TECH_DISCOVERED"/>
      </intent-filter>
      <meta-data android:name="android.nfc.action.TECH_DISCOVERED"
          android:resource="@xml/tech_list" />

      <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
    </activity>


  </application>
</manifest>

наконец, это содержимое techList, необходимое для TECH_DISCOVERED.

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
  <tech-list>
    <tech>android.nfc.tech.IsoDep</tech>
    <tech>android.nfc.tech.NfcA</tech>
    <tech>android.nfc.tech.NfcB</tech>
    <tech>android.nfc.tech.NfcF</tech>
    <tech>android.nfc.tech.NfcV</tech>
    <tech>android.nfc.tech.Ndef</tech>
    <tech>android.nfc.tech.NdefFormatable</tech>
    <tech>android.nfc.tech.MifareClassic</tech>
    <tech>android.nfc.tech.MifareUltralight</tech>
  </tech-list>
</resources>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...