Sorry if it’s a silly question…i m only a beginner in android programming.
i want to make an array of signal levels and access point mac addresses. Later my goal is to POST these two arrays to a server using JSON for getting a web service. i cannot find a right way to make the array..though i m able to retrieve the wifi information that android provides. see the commented out statements in the Onreceive method of the following code.
package com.example.jsonwifi;
public class JsonWifi extends Activity {
TextView mainText;
WifiManager mainWifi;
WifiReceiver receiverWifi;
List<ScanResult> wifiList;
StringBuilder sb = new StringBuilder();
String[] apmacs;
String[] levels;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainText = (TextView) findViewById(R.id.mainText);
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");
}
@Override
public boolean onCreateOptionsMenu(Menu mn) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.menu, mn);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.refreshbt:
mainWifi.startScan();
mainText.setText("Starting Scan");
default:
return super.onOptionsItemSelected(item);
}
}
protected void onPause() {
unregisterReceiver(receiverWifi);
super.onPause();
}
protected void onResume() {
registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
Toast.makeText(getBaseContext(), "resumed", Toast.LENGTH_LONG).show();
super.onResume();
}
class 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(" SSID= ");
sb.append((wifiList.get(i)).SSID.toString());
sb.append("\n");
sb.append(" BSSID= ");
sb.append((wifiList.get(i)).BSSID.toString());
sb.append(",");
sb.append(" signal level= ");
sb.append((wifiList.get(i)).level);
sb.append("\n");
// apmacs[]= (wifiList.get(i).BSSID);
// levels[] = (wifiList.get(i).level);
}
mainText.setText(sb);
}
}
}
seems like you want to build String arrays out of retrieved data stored in a
List<ScanResult>.. I’d initialize the arrays to the size of the List and populate them in side the loop… like thisSee if this works.