How to connect wifi?


#1

How to connect wifi to ionic and angular 4?
I am done scanning the wifi and show it in list view but I don’t know how to connect wifi device
can pls help me to how to connect to wifi in ionic 3
I am attached scanned wifi list image.
Here I am using hotspot plugin to list the wifi device.


#2

If you’ve used this plugin: https://ionicframework.com/docs/native/hotspot/

Then you can connect to wifi with following code:

this.hotspot.connectToWifi(ssid, password).then( () => {
     //connection to the WiFi network was successfull
}).catch( () => {
    //connection to the WiFi network failed
});

where ssid is name of any one wifi network from the list which you’ve already got.

If you’ve any doubts, feel free to ask.


Find list and connect to specificed wifi
#3

Actually this my code.What must be error.

Home.ts

import { Component } from ‘@angular/core’;
import {AlertController, NavController, Platform} from ‘ionic-angular’;
import { Hotspot, HotspotNetwork } from ‘@ionic-native/hotspot’;
import {AppMinimize} from “@ionic-native/app-minimize”;

@Component({
selector: ‘page-home’,
templateUrl: ‘home.html’
})
export class HomePage {
data: any;
constructor(private platform: Platform, private appMinimize: AppMinimize,public navCtrl: NavController,private hotspot: Hotspot,public alertCtrl: AlertController) {
this.platform.registerBackButtonAction(() => {
this.appMinimize.minimize();
});
}

ionViewDidLoad(){
this.hotspot.scanWifi().then((networks: Array) => {

  this.data=networks;
  console.log(".........hotspot..........",JSON.stringify(networks));
});

}

pass(SSID){

let prompt = this.alertCtrl.create({
  title: SSID,
  message: "Enter a name for this new album you're so keen on adding",
  inputs: [
    {
      name: 'title',
      placeholder: 'Title'
    },
  ],
  buttons: [
    {
      text: 'Cancel',
      handler: data => {
        console.log('Cancel clicked');
      }
    },
    {
      text: 'Save',
      handler: Password => {
       
        console.log("password",Password.title,SSID);
        this.hotspot.connectToWifi(SSID, Password)
          .then((data) => {

          console.log(".........hotspot..........",data);
        }, (error) => {
            console.log(".........hotspot..........",error);
          })

      }
    }
  ]
});
prompt.present();

}

}


#4

Home.html

<ion-header>
  <ion-navbar>
    <ion-title>
     Wifi Scanner
    </ion-title>
  </ion-navbar>
</ion-header>

<ion-content>
  <ion-list >
    <ion-item *ngFor="let x of data" (tap)="pass(x.SSID)">
      <ion-avatar item-start>
        <img src="assets/img/images.png">
      </ion-avatar >
      <p>{{x.SSID}}</p>
    </ion-item>
  </ion-list>
</ion-content>

#5

There is one error in above code:
In your connectToWifi method 2nd parameter is wrong. It should be like this:

       console.log("password",Password.title,SSID);
       //2nd parameter should be Password.title
       this.hotspot.connectToWifi(SSID, Password.title) 
          .then((data) => {
                 console.log(".........hotspot..........",data);
            }, (error) => {
                 console.log(".........hotspot..........",error);
            });

#6

I try it .But it connected to pervious wifi device
can you help me


#7

are you getting any error?


#8

I am tried it now
But it connect to the pervious device may know why it happening


#9

I try to do disable the previous network but i try to connect means this shows like this message


#10

Can you explain the scenario what you did and what exactly is happening?
Because log message shows that first time your connection to wifi network “Connecting…” was successful.


#11

yes, actually the first time I am trying to connect.But it connects to previous wifi device.
And show message like this

“connection was successfull”

Second time disable the previous wifi device(wifi setting/Forgot network) and trying to connect to (connecting…) wifi device but it show error message

“connection was not successful”


#12

Oh…sorry. There is already one ongoing issue on this plugin: https://github.com/hypery2k/cordova-hotspot-plugin/issues/57

Many people are facing this kind of issue.

Someone suggested one workaround on that issue, try and see if it works for you.

       console.log("password",Password.title,SSID);
       this.hotspot.removeWifiNetwork(SSID).then( () => {
            this.hotspot.connectToWifi(SSID, Password.title) 
                .then((data) => {
                      console.log(".........hotspot..........",data);
                 }, (error) => {
                      console.log(".........hotspot..........",error);
                 });
       }, () => {
       });

#13

ok i am trying @Ankit_Makwana


#14

No , again showing like that error


#15

Ok…I will try myself and see if there is some issue.
I will reply back if I’ll find anything useful.


#16

ok @Ankit_Makwana i am also trying


#17

you get any solution ankit makwana


#18

Nope…and that guy who made the plugin has discontinued the project.


#19

Maybe you can try WifiWizard:

Never tried it and I noticed some issues while reading about it but someone managed it to work:

It does contains methods to connect to a WiFi net (even if at a first glance I can’t figure out how to pass the password as an argument… maybe you have to study it a bit):


#20

ok @Ankit_Makwana
I will try some other plugin