Android Question Permission request in a webview

kohlenach

Member
Hi,
I open a website in a webview with a Webchromeclient. See code below.
On the website I can read and write NFC tags.
If I run the website in the normal browser I am asked
the first time for the permission read/writing NFC tags.
In the webview I get the error : Not allowed Error.... NFC permission request denied.

What can I do ?

rgs
Jürgen





B4X:
    webview1.initialize("wv")
    webExtras.Initialize(webview1)
    'webExtras.clearCache(True)
    

    JavascriptInterface.Initialize
    WebChromeClient.Initialize("WebChromeClient1")
    
    webExtras.SetWebChromeClient(WebChromeClient)
    webExtras.addJavascriptInterface(JavascriptInterface, "B4X")
    webExtras.JavaScriptEnabled = True
    
    
    
    webview1.ZoomEnabled = False
    
    webExtras.clearCache(True)
    webview1.loadurl(url)
 

drgottjr

Expert
Licensed User
Longtime User
What can I do ?

not use webview for this.
the full error message you should have received says:
JavaScript:
{console.log("Argh! Cannot read data from the NFC tag. Try another one?");controller.abort();reject();});ndef.addEventListener("reading",(data)=>{console.log(`> Serial Number: ${data.serialNumber}`);console.log(`> Records: (${data.message.records.length})`);controller.abort();resolve(data);});}catch(error){console.log("Argh! "+error);reject('Your browser does not support the web NFC APIs. This tool requires Chrome 89+ on Android.');}});}

Your browser does not support the web NFC APIs. This tool requires Chrome 89+ on Android.

you are probably better off just using the nfc example available here on the forum and reading the tags yourself. plus you don't have to be online to use it.
the site you went to only reads NDEF tags, so @Erel's example is good-to-go right out of the box.
 
Last edited:
Upvote 0

kohlenach

Member
not use webview for this.
the full error message you should have received says:
JavaScript:
{console.log("Argh! Cannot read data from the NFC tag. Try another one?");controller.abort();reject();});ndef.addEventListener("reading",(data)=>{console.log(`> Serial Number: ${data.serialNumber}`);console.log(`> Records: (${data.message.records.length})`);controller.abort();resolve(data);});}catch(error){console.log("Argh! "+error);reject('Your browser does not support the web NFC APIs. This tool requires Chrome 89+ on Android.');}});}

Your browser does not support the web NFC APIs. This tool requires Chrome 89+ on Android.

you are probably better off just using the nfc example available here on the forum and reading the tags yourself. plus you don't have to be online to use it.
the site you went to only reads NDEF tags, so @Erel's example is good-to-go right out of the box.

Its a website which I want to put in the playstore. So I use b4a and a webview.
I use the website also on a desktop computer.
I can open the website in "mobile design" in the android browser on the phone. All works fine.
But in a WEBVIEW of b4x , only the permission for NFC dont work.

I am using NDEFReader javascript API on my website.
First I check if the device is able for NFC. Here I get an OK from the NDEFReader API.
If I try to scan : Argh! NotAllowedError: Failed to execute 'scan' on 'NDEFReader' : NFC permission request denied.
The error is similar to yours.

So what you are saying is, if you open a webview with a webchromeclient, it dont has the same features like the installed chrome browser on android?
So what means webchromeclient ? What version supports what ?

rgs
J.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
webchromeclient is a kind of extension for webview. it handles tasks that webview is not designed to do.
one of these tasks has to do with asking the user for runtime permission: camera, location, etc. a dialog
pops up on the browser screen asking for permission to use these features. apparently, permission to
use nfc is not currently supported by webview. webview's chromeclient does not respond to a request
from the server to use nfc. permission is not granted. the server's javascript posts the error. i assume it
is a matter of updating android's webview (and its extensions).

while it is possible to make chromeclient grant permission to a request programmatically (not recommended
for security reasons), it is probably not possible to grant permission for which there was no recognized request.
i have an idea; i need a little time to look through an old project. i'll let you know. maybe today.
sorry, it doesn't appear what webchromeclient is asked to grant permission for nfc (at this time).

by the way, you could try adding "android.permisson.NFC" to your manifest, but i'd be surprised if it made any difference.
it's not a "dangerous" permission for android, like location.

i can tell you webviewextras won't work in this case. there are some other webview libraries you could try. i'm not
very familiar with them. they would have a webchromeclient and an event in b4a to handle permission requests.

---------------------------------------- update -----------------------------------------------------------
so i tested with the web nfc api in a webview. see attached. technically, webchromeclient says nfc is supported? but there is no way to grant the permission since it's not asked for ?. that's the holdup. see the log message on the right
 

Attachments

  • nfc-web.png
    nfc-web.png
    38 KB · Views: 53
Last edited:
Upvote 0
Top