在Android中访问#WIFI栏

时间:2015-01-14 21:20:12

标签: android android-intent android-wifi wear-os wifimanager

这是我创建的接收器。问题在于它没有给我任何新的价值。我觉得它没有收到任何东西。我怎样才能让自己收到一些东西,为什么它不在这里工作?我用相同的方式对电池电量做了它并且它起作用了。最后,当我弄清楚如何收到某些东西时,我怎么才能得到WIFI吧(我想如果我让这个东西返回一些东西那么WIFI吧会更有意义)。

final BroadcastReceiver mWifiReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {

            final WifiManager wifi = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);

            int state = wifi.getWifiState();
            if(state == WifiManager.WIFI_STATE_ENABLED) {
                List<ScanResult> results = wifi.getScanResults();



                for (ScanResult result : results) {

                    if(result.BSSID.equals(wifi.getConnectionInfo().getBSSID())) {
                        int level = WifiManager.calculateSignalLevel(wifi.getConnectionInfo().getRssi(),
                                result.level);

                        int difference = level * 100 / result.level;

                        if(difference >= 100)
                            signalStrength = 4;
                        else if(difference >= 75)
                            signalStrength = 3;
                        else if(difference >= 50)
                            signalStrength = 2;
                        else if(difference >= 25)
                            signalStrength = 1;

                    }
                }
            }
        }
    };

    boolean mRegisteredWifiReceiver = false;

当然,我处理了这里没有看到的用于注册,取消注册和调用的函数。

我是这样做的:

  public void onVisibilityChanged(boolean visible) {
            super.onVisibilityChanged(visible);

            if (visible) {
                registerReceiver();
                registerWifiReceiver();
                // registerBatteryReceiver();
                // Update time zone in case it changed while we weren't visible.
                mTime.clear(TimeZone.getDefault().getID());
                mTime.setToNow();

                invalidate();
            } else {
                unregisterReceiver();
                unRegisterWifiReceiver();
                //unRegisterBatteryReceiver();
            }
        }
 private void registerWifiReceiver() {

            if (mRegisteredWifiReceiver)
                return;

            mRegisteredWifiReceiver = true;
            IntentFilter filter = new IntentFilter(WifiManager.RSSI_CHANGED_ACTION);
            WatchFaceService.this.registerReceiver(mWifiReceiver, filter);
        }

 private void unRegisterWifiReceiver() {

            if (!mRegisteredWifiReceiver)
                return;

            mRegisteredWifiReceiver = false;
            WatchFaceService.this.unregisterReceiver(mWifiReceiver);

}

0 个答案:

没有答案