使用WiFi应用搜索Android设备

时间:2011-09-05 11:43:03

标签: android

我正在开发基于WiFi的应用程序,我有一个程序与我,我还添加了权限,但它没有给出响应。

如何测试或模拟它?

我添加了此权限块:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_CHECKIN_PROPERTIES"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE"></uses-permission>

这是我的来源:

 package org.example.WiFi;
 import java.util.List;
 import android.app.Activity; 
 import android.content.BroadcastReceiver; 
 import android.content.Context; 
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.net.wifi.ScanResult; 
 import android.net.wifi.WifiManager;
 import android.os.Bundle; 
 import android.view.Menu;
 import android.view.MenuItem;
 import android.widget.TextView;

 public class WiFiActivity extends Activity {
    TextView mainText;

    WifiManager mainWifi;

    WifiReceiver receiverWifi;

    List<ScanResult> wifiList;

    StringBuilder sb = new StringBuilder();



public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.search);

    mainText = (TextView) findViewById(R.id.search);

    mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);


    receiverWifi = new WifiReceiver();

    registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));

    mainWifi.startScan();

    mainText.setText("\nStarting Scan...\n");

}
    public boolean onCreateOptionsMenu(Menu menu) {

            menu.add(0, 0, 0, "Refresh");

            return super.onCreateOptionsMenu(menu);

    }
    public boolean onMenuItemSelected(int featureId, MenuItem item) {

            mainWifi.startScan();

            mainText.setText("Starting Scan");

            return super.onMenuItemSelected(featureId, item);

    }
    protected void onPause() {

            unregisterReceiver(receiverWifi);

            super.onPause();

    }

    protected void onResume() {

            registerReceiver(receiverWifi, new 

    IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));

            super.onResume();

    }
     ass WifiReceiver extends BroadcastReceiver {

            public void onReceive(Context c, Intent intent) {

                    sb = new StringBuilder();

                    wifiList = mainWifi.getScanResults();

            for(int i = 0; i < wifiList.size(); i++){

                    sb.append(new Integer(i+1).toString() + ".");

                    sb.append((wifiList.get(i)).toString());

                    sb.append("\n");

            }
            mainText.setText(sb);

            }

    } 
}

1 个答案:

答案 0 :(得分:0)

您的标题是“使用WiFi应用搜索Android设备”。在Android上,您只能搜索Wifi接入点,而不能搜索设备。

Wifi设备可能会在Wifi ad hoc模式中被发现,但Android不支持:http://code.google.com/p/android/issues/detail?id=82