怎样编程机器人寻找一个特定的网络? (问题下面这个简单的教程) [英] How do I program android to look for a particular network? (Issues Following Simple Tutorial)

查看:210
本文介绍了怎样编程机器人寻找一个特定的网络? (问题下面这个简单的教程)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试使用下面的例子:

<一个href=\"http://stackoverflow.com/questions/11060414/how-do-i-program-android-to-look-for-a-particular-network\">How我做Android的程序来寻找特定的网络?

但我无法执行它由于从线所产生的问题:

WifiManager wifiManager =(WifiManager)context.getSystemService(Context.WIFI_SERVICE);

在那之后我得到其他几个错误 - 但[我觉得]我跟着教程/例如precisely。

 进口的java.util.List;
进口android.app.Activity;
进口android.net.wifi.WifiConfiguration;
进口android.net.wifi.WifiManager;
进口android.os.Bundle;
进口android.util.Log;
进口android.content.Context;公共类连接扩展活动{
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.connect);
        字符串networkSSID =安德烈 - PC_NETWORK
        字符串networkPass =超人;        WifiConfiguration的conf =新WifiConfiguration();
        conf.SSID =\\+ networkSSID +\\; // SSID必须在引号
        conf.wepKeys [0] =\\+ networkPass +\\;
        conf.wepTxKeyIndex = 0;
        conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);        。CONF preSharedKey =\\+ networkPass +\\;        conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);        WifiManager wifiManager =(WifiManager)getSystemService(WIFI_SERVICE);
        wifiManager.addNetwork(CONF);        清单&LT; WifiConfiguration&GT;清单= wifiManager.getConfiguredNetworks();
        对于(WifiConfiguration我:名单){
            如果(i.SSID = NULL&放大器;!&安培; i.SSID.equals(\\+ networkSSID +\\)){
                 WifiManager.disconnect();
                 WifiManager.enableNetwork(i.networkId,真);
                 WifiManager.reconnect();                 打破;
            }
         }    }}

问题在Eclipse中的问题记录:

 描述资源路径位置类型
不能让一个静态引用非静态方法重新连接()从类型WifiManager Connect.java线41 Java问题
不能让一个静态引用非静态方法断开()从类型WifiManager Connect.java线39 Java问题
不能让从类型WifiManager Connect.java线40 Java问题的静态参考非静态方法enableNetwork(INT,布尔)


解决方案

您似乎混淆一些变量名:

  WifiManager wifiManager =(WifiManager)context.getSystemService(Context.WIFI_SERVICE);
wifiManager.add(CONF);清单&LT; WifiConfiguration&GT;清单= wifiManager.getConfiguredNetworks();
对于(WifiConfiguration我:名单){
    如果(i.SSID = NULL&放大器;!&安培; i.SSID.equals(\\+ networkSSID +\\)){
         wm.disconnect();
         wm.enableNetwork(i.networkId,真);
         wm.reconnect();
         打破;
    }
}

wifiManager WM ,反之亦然。此外背景这个或只是抛弃了,因为 getSystemService()是你的类的方法。

我不知道有关与)的问题,添加( ...

修改

添加() WifiManager ,但 addNetwork()呢,所以我想你需要的方法,而不是添加()

EDIT2

你根本不知道你在做什么,对不对?你甚至不明白一个类名和变量名之间的差别!

WifiManager wifiManager &LT; - 首先是类型,也被称为类。第二个是变量名。在你的循环使用类型/类而不是变量,所以只写 wifiManager 而不是 WifiManager

你应该明确地通过了解编程,面向对象和Java基础做起。认真...学习语言,你得试着写一个前...

I'm attempting to use the following example:

How do I program android to look for a particular network?

But I'm unable to execute it due to issues stemming from the line:

WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);

After that I'm getting several other errors - but [I think] I've followed the tutorial/example precisely.

import java.util.List;
import android.app.Activity;    
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.content.Context; 

public class Connect extends Activity  {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.connect);
        String networkSSID = "ANDRE-PC_NETWORK";
        String networkPass = "superman";

        WifiConfiguration conf = new WifiConfiguration();
        conf.SSID = "\"" + networkSSID + "\"";   //ssid must be in quotes


        conf.wepKeys[0] = "\"" + networkPass + "\""; 
        conf.wepTxKeyIndex = 0;
        conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); 

        conf.preSharedKey = "\""+ networkPass +"\"";

        conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);

        WifiManager wifiManager = (WifiManager)getSystemService(WIFI_SERVICE); 
        wifiManager.addNetwork(conf);

        List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
        for( WifiConfiguration i : list ) {
            if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
                 WifiManager.disconnect();
                 WifiManager.enableNetwork(i.networkId, true);
                 WifiManager.reconnect();                

                 break;
            }           
         }

    }}

ISSUES IN ECLIPSE PROBLEM LOG:

Description Resource    Path    Location    Type
Cannot make a static reference to the non-static method reconnect() from the type WifiManager   Connect.java        line 41 Java Problem
Cannot make a static reference to the non-static method disconnect() from the type WifiManager  Connect.java        line 39 Java Problem
Cannot make a static reference to the non-static method enableNetwork(int, boolean) from the type WifiManager   Connect.java        line 40 Java Problem

解决方案

You seem to confuse some variable names:

WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE); 
wifiManager.add(conf);

List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
    if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
         wm.disconnect();
         wm.enableNetwork(i.networkId, true);
         wm.reconnect();                
         break;
    }           
}

wifiManager should be wm or vice versa. Also context should be this or just left out as getSystemService() is a method of your class.

I am not sure about the issue with add()...

Edit:

Well add() does not exist for WifiManager but addNetwork() does, so I guess you need that method instead of add()

Edit2:

You have no idea what you are doing, right? You don't even understand the difference between a class name and a variable name!

WifiManager wifiManager <- First is the Type, also known as class. The second one is the variable name. In your for loop you use the type/class instead of the variable, so just write wifiManager instead of WifiManager.

And you should definitively start by understanding the very basics of programming, OOP and Java. Seriously... Learn the language before you try to write it...

这篇关于怎样编程机器人寻找一个特定的网络? (问题下面这个简单的教程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆