List of usage examples for android.net.wifi WifiInfo getRssi
public int getRssi()
From source file:alaindc.crowdroid.RadioUtils.java
public static String[] getWifiInfo(Context context) { try {//from w w w . j a v a2s . co m WifiManager wifiMan = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiMan.getConnectionInfo(); String bssid = wifiInfo.getBSSID(); String ssid = wifiInfo.getSSID(); String signalStrength = String.valueOf(wifiInfo.getRssi()); // Update view Intent senseintent = new Intent(Constants.INTENT_UPDATE_SENSORS); senseintent.putExtra(Constants.INTENT_RECEIVED_DATA_EXTRA_DATA, ssid + " " + signalStrength); LocalBroadcastManager.getInstance(context).sendBroadcast(senseintent); return new String[] { bssid, ssid, signalStrength }; } catch (Exception e) { return new String[] { "", "", "" }; } }
From source file:com.googlecode.android_scripting.jsonrpc.JsonBuilder.java
private static JSONObject buildJsonWifiInfo(WifiInfo data) throws JSONException { JSONObject result = new JSONObject(); result.put("hidden_ssid", data.getHiddenSSID()); result.put("ip_address", data.getIpAddress()); result.put("link_speed", data.getLinkSpeed()); result.put("network_id", data.getNetworkId()); result.put("rssi", data.getRssi()); result.put("bssid", data.getBSSID()); result.put("mac_address", data.getMacAddress()); result.put("ssid", data.getSSID()); String supplicantState = ""; switch (data.getSupplicantState()) { case ASSOCIATED: supplicantState = "associated"; break;/*w ww . j a v a 2 s .c o m*/ case ASSOCIATING: supplicantState = "associating"; break; case COMPLETED: supplicantState = "completed"; break; case DISCONNECTED: supplicantState = "disconnected"; break; case DORMANT: supplicantState = "dormant"; break; case FOUR_WAY_HANDSHAKE: supplicantState = "four_way_handshake"; break; case GROUP_HANDSHAKE: supplicantState = "group_handshake"; break; case INACTIVE: supplicantState = "inactive"; break; case INVALID: supplicantState = "invalid"; break; case SCANNING: supplicantState = "scanning"; break; case UNINITIALIZED: supplicantState = "uninitialized"; break; default: supplicantState = null; } result.put("supplicant_state", build(supplicantState)); return result; }
From source file:org.metawatch.manager.Monitors.java
private static void createWifiReceiver(final Context context) { if (wifiReceiver != null) return;/*from w w w . j a v a2 s. co m*/ WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo info = wm.getConnectionInfo(); if (info != null) SignalData.wifiBars = 1 + WifiManager.calculateSignalLevel(info.getRssi(), 4); wifiReceiver = new BroadcastReceiver() { int wifiBars = 0; @Override public void onReceive(Context c, Intent intent) { String action = intent.getAction(); if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) { if (intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN) != WifiManager.WIFI_STATE_ENABLED) { wifiBars = 0; } } else if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) { if (!intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false)) { wifiBars = 0; } } else if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) { NetworkInfo netInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); if (netInfo.getState() != NetworkInfo.State.CONNECTED) { wifiBars = 0; } else { WifiInfo wifiInfo = intent.getParcelableExtra(WifiManager.EXTRA_WIFI_INFO); if (wifiInfo == null) { wifiBars = 0; } else { wifiBars = 1 + WifiManager.calculateSignalLevel(wifiInfo.getRssi(), 4); } } } else if (action.equals(WifiManager.RSSI_CHANGED_ACTION)) { final int newRssi = intent.getIntExtra(WifiManager.EXTRA_NEW_RSSI, -200); wifiBars = 1 + WifiManager.calculateSignalLevel(newRssi, 4); } if (wifiBars != SignalData.wifiBars) { SignalData.wifiBars = wifiBars; Idle.updateIdle(context, true); } } }; IntentFilter f = new IntentFilter(); f.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); f.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION); f.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); f.addAction(WifiManager.RSSI_CHANGED_ACTION); context.registerReceiver(wifiReceiver, f); }
From source file:org.wso2.emm.agent.services.DeviceNetworkStatus.java
private int getWifiSignalStrength() { if (wifiManager != null) { WifiInfo wifiInfo = wifiManager.getConnectionInfo(); return wifiInfo.getRssi(); }//w ww. ja va 2s. c o m return -1; }
From source file:org.wahtod.wififixer.ui.StatusFragment.java
private void refresh() { WifiInfo info = getNetwork(getContext()); if (info == null) { _views.setSsid(getContext().getString(R.string.wifi_is_disabled)); _views.setSignal(EMPTYSTRING);// w ww . ja v a 2 s.c o m _views.setLinkspeed(EMPTYSTRING); _views.setStatus(EMPTYSTRING); _views.setIcon(R.drawable.icon); } else if (info.getRssi() == -200) { _views.setSsid(EMPTYSTRING); _views.setSignal(EMPTYSTRING); _views.setLinkspeed(EMPTYSTRING); _views.setIcon(R.drawable.icon); } else { _views.setSsid(StringUtil.removeQuotes(info.getSSID())); _views.setSignal(String.valueOf(info.getRssi()) + DBM); _views.setLinkspeed(String.valueOf(info.getLinkSpeed()) + MB); _views.setStatus(info.getSupplicantState().name()); _views.setIcon(NotifUtil.getIconfromSignal(WifiManager.calculateSignalLevel(info.getRssi(), 5), NotifUtil.ICON_SET_LARGE)); } drawhandler.sendEmptyMessageDelayed(REFRESH, REFRESH_DELAY); }
From source file:ch.ethz.coss.nervousnet.vm.sensors.ConnectivitySensor.java
public void runConnectivitySensor() { Log.d(LOG_TAG, "Inside runConnectivitySensor"); if (Build.VERSION.SDK_INT >= 23 && ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return;//from w ww. j a va 2s . co m } ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting(); int networkType = -1; boolean isRoaming = false; if (isConnected) { networkType = activeNetwork.getType(); isRoaming = activeNetwork.isRoaming(); } String wifiHashId = ""; int wifiStrength = Integer.MIN_VALUE; if (networkType == ConnectivityManager.TYPE_WIFI) { WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo wi = wm.getConnectionInfo(); StringBuilder wifiInfoBuilder = new StringBuilder(); wifiInfoBuilder.append(wi.getBSSID()); wifiInfoBuilder.append(wi.getSSID()); try { MessageDigest messageDigest = MessageDigest.getInstance("SHA-256"); messageDigest.update(wifiInfoBuilder.toString().getBytes()); wifiHashId = new String(messageDigest.digest()); } catch (NoSuchAlgorithmException e) { } wifiStrength = wi.getRssi(); } byte[] cdmaHashId = new byte[32]; byte[] lteHashId = new byte[32]; byte[] gsmHashId = new byte[32]; byte[] wcdmaHashId = new byte[32]; TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); List<CellInfo> cis = tm.getAllCellInfo(); if (cis != null) { // New method for (CellInfo ci : cis) { if (ci.isRegistered()) { if (ci instanceof CellInfoCdma) { CellInfoCdma cic = (CellInfoCdma) ci; cdmaHashId = generateMobileDigestId(cic.getCellIdentity().getSystemId(), cic.getCellIdentity().getNetworkId(), cic.getCellIdentity().getBasestationId()); } if (ci instanceof CellInfoGsm) { CellInfoGsm cic = (CellInfoGsm) ci; gsmHashId = generateMobileDigestId(cic.getCellIdentity().getMcc(), cic.getCellIdentity().getMnc(), cic.getCellIdentity().getCid()); } if (ci instanceof CellInfoLte) { CellInfoLte cic = (CellInfoLte) ci; lteHashId = generateMobileDigestId(cic.getCellIdentity().getMcc(), cic.getCellIdentity().getMnc(), cic.getCellIdentity().getCi()); } if (ci instanceof CellInfoWcdma) { CellInfoWcdma cic = (CellInfoWcdma) ci; wcdmaHashId = generateMobileDigestId(cic.getCellIdentity().getMcc(), cic.getCellIdentity().getMnc(), cic.getCellIdentity().getCid()); } } } } else { // Legacy method CellLocation cl = tm.getCellLocation(); if (cl instanceof CdmaCellLocation) { CdmaCellLocation cic = (CdmaCellLocation) cl; cdmaHashId = generateMobileDigestId(cic.getSystemId(), cic.getNetworkId(), cic.getBaseStationId()); } if (cl instanceof GsmCellLocation) { GsmCellLocation cic = (GsmCellLocation) cl; gsmHashId = generateMobileDigestId(cic.getLac(), 0, cic.getCid()); } } StringBuilder mobileHashBuilder = new StringBuilder(); mobileHashBuilder.append(new String(cdmaHashId)); mobileHashBuilder.append(new String(lteHashId)); mobileHashBuilder.append(new String(gsmHashId)); mobileHashBuilder.append(new String(wcdmaHashId)); dataReady(new ConnectivityReading(System.currentTimeMillis(), isConnected, networkType, isRoaming, wifiHashId, wifiStrength, mobileHashBuilder.toString())); }
From source file:fr.inria.ucn.collectors.NetworkStateCollector.java
private JSONObject getWifi(Context c) throws JSONException { WifiManager wm = (WifiManager) c.getSystemService(Context.WIFI_SERVICE); WifiInfo wi = wm.getConnectionInfo(); // start a wifi AP scan Helpers.acquireWifiLock(c);//w w w .ja va 2 s. c om IntentFilter filter = new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); c.registerReceiver(this, filter); wm.startScan(); JSONObject o = new JSONObject(); o.put("link_speed", wi.getLinkSpeed()); o.put("link_speed_units", WifiInfo.LINK_SPEED_UNITS); o.put("signal_level", WifiManager.calculateSignalLevel(wi.getRssi(), 100)); o.put("rssi", wi.getRssi()); o.put("bssid", wi.getBSSID()); o.put("ssid", wi.getSSID().replaceAll("\"", "")); o.put("mac", wi.getMacAddress()); int ip = wi.getIpAddress(); String ipstr = String.format(Locale.US, "%d.%d.%d.%d", (ip & 0xff), (ip >> 8 & 0xff), (ip >> 16 & 0xff), (ip >> 24 & 0xff)); o.put("ip", ipstr); return o; }
From source file:com.landenlabs.all_devtool.ConsoleFragment.java
@SuppressLint("DefaultLocale") private void updateConsole() { if (mSystemViews.isEmpty()) return;/*from ww w .j ava2s.c o m*/ try { // ----- System ----- int threadCount = Thread.activeCount(); ApplicationInfo appInfo = getActivity().getApplicationInfo(); mSystemViews.get(SYSTEM_PACKAGE).get(1).setText(appInfo.packageName); mSystemViews.get(SYSTEM_MODEL).get(1).setText(Build.MODEL); mSystemViews.get(SYSTEM_ANDROID).get(1).setText(Build.VERSION.RELEASE); if (Build.VERSION.SDK_INT >= 16) { int lines = 0; final StringBuilder permSb = new StringBuilder(); try { PackageInfo pi = getContext().getPackageManager().getPackageInfo(getContext().getPackageName(), PackageManager.GET_PERMISSIONS); for (int i = 0; i < pi.requestedPermissions.length; i++) { if ((pi.requestedPermissionsFlags[i] & PackageInfo.REQUESTED_PERMISSION_GRANTED) != 0) { permSb.append(pi.requestedPermissions[i]).append("\n"); lines++; } } } catch (Exception e) { } final int lineCnt = lines; mSystemViews.get(SYSTEM_PERM).get(1).setText(String.format("%d perms [press]", lines)); mSystemViews.get(SYSTEM_PERM).get(1).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (v.getTag() == null) { String permStr = permSb.toString().replaceAll("android.permission.", "") .replaceAll("\n[^\n]*permission", ""); mSystemViews.get(SYSTEM_PERM).get(1).setText(permStr); mSystemViews.get(SYSTEM_PERM).get(0).setLines(lineCnt); mSystemViews.get(SYSTEM_PERM).get(1).setLines(lineCnt); mSystemViews.get(SYSTEM_PERM).get(1).setTag(lineCnt); } else { mSystemViews.get(SYSTEM_PERM).get(1).setText(String.format("%d perms", lineCnt)); mSystemViews.get(SYSTEM_PERM).get(0).setLines(1); mSystemViews.get(SYSTEM_PERM).get(1).setLines(1); mSystemViews.get(SYSTEM_PERM).get(1).setTag(null); } } }); } else { String permStr = ""; if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) permStr += (" Find Loc"); if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) permStr += (" Coarse Loc"); mSystemViews.get(SYSTEM_PERM).get(1).setText(permStr); } ActivityManager actMgr = (ActivityManager) getActivity().getSystemService(Context.ACTIVITY_SERVICE); int processCnt = actMgr.getRunningAppProcesses().size(); mSystemViews.get(SYSTEM_PROCESSES).get(1).setText(String.format("%d", consoleState.processCnt)); mSystemViews.get(SYSTEM_PROCESSES).get(2).setText(String.format("%d", processCnt)); // mSystemViews.get(SYSTEM_BATTERY).get(1).setText(String.format("%d%%", consoleState.batteryLevel)); mSystemViews.get(SYSTEM_BATTERY).get(2) .setText(String.format("%%%d", calculateBatteryLevel(getActivity()))); // long cpuNano = Debug.threadCpuTimeNanos(); // mSystemViews.get(SYSTEM_CPU).get(2).setText(String.format("%d%%", cpuNano)); } catch (Exception ex) { m_log.e(ex.getMessage()); } try { // ----- Network WiFi----- WifiManager wifiMgr = (WifiManager) getContext().getApplicationContext() .getSystemService(Context.WIFI_SERVICE); if (wifiMgr != null && wifiMgr.isWifiEnabled() && wifiMgr.getDhcpInfo() != null) { DhcpInfo dhcpInfo = wifiMgr.getDhcpInfo(); mNetworkViews.get(NETWORK_WIFI_IP).get(1).setText(Formatter.formatIpAddress(dhcpInfo.ipAddress)); WifiInfo wifiInfo = wifiMgr.getConnectionInfo(); mNetworkViews.get(NETWORK_WIFI_SPEED).get(1).setText(String.valueOf(wifiInfo.getLinkSpeed())); int numberOfLevels = 10; int level = WifiManager.calculateSignalLevel(wifiInfo.getRssi(), numberOfLevels + 1); mNetworkViews.get(NETWORK_WIFI_SIGNAL).get(1) .setText(String.format("%%%d", 100 * level / numberOfLevels)); } } catch (Exception ex) { m_log.e(ex.getMessage()); } try { // ----- Network Traffic----- // int uid = android.os.Process.myUid(); mNetworkViews.get(NETWORK_RCV_BYTES).get(1).setText(String.format("%d", consoleState.netRxBytes)); mNetworkViews.get(NETWORK_RCV_PACK).get(1).setText(String.format("%d", consoleState.netRxPacks)); mNetworkViews.get(NETWORK_SND_BYTES).get(1).setText(String.format("%d", consoleState.netTxBytes)); mNetworkViews.get(NETWORK_SND_PACK).get(1).setText(String.format("%d", consoleState.netTxPacks)); mNetworkViews.get(NETWORK_RCV_BYTES).get(2) .setText(String.format("%d", TrafficStats.getTotalRxBytes())); mNetworkViews.get(NETWORK_RCV_PACK).get(2) .setText(String.format("%d", TrafficStats.getTotalRxPackets())); mNetworkViews.get(NETWORK_SND_BYTES).get(2) .setText(String.format("%d", TrafficStats.getTotalTxBytes())); mNetworkViews.get(NETWORK_SND_PACK).get(2) .setText(String.format("%d", TrafficStats.getTotalRxPackets())); mNetworkViews.get(NETWORK_RCV_BYTES).get(3) .setText(String.format("%d", TrafficStats.getTotalRxBytes() - consoleState.netRxBytes)); mNetworkViews.get(NETWORK_RCV_PACK).get(3) .setText(String.format("%d", TrafficStats.getTotalRxPackets() - consoleState.netRxPacks)); mNetworkViews.get(NETWORK_SND_BYTES).get(3) .setText(String.format("%d", TrafficStats.getTotalTxBytes() - consoleState.netTxBytes)); mNetworkViews.get(NETWORK_SND_PACK).get(3) .setText(String.format("%d", TrafficStats.getTotalRxPackets() - consoleState.netTxPacks)); } catch (Exception ex) { m_log.e(ex.getMessage()); } // ----- Memory ----- try { MemoryInfo mi = new MemoryInfo(); ActivityManager activityManager = (ActivityManager) getActivity() .getSystemService(Context.ACTIVITY_SERVICE); activityManager.getMemoryInfo(mi); long heapUsing = Debug.getNativeHeapSize(); Date now = new Date(); long deltaMsec = now.getTime() - consoleState.lastFreeze.getTime(); List<TextView> timeViews = mMemoryViews.get(MEMORY_TIME); timeViews.get(1).setText(TIMEFORMAT.format(consoleState.lastFreeze)); timeViews.get(2).setText(TIMEFORMAT.format(now)); timeViews.get(3).setText( DateUtils.getRelativeTimeSpanString(consoleState.lastFreeze.getTime(), now.getTime(), 0)); // timeViews.get(3).setText( String.valueOf(deltaMsec)); List<TextView> usingViews = mMemoryViews.get(MEMORY_USING); usingViews.get(1).setText(String.format("%d", consoleState.usingMemory)); usingViews.get(2).setText(String.format("%d", heapUsing)); usingViews.get(3).setText(String.format("%d", heapUsing - consoleState.usingMemory)); List<TextView> freeViews = mMemoryViews.get(MEMORY_FREE); freeViews.get(1).setText(String.format("%d", consoleState.freeMemory)); freeViews.get(2).setText(String.format("%d", mi.availMem)); freeViews.get(3).setText(String.format("%d", mi.availMem - consoleState.freeMemory)); List<TextView> totalViews = mMemoryViews.get(MEMORY_TOTAL); if (Build.VERSION.SDK_INT >= 16) { totalViews.get(1).setText(String.format("%d", consoleState.totalMemory)); totalViews.get(2).setText(String.format("%d", mi.totalMem)); totalViews.get(3).setText(String.format("%d", mi.totalMem - consoleState.totalMemory)); } else { totalViews.get(0).setVisibility(View.GONE); totalViews.get(1).setVisibility(View.GONE); totalViews.get(2).setVisibility(View.GONE); } } catch (Exception ex) { m_log.e(ex.getMessage()); } }
From source file:com.mobilyzer.util.PhoneUtils.java
public int getWifiRSSI() { WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); if (wifiInfo != null) { return wifiInfo.getRssi(); }/*from ww w . ja v a 2s. c o m*/ return -1; }
From source file:com.example.aaron.test.MyGLSurfaceView.java
public void tick() { antispam = antispam + 1;//from w ww . j av a 2 s . c om count = 0; WifiInfo wifiInfo = getWifi(context1); NetworkInfo.DetailedState state = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState()); mRenderer.textListSINFO.get(1).setText("Strength: " + wifiInfo.getRssi()); if (state == NetworkInfo.DetailedState.CONNECTED || state == NetworkInfo.DetailedState.OBTAINING_IPADDR) { mRenderer.textListSINFO.get(2).setText("Network: " + wifiInfo.getSSID()); } if (mRenderer.voronoiDeploymentToggle.active == true) { mRenderer.textListSINFO.get(3).setText("Deployment: Multi-agent Voronoi"); } else if (pFlag == 1) { mRenderer.textListSINFO.get(3).setText("Go to Goal"); } for (int i = 0; i < 50; i++) { if (tList[i].getState() == 1) { count++; if (count > 1) { mRenderer.textListARINFO.get(0).setText("Multiple Robots Selected"); mRenderer.textListARINFO.get(2).setText(" X:"); mRenderer.textListARINFO.get(3).setText(" Y:"); mRenderer.textListARINFO.get(4).setText(" Z:"); } else { mRenderer.textListARINFO.get(0).setText(tList[i].getIdentification()); mRenderer.textListARINFO.get(2).setText(" X:" + truncateDecimal(tList[i].getX(), 3)); mRenderer.textListARINFO.get(3).setText(" Y:" + truncateDecimal(tList[i].getY(), 3)); mRenderer.textListARINFO.get(4).setText(" Z:" + truncateDecimal(tList[i].getZ(), 3)); } } } if (count == 0) { mRenderer.textListARINFO.get(0).setText("No Robots Selected"); mRenderer.textListARINFO.get(2).setText(" X:"); mRenderer.textListARINFO.get(3).setText(" Y:"); mRenderer.textListARINFO.get(4).setText(" Z:"); } mRenderer.getUniqueVertices(); mRenderer.toMapPoints(); mRenderer.calculateAdjacencyMatrix(); mRenderer.calculateAdjacencyMatrixGoal(); if (!mRenderer.obstacleMapPoints.isEmpty()) { mRenderer.Dijkstra(); } }