2011-09-05 84 views
0

我正在開發一個基於WiFi的應用,我有一個程序與我,我還添加了權限,但它沒有給予迴應。使用WiFi應用搜索Android設備

如何測試或模擬一次?

我加入此權限塊:

<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); 

      } 

    } 
} 
+0

請編輯和重新格式化代碼:

WiFi設備可以在無線上網的ad hoc模式,它不支持在Android潛在地發現。 – 2011-09-05 11:47:10

+1

@alextsc:編輯 – Kheldar

+0

謝謝。這更好。 :) – 2011-09-05 13:20:04

回答

相關問題