Problem with NfcAdapter's enableForegroundDispatch call in android q (android 10)

huangapple 未分类评论44阅读模式
英文:

Problem with NfcAdapter's enableForegroundDispatch call in android q (android 10)

问题

I have some issues developing the nfc plugin in Unity.

When nfc tagging, onNewIntent is not called, the app moves to the background and the default nfc tagviewer opens.

It worked on Android 9 and below, but not android 10.

I found a suspicious logs using Logcat.

NfcService: setForegroundDispatch: Caller not in foreground.

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}

I have found several places to print the log. As a result, I found the part that outputs the same log in Android Code Search.

Link to code in Android Code Search

@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);
}

According to the code, if a log occurs, processing is stopped without returning any results. So it seems that the foregroundDispatch does not proceed and goes over.

I've been trying for a long time to solve this issue, but couldn't find the same case, so I think it's a new issue in Android 10.

Does anyone know how to fix this?

Lastly, I'm sorry that it was difficult to understand because I used a translation site to write this question.

Below is the code for the plugin.

@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.enable

<details>
<summary>英文:</summary>

I have some issues developing the nfc plugin in Unity. 

When nfc tagging, onNewIntent is not called, the app moves to the background and the default nfc tagviewer opens.

It worked on Android 9 and below, but not android 10.

I found a suspicious logs using Logcat.




**NfcService: setForegroundDispatch: Caller not in foreground.**

    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}


I have found several places to print the log. As a result, I found the part that outputs the same log in 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&amp;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, &quot;setForegroundDispatch: Caller not in foreground.&quot;);
                    return;
                }
                // Short-cut the disable path
                if (intent == null &amp;&amp; filters == null &amp;&amp; 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(&quot;null IntentFilter&quot;);
                            }
                        }
                    }
                }
    
                // Validate the tech lists
                String[][] techLists = null;
                if (techListsParcel != null) {
                    techLists = techListsParcel.getTechLists();
                }
    
                mNfcDispatcher.setForegroundDispatch(intent, filters, techLists);
            }


According to the code, if a log occurs, processing is stopped without returning any results. So it seems that the foregroundDispatch does not proceed and goes over.


I&#39;ve been trying for a long time to solve this issue, but couldn&#39;t find the same case, so I think it&#39;s a new issue in android 10.

Does anyone know how to fix this?

Lastly, I&#39;m sorry that it was difficult to understand because I used a translation site to write this question.

Below is the code for the plugin.

    @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(&quot;*/*&quot;);
            }
            catch (MalformedMimeTypeException e) {
                throw new RuntimeException(&quot;Filter fail:&quot;, e);
            }
            
            IntentFilter _ndfFilter_NDEF =  new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    
            try {
            	_ndfFilter_NDEF.addDataType(&quot;*/*&quot;);
            }
            catch (MalformedMimeTypeException e) {
                throw new RuntimeException(&quot;Filter fail:&quot;, e);
            }        
            
            IntentFilter _ndfFilter_Tech =  new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
            try {
            	_ndfFilter_Tech.addDataType(&quot;*/*&quot;);
            }
            catch (MalformedMimeTypeException e) {
                throw new RuntimeException(&quot;Filter fail:&quot;, 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(), &quot;onResume Activity&quot;);   
      	  mNfcAdapter.enableForegroundDispatch(this, pendingIntent, mIntentFilter, techListsArray);  
        }
        
        
        @Override
        public void onPause() {
            super.onPause();
            Log.e(NFCPlugin.class.toString(), &quot;onPause Activity&quot;);       
            mNfcAdapter.disableForegroundDispatch(this);        
        }

Next is the manifest content.

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
    &lt;manifest
        xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
        package=&quot;com.package.product&quot;
        xmlns:tools=&quot;http://schemas.android.com/tools&quot;
        android:installLocation=&quot;preferExternal&quot;&gt;
      &lt;uses-permission android:name=&quot;android.permission.NFC&quot; &gt;
      &lt;/uses-permission&gt;
      
      &lt;uses-feature
        android:name=&quot;android.hardware.nfc&quot;
        android:required=&quot;true&quot; &gt;
      &lt;/uses-feature&gt;
      
      &lt;supports-screens
          android:smallScreens=&quot;true&quot;
          android:normalScreens=&quot;true&quot;
          android:largeScreens=&quot;true&quot;
          android:xlargeScreens=&quot;true&quot;
          android:anyDensity=&quot;true&quot;/&gt;
    
      &lt;application
          android:theme=&quot;@style/UnityThemeSelector&quot;
          android:icon=&quot;@mipmap/app_icon&quot;
          android:label=&quot;@string/app_name&quot;
           android:networkSecurityConfig=&quot;@xml/network_security_config&quot;
          &gt;
        &lt;activity android:name=&quot;.NFCPlugin&quot;
                        android:launchMode=&quot;singleTask&quot;
                android:configChanges=&quot;keyboard|keyboardHidden|screenLayout|screenSize|orientation&quot;
                        android:screenOrientation=&quot;landscape&quot;
                        android:label=&quot;@string/app_name&quot; &gt;
          &lt;intent-filter&gt;
            &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
            &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
          &lt;/intent-filter&gt;
    
          &lt;intent-filter&gt;
            &lt;action android:name=&quot;android.nfc.action.TAG_DISCOVERED&quot; /&gt;
          &lt;/intent-filter&gt;
          &lt;intent-filter&gt;
            &lt;action android:name=&quot;android.nfc.action.NDEF_DISCOVERED&quot; /&gt;
            &lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
          &lt;/intent-filter&gt;
          
          &lt;intent-filter&gt;
            &lt;action android:name=&quot;android.nfc.action.TECH_DISCOVERED&quot;/&gt;
          &lt;/intent-filter&gt;
          &lt;meta-data android:name=&quot;android.nfc.action.TECH_DISCOVERED&quot;
              android:resource=&quot;@xml/tech_list&quot; /&gt;
          
          &lt;meta-data android:name=&quot;unityplayer.UnityActivity&quot; android:value=&quot;true&quot; /&gt;
        &lt;/activity&gt;
        
        
      &lt;/application&gt;
    &lt;/manifest&gt;

lastly, it is the contents of techList required for TECH_DISCOVERED.

    &lt;resources xmlns:xliff=&quot;urn:oasis:names:tc:xliff:document:1.2&quot;&gt;
      &lt;tech-list&gt;
        &lt;tech&gt;android.nfc.tech.IsoDep&lt;/tech&gt;
        &lt;tech&gt;android.nfc.tech.NfcA&lt;/tech&gt;
        &lt;tech&gt;android.nfc.tech.NfcB&lt;/tech&gt;
        &lt;tech&gt;android.nfc.tech.NfcF&lt;/tech&gt;
        &lt;tech&gt;android.nfc.tech.NfcV&lt;/tech&gt;
        &lt;tech&gt;android.nfc.tech.Ndef&lt;/tech&gt;
        &lt;tech&gt;android.nfc.tech.NdefFormatable&lt;/tech&gt;
        &lt;tech&gt;android.nfc.tech.MifareClassic&lt;/tech&gt;
        &lt;tech&gt;android.nfc.tech.MifareUltralight&lt;/tech&gt;
      &lt;/tech-list&gt;
    &lt;/resources&gt;



</details>


huangapple
  • 本文由 发表于 2020年4月5日 15:23:14
  • 转载请务必保留本文链接:https://java.coder-hub.com/61039315.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定