hypery2k / cordova-hotspot-plugin

A Cordova plugin for managing HotSpot networks on Android
MIT License
37 stars 49 forks source link

Getting Started #62

Closed vicatcu closed 7 years ago

vicatcu commented 7 years ago

I am getting my feet wet with this plugin and made a bare bones ionic2 application to try it out. Did that using:

ionic start ionicwifiscan tabs --v2
ionic platform add android
ionic plugin add cordova-plugin-hotspot
ionic --version
2.2.1
cordova --version
6.4.0

Changed src/pages/home.ts to this:

import { Component } from '@angular/core';

import { NavController } from 'ionic-angular';
import { Hotspot, Network } from 'ionic-native';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  networks: Network[] = [];
  counter = 0;
  errors = "no errors";
  anything = "nothing yet";
  constructor(public navCtrl: NavController) {

  }

  ionViewDidEnter(){
    this.rescanNetworks()
  }

  rescanNetworks(){
    console.log("Rescanning Networks");
    this.counter++;
    let cnt = this.counter;
    Hotspot.scanWifi().then((networks: Network[]) => {
      this.anything = `callback reached ${cnt}`;
      this.networks = networks;
    }).catch((e) => {
      this.errors = e.message;
    });
  }

}

Changed src/pages/home.html to this:

<ion-header>
  <ion-navbar>
    <ion-title>Home</ion-title>
  </ion-navbar>
</ion-header>

<ion-content>
  <button ion-button (click)="rescanNetworks($event)">Scan for Networks</button>
  <p>Counter: {{ counter }}</p>
  <p>Networks: {{ networks | json }}</p>
  <p>Errors: {{ errors }}</p>
  <p>Anything: {{ anything }}</p>
</ion-content>

Loaded it onto my Galaxy S7 over USB with the command : ionic run android Neither the then(), nor the catch(), seem to execute on the promise returned by HotSpot.scanWifi(), like the promise never resolves or rejects. So, with my phone still attached, I ran the command adb shell, and then from with the shell logcat and scanned the output for references to HotSpotPlugin and Cordova, and I encountered the following log messages:

02-26 17:20:36.482  8794  8794 I chromium: [INFO:CONSOLE(56138)] "Rescanning Networks", source: file:///android_asset/www/build/main.js (56138)
02-26 17:20:36.492  8794  8988 D HotSpotPlugin: Can Write Settings: false
02-26 17:20:36.492  8794  8988 W CordovaPlugin: Attempted to send a second callback for ID: HotSpotPlugin1422796708
02-26 17:20:36.492  8794  8988 W CordovaPlugin: Result was: "Invalid action"

I also came across this other issue that was closed 3 days ago, and this reference from the wiki. And I found the manifest in the ionic project under platforms/android/AndroidManifest.xml, but it's not clear to me what is critical for compliance with the needs of this plugin. Currently my manifest looks like this:

<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="0.0.1" package="com.ionicframework.ionicwifiscan371508" xmlns:android="http://schemas.android.com/apk/res/android">
    <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
    <application android:hardwareAccelerated="true" android:icon="@mipmap/icon" android:label="@string/app_name" android:supportsRtl="true">
        <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize">
            <intent-filter android:label="@string/launcher_name">
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="21" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</manifest>

... the original Manifest generated by ionic looked like this:

<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="0.0.1" package="com.ionicframework.ionicwifiscan371508" xmlns:android="http://schemas.android.com/apk/res/android">
    <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application android:hardwareAccelerated="true" android:icon="@mipmap/icon" android:label="@string/app_name" android:supportsRtl="true">
        <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize">
            <intent-filter android:label="@string/launcher_name">
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="24" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
</manifest>

Just like @alvaroslm in issue #55 I'm just wanting to scan available networks, connect and disconnect from them in my app. Can anyone out there tell me where I'm going wrong here?

vicatcu commented 7 years ago

I also just noticed, if I do the following:

cordova plugin remove cordova-plugin-hotspot
ionic plugin add cordova-plugin-hotspot
ionic plugin list

I get: cordova-plugin-hotspot 1.2.7 "Cordova HotSpot Plugin" ... even though 3 days ago 1.2.8 was "released" and the changes therein seem possibly related? How do I tell cordova to give me 1.2.8?

Wierdly, even if I say ionic plugin add cordova-plugin-hotspot@1.2.8 I still end up with 1.2.7. And npm says 1.2.8.

vicatcu commented 7 years ago

Continuing documentation of my personal odyssey, I was able to get my version updated in the project by following the breadcrumbs on this SO post. I did:

npm install -g cordova-check-plugins
cordova-check-plugins --update=auto

... and got this output...

***************************
* Plugin update available *
***************************

plugin: cordova-plugin-hotspot
source: npm://cordova-plugin-hotspot@1.2.8
installed version: 1.2.7
remote version: 1.2.8
Updated 'cordova-plugin-hotspot' from 1.2.7 to 1.2.8

... still not getting any action out of the plugin. I also went out and found apktool, so that I could read the manifest from the created APK file, and it's contents looks like this:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:hardwareAccelerated="true" package="com.ionicframework.ionicwifiscan371508" platformBuildVersionCode="24" platformBuildVersionName="7.0">
    <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_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.INTERNET"/>
    <application android:debuggable="true" android:hardwareAccelerated="true" android:icon="@mipmap/icon" android:label="@string/app_name" android:supportsRtl="true">
        <activity android:configChanges="locale|keyboard|keyboardHidden|orientation|screenSize" android:label="@string/activity_name" android:launchMode="singleTop" android:name="com.ionicframework.ionicwifiscan371508.MainActivity" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize">
            <intent-filter android:label="@string/launcher_name">
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
        </activity>
    </application>
</manifest>
vicatcu commented 7 years ago

@hypery2k any ideas? i'm eager to get this working :-)

hypery2k commented 7 years ago

sorry, but didn't know what exactly happened that it doesn't work for you. But currently I'm very busy with other stuff.

vicatcu commented 7 years ago

@hypery2k no problem, busy is good, congrats :-)

shivamsngh commented 7 years ago

facing the same problem as @vicatcu getting older version 1.2.7 always

shivamsngh commented 7 years ago

the changelog says version 1.2.8 and before that 1.2.6, I guess some versioning issue, also the permission removed in version 8(write_settings) doenot exist in my plugin.

shivamsngh commented 7 years ago

In the mean time I added "uses-permission android:name="android.permission.WRITE_SETTINGS" this line, (which was removed in 1.2.8 build) into my Android.manifest.xml file and my app works now.