Android-runtime: NativeScript RequestPermission undefined

Created on 23 Mar 2016  路  10Comments  路  Source: NativeScript/android-runtime

_From @mabelanski on March 23, 2016 7:11_

How i can do for use android.support.v4.app.ActivityCompat.requestPermissions or android.app.Activity.requestPermissions ?
Because it's always undefined

I want to do this

var context = android.content.Context;
var wifi_service = application.android.context.getSystemService(context.WIFI_SERVICE);

wifi_service.setWifiEnabled(true);  
var rs = wifi_service.startScan();

console.log(android.support.v4.app.ActivityCompat.requestPermissions); // undefined
console.log(android.support.v4.content.ContextCompat.requestPermissions); // undefined

application.android.registerBroadcastReceiver(
  android.net.wifi.WifiManager.SCAN_RESULTS_AVAILABLE_ACTION,
  function onReceiveCallback(context, intent) {      
     var tp = wifi_service.getScanResults();
     console.log(tp); // empty []
     console.log(tp.size()); // 0
  });

but array tp is always empty and i thing that because i need the right permission, and on android 6 we need to ask it

i also add on manifestfile :

<uses-feature android:name="android.hardware.wifi" />
<uses-feature android:name="android.hardware.location" android:required="false" />
<uses-feature android:name="android.hardware.location.network" android:required="false" />
<uses-feature android:name="android.hardware.location.gps" android:required="false" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission-sdk-23 android:name="android.permission.ACCESS_COARSE_LOCATION" />

also add on C:...\app\App_Resources\Android\app.gradle :

dependencies {
    compile "com.android.support:support-v4:+"
    , compile "com.android.support:appcompat-v7:+"
}

Thanks

_Copied from original issue: NativeScript/NativeScript#1828_

question

All 10 comments

Hi @mabelanski,

  • also add on C:...\app\App_Resources\Android\app.gradle :
dependencies {
    compile "com.android.support:support-v4:+"
    , compile "com.android.support:appcompat-v7:+"
}

There is no need to add these particular libraries, because we already have added them as compile dependencies (as you have), in the native project template

  • console.log(android.support.v4.app.ActivityCompat.requestPermissions); // undefined
    I pasted your code on a project created with CLI version 1.7.0 and it prints function () { [native code] } which is exactly what it should do. So what version of CLI are you using? You can check that out by running the following command tns --version.
  • console.log(android.support.v4.content.ContextCompat.requestPermissions); // undefined
    This line should return undefined, because the class android.support.v4.content.ContextCompat has no method called requestPermissions.

EDIT:

  • var wifi_service = application.android.context.getSystemService(context.WIFI_SERVICE);
    if you didn't have the right permission the app would instantly crash when you call this line.

Hi @Plamen5kov ,

Thanks for your feedback ;)
I'm with CLI 1.6.2

I will test with 1.7.0 right now

Do you have any idear why my getscanlist result is empty ?

Hi @Plamen5kov

I updated CLI to 1.7.0
I remove and re-add plateform android

but it the same issue
Maybe miss me some sdk package ? with sdk manager

sdk

Thanks

@mabelanski could you post the logcat of the error or a snapshot of the error message you receive.

There is no error
but console.log(android.support.v4.app.ActivityCompat.requestPermissions); is undefined and scan result is empy

@mabelanski I believe this is an issue with the app and not with the android runtime. I'd suggest to ask in the {N} slack channel called Nativescript Community, or ask in the {N} google group

@mabelanski Can you send us the app package, if this is possible?

Hi guys,

I created a new application and i start it from the beginning and that work ! :)
I think that upgrade of CLI solved the issue, and we need to re-create an app because the \node_modules\tns-core-modules it's not the same

Here the code to fix the scan result issue :

    var context = android.content.Context;
    var wifi_service = application.android.context.getSystemService(context.WIFI_SERVICE);
    wifi_service.setWifiEnabled(true);  

    var rs = wifi_service.startScan();

    var hasPermission = android.os.Build.VERSION.SDK_INT < 23;
    if (!hasPermission) {
        hasPermission = android.content.pm.PackageManager.PERMISSION_GRANTED ==
        android.support.v4.content.ContextCompat.checkSelfPermission(application.android.foregroundActivity, android.Manifest.permission.ACCESS_COARSE_LOCATION);
    }

    var ACCESS_COARSE_LOCATION_PERMISSION_REQUEST_CODE = 555;

    if (!hasPermission) {
        android.support.v4.app.ActivityCompat.requestPermissions(
                        application.android.foregroundActivity,
                        [android.Manifest.permission.ACCESS_COARSE_LOCATION],
                        ACCESS_COARSE_LOCATION_PERMISSION_REQUEST_CODE);
    }

    application.android.registerBroadcastReceiver(
            android.net.wifi.WifiManager.SCAN_RESULTS_AVAILABLE_ACTION
            , function onReceiveCallback(context, intent) {    
                var tp = wifi_service.getScanResults();
                console.log(tp);
            });

Had run into this same issue. The following commands worked for me

tns platform remove android
tns platform add android
Was this page helpful?
0 / 5 - 0 ratings