【问题标题】:How to automatically switch to the strongest WiFi access point with same SSID but multiple BSSID in nativescript?如何在nativescript中自动切换到具有相同SSID但多个BSSID的最强WiFi接入点?
【发布时间】:2022-04-26 20:08:10
【问题描述】:

我正在编写一个程序来自动切换到最强WiFi Access Point接入点 same SSIDdifferent BSSID

当存在信号强度高于当前连接的接入点的 Wifi 接入点时,程序应将 当前 access pointreconnect reconnect最强的 wifi 接入点可用。

在这种情况下,即使我找到带有 higher strength 的 wifi 网络,当前网络也不是 disconnected更新 到带有 @ 的接入点987654329@wifi信号。

因此,没有自动切换。

import { Injectable, NgZone } from '@angular/core';
import * as applicationModule from 'tns-core-modules/application';
import * as utils from 'tns-core-modules/utils/utils';
import { config } from '../data/config';

import { WifiCon } from '../interfaces/wifi-con';
import { LoggerService } from './logger.service';

declare var android: any;

@Injectable({
  providedIn: 'root'
})
export class WifiConService implements WifiCon {

  readonly ssid: string = config.SSID;
  readonly password: string = '12345678';
  oldBSSID = '';

  private readonly ntwkStatus: boolean;
  private readonly conBSSID: string;
  private readonly ConSubject: any;

  private readonly wifiManager: any;
  private connectedBSSID = '';
  private connectedNetworkId = 0;
  private isScanSuccessful = false;
  private netId = -1;

  private readonly items: Array<Item> = new Array<Item>();

  constructor(private readonly zone: NgZone, private readonly logger: LoggerService) {
    this.wifiManager = utils.ad.getApplicationContext()
    .getSystemService(android.content.Context.WIFI_SERVICE);
    this.monitorBSSID();
    this.logger.log('Created service for wifi');
  }

  connectWifi(): boolean {
    let isConnected = false;
    this.wifiManager.setWifiEnabled(true);
    const conf = new android.net.wifi.WifiConfiguration();
    conf.SSID = `"${this.ssid}"`;
    conf.preSharedKey = `"${this.password}"`;
    this.wifiManager.addNetwork(conf);
    // const configuredNetworks = this.wifiManager.getConfiguredNetworks();
    // for (let i = 0; i < configuredNetworks.size(); i++) {
    //   // console.log(configuredNetworks.get(i).SSID + " ; ");
    //   if (configuredNetworks.get(i).SSID !== undefined && configuredNetworks.get(i).SSID === `"${this.ssid}"`) {
    //     this.wifiManager.disconnect();
    //     isConnected = this.wifiManager.enableNetwork(configuredNetworks.get(i).networkId, true);
    //     break;
    //   }
    // }

    return isConnected;
  }

  connectWiFiNetwork(): void {
    const wifiConnections = this.items;
    this.logger.log(` Connections ${JSON.stringify(wifiConnections)}`);

    wifiConnections.sort((a, b) =>
        b.strength - a.strength);
    this.logger.log(`Sorted Connections ${JSON.stringify(wifiConnections)}`);

    if (this.oldBSSID !== wifiConnections[0].bssid) {

        let isConnected = false;
        // const password = '12345678';
        this.wifiManager.setWifiEnabled(true);
        // this.wifiManager.enableNetwork(this.wifiManager.getConnectionInfo().getNetworkId(), false);
        const conf = new android.net.wifi.WifiConfiguration();

        conf.networkId = wifiConnections[0].netId;
        conf.BSSID = wifiConnections[0].bssid;
        // console.log("Configured Net Id", conf.networkId);
        // console.log("Configured BSSID", conf.BSSID);
        // console.log("Current network ID", this.wifiManager.getConfiguredNetworks().networkId);
        // const isRemoved = this.wifiManager.removeNetwork(this.wifiManager.getConnectionInfo()
        // .getNetworkId());
        // if (isRemoved === true)
        // {
        //   console.log("Removed network");
        // }
        // this.connectedNetworkId = this.wifiManager.addNetwork(conf);
        this.netId = this.wifiManager.updateNetwork(conf);
        // console.log("****************Net Id ", this.connectedNetworkId);
        // console.log("****************Current Net Id", this.connectedNetworkId);
        isConnected = this.wifiManager.enableNetwork(this.netId, true);
        this.wifiManager.reconnect();
        this.oldBSSID = wifiConnections[0].bssid;
    }
}

  disConnectWifi(): boolean {
    this.logger.log(`WiFi disconnected
Not connected to "Wi-Fi VLAN"`);

    return this.wifiManager.setWifiEnabled(false);
  }

  getConSubject(): boolean {
    return true;
  }

  switchToStrongNtwk(BSSID: string): boolean {
    return true;
  }

  getNtwkInfo(): any {
    return this.wifiManager.getConnectionInfo();
  }

  isWifiEnabled(): boolean {
    return this.wifiManager.isWifiEnabled();
  }

  monitorBSSID(): void {
    const receiverCallback = (androidContext, intent) => {
      if (this.isScanSuccessful) {
        this.connectedNetworkId = this.getNtwkInfo().getNetworkId();
        this.connectedBSSID = this.getNtwkInfo().getBSSID();
        this.logger.log('Get Scan Result - ');
        const scanResults = this.wifiManager.getScanResults();
        this.zone.run(() => {
          this.items.length = 0;
          for (let n = 0; n < scanResults.size(); n++) {
            if (scanResults.get(n).SSID == this.ssid) {
              this.items.push({ netId: scanResults.get(n).networkId, ssid: scanResults.get(n).SSID, bssid: scanResults.get(n).BSSID,
                                strength: scanResults.get(n).level, isConnected: (scanResults.get(n).BSSID === this.connectedBSSID) });
            }
          }
          this.logger.log(`Connections ${JSON.stringify(this.items)}`);
          this.connectWiFiNetwork();
        });
      }
    };

    applicationModule.android.registerBroadcastReceiver(
      android.net.wifi.WifiManager.SCAN_RESULTS_AVAILABLE_ACTION,
      receiverCallback
    );

    setInterval(() => {
      this.logger.log('Scanning for WiFi Connection');
      this.isScanSuccessful = this.wifiManager.startScan();
      // console.log("isScanSuccessful", this.isScanSuccessful);
    }, config.wifi_scan_time * 1000);
  }
}

interface Item {
  ssid: string;
  bssid: string;
  strength: number;
  isConnected: boolean;
  netId: number;
}

【问题讨论】:

    标签: android-wifi angular2-nativescript wifimanager wifi


    【解决方案1】:

    由于没有人回答,我会回答。 在 android 9(API 级别 28)之前,reassociate() 将完成切换接入点的工作,而不会断开与先前连接的接入点的连接。 使用this.wifiManager.reassociate() 而不是this.wifiManager.reconnect()

    对于 android 10 及更高版本,不推荐使用 reassociate() 和许多 WifiManager 函数。 Android 10 引入了大量基于隐私的更改和restrictions which restrict the enabling and disabling of wifidirect access to configured Wi-Fi networks。因此,无法以编程方式切换接入点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-09
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-26
      • 1970-01-01
      相关资源
      最近更新 更多