Example usage for android.content Context WIFI_SERVICE

List of usage examples for android.content Context WIFI_SERVICE

Introduction

In this page you can find the example usage for android.content Context WIFI_SERVICE.

Prototype

String WIFI_SERVICE

To view the source code for android.content Context WIFI_SERVICE.

Click Source Link

Document

Use with #getSystemService(String) to retrieve a android.net.wifi.WifiManager for handling management of Wi-Fi access.

Usage

From source file:net.micode.fileexplorer.ServerControlActivity.java

/**
 * This will be called by the static UiUpdater whenever the service has
 * changed state in a way that requires us to update our UI. We can't use
 * any myLog.l() calls in this function, because that will trigger an
 * endless loop of UI updates./*  w ww  . j av a 2  s .  co m*/
 */
public void updateUi() {
    myLog.l(Log.DEBUG, "Updating UI", true);

    WifiManager wifiMgr = (WifiManager) mActivity.getSystemService(Context.WIFI_SERVICE);
    int wifiState = wifiMgr.getWifiState();
    WifiInfo info = wifiMgr.getConnectionInfo();
    String wifiId = info != null ? info.getSSID() : null;
    boolean isWifiReady = FTPServerService.isWifiEnabled();

    setText(R.id.wifi_state, isWifiReady ? wifiId : getString(R.string.no_wifi_hint));
    ImageView wifiImg = (ImageView) mRootView.findViewById(R.id.wifi_state_image);
    wifiImg.setImageResource(isWifiReady ? R.drawable.wifi_state4 : R.drawable.wifi_state0);

    boolean running = FTPServerService.isRunning();
    if (running) {
        myLog.l(Log.DEBUG, "updateUi: server is running", true);
        // Put correct text in start/stop button
        // Fill in wifi status and address
        InetAddress address = FTPServerService.getWifiIp();
        if (address != null) {
            String port = ":" + FTPServerService.getPort();
            ipText.setText(
                    "ftp://" + address.getHostAddress() + (FTPServerService.getPort() == 21 ? "" : port));

        } else {
            // could not get IP address, stop the service
            Context context = mActivity.getApplicationContext();
            Intent intent = new Intent(context, FTPServerService.class);
            context.stopService(intent);
            ipText.setText("");
        }
    }

    startStopButton.setEnabled(isWifiReady);
    TextView startStopButtonText = (TextView) mRootView.findViewById(R.id.start_stop_button_text);
    if (isWifiReady) {
        startStopButtonText.setText(running ? R.string.stop_server : R.string.start_server);
        startStopButtonText.setCompoundDrawablesWithIntrinsicBounds(
                running ? R.drawable.disconnect : R.drawable.connect, 0, 0, 0);
        startStopButtonText.setTextColor(running ? getResources().getColor(R.color.remote_disconnect_text)
                : getResources().getColor(R.color.remote_connect_text));
    } else {
        if (FTPServerService.isRunning()) {
            Context context = mActivity.getApplicationContext();
            Intent intent = new Intent(context, FTPServerService.class);
            context.stopService(intent);
        }

        startStopButtonText.setText(R.string.no_wifi);
        startStopButtonText.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        startStopButtonText.setTextColor(Color.GRAY);
    }

    ipText.setVisibility(running ? View.VISIBLE : View.INVISIBLE);
    instructionText.setVisibility(running ? View.VISIBLE : View.GONE);
    instructionTextPre.setVisibility(running ? View.GONE : View.VISIBLE);
}

From source file:com.vinexs.tool.NetworkManager.java

public static String getSSID(Context context) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo info = wifiManager.getConnectionInfo();
    return info.getBSSID();
}

From source file:com.tvs.signaltracker.STService.java

private void UpdateWiFi() {
    SupplicantState supState;//ww w . ja  v a2s  .c o  m
    WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    supState = wifiInfo.getSupplicantState();
    CommonHandler.WifiConnected = (supState == SupplicantState.COMPLETED);

}

From source file:net.dian1.player.service.PlayerService.java

@Override
public void onCreate() {
    Log.i(Dian1Application.TAG, "Player Service onCreate");

    mPlayerEngine = new PlayerEngineImpl();
    mPlayerEngine.addListener(mLocalEngineListener);

    mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    mAudioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_MUSIC,
            AudioManager.AUDIOFOCUS_GAIN);

    //        mTelephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
    //        mPhoneStateListener = new PhoneStateListener() {
    ///*from  ww  w. ja v  a 2 s.c o m*/
    //            @Override
    //            public void onCallStateChanged(int state, String incomingNumber) {
    //                Log.e(Dian1Application.TAG, "onCallStateChanged");
    //                if (state == TelephonyManager.CALL_STATE_IDLE) {
    //                    // resume playback
    //                } else {
    //                    if (mPlayerEngine != null) {
    //                        mPlayerEngine.pause();
    //                    }
    //                }
    //            }
    //
    //        };
    //        mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);

    mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    mWifiLock = mWifiManager.createWifiLock(Dian1Application.TAG);
    mWifiLock.setReferenceCounted(false);

    Dian1Application.getInstance().setConcretePlayerEngine(mPlayerEngine);
    mRemoteEngineListeners = Dian1Application.getInstance().fetchPlayerEngineListener();
}

From source file:com.example.bluetooth_faster_connection.MainActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (D)//  ww  w.  j  av  a  2s  . com
        Log.e(TAG, "+++ ON CREATE +++");

    // Set up the window layout
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.main);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);

    // Set up the custom title
    mTitle = (TextView) findViewById(R.id.title_left_text);
    mTitle.setText(R.string.app_name);
    mTitle = (TextView) findViewById(R.id.title_right_text);
    mInfo = (TextView) findViewById(R.id.myText);

    Button mButton = (Button) findViewById(R.id.myButton);
    mButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            openOptionsMenu();
        }
    });

    // Get local Bluetooth adapter
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    // If the adapter is null, then Bluetooth is not supported
    if (mBluetoothAdapter == null) {
        Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
        finish();
        return;
    }

    // register for background service broadcast.
    IntentFilter ifilt = new IntentFilter(DeviceDicoverService.DEVICE_CONNECTION_ADDRESS);
    ifilt.addAction(DeviceDicoverService.DEVICE_CONNECTION_INFO);
    registerReceiver(mReceiver, ifilt);

    // just for test purpose, should be deleted if not in test procedure.
    Intent service = new Intent(this, DeviceDicoverService.class);
    startService(service);

    wifii = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    WifiInfo wInfo = wifii.getConnectionInfo();
}

From source file:org.csploit.android.core.System.java

public static void init(Context context) throws Exception {
    mContext = context;/*from   w w  w  . j  a va2 s . co m*/
    try {
        Logger.debug("initializing System...");
        mStoragePath = getSettings().getString("PREF_SAVE_PATH",
                Environment.getExternalStorageDirectory().toString());
        mSessionName = "csploit-session-" + java.lang.System.currentTimeMillis();
        mKnownIssues = new KnownIssues();
        mPlugins = new ArrayList<>();
        mOpenPorts = new SparseIntArray(3);
        mServices = new HashMap<>();
        mPorts = new HashMap<>();

        // if we are here, network initialization didn't throw any error, lock wifi
        WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);

        if (mWifiLock == null)
            mWifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL, "wifiLock");

        if (!mWifiLock.isHeld())
            mWifiLock.acquire();

        // wake lock if enabled
        if (getSettings().getBoolean("PREF_WAKE_LOCK", true)) {
            PowerManager powerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);

            if (mWakeLock == null)
                mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "wakeLock");

            if (!mWakeLock.isHeld())
                mWakeLock.acquire();
        }

        // set ports
        try {
            HTTP_PROXY_PORT = Integer.parseInt(getSettings().getString("PREF_HTTP_PROXY_PORT", "8080"));
            HTTP_SERVER_PORT = Integer.parseInt(getSettings().getString("PREF_HTTP_SERVER_PORT", "8081"));
            HTTPS_REDIR_PORT = Integer.parseInt(getSettings().getString("PREF_HTTPS_REDIRECTOR_PORT", "8082"));
            MSF_RPC_PORT = Integer.parseInt(getSettings().getString("MSF_RPC_PORT", "55553"));
        } catch (NumberFormatException e) {
            HTTP_PROXY_PORT = 8080;
            HTTP_SERVER_PORT = 8081;
            HTTPS_REDIR_PORT = 8082;
            MSF_RPC_PORT = 55553;
        }

        uncaughtReloadNetworkMapping();

        ThreadHelper.getSharedExecutor().execute(new Runnable() {
            @Override
            public void run() {
                preloadServices();
                preloadVendors();
            }
        });
    } catch (Exception e) {
        if (!(e instanceof NoRouteToHostException))
            errorLogging(e);

        throw e;
    }
}

From source file:fr.inria.ucn.collectors.NetworkStateCollector.java

@SuppressLint("NewApi")
@Override// w  ww.ja va 2s . co m
public void onReceive(Context context, Intent intent) {
    long ts = System.currentTimeMillis();
    if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
        context.unregisterReceiver(this);
        try {
            JSONArray neighs = new JSONArray();
            WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            for (ScanResult r : wm.getScanResults()) {
                JSONObject o = new JSONObject();
                o.put("frequency", r.frequency);
                o.put("level", r.level);
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
                    o.put("timestamp", r.timestamp);
                }
                o.put("bssid", r.BSSID);
                o.put("ssid", r.SSID);
                neighs.put(o);
            }

            JSONObject wifi = new JSONObject();
            wifi.put("list", neighs);
            Helpers.sendResultObj(context, "wifi_neigh", ts, wifi);

        } catch (JSONException jex) {
            Log.w(Constants.LOGTAG, "failed to create json object", jex);
        }
        // this will clean the CPU lock too if running on the bg
        Helpers.releaseWifiLock();
    }
}

From source file:org.cocos2dx.lib.CCUtils.java

public static String getMacAddress() {
    String mac = "";
    Context ctx = Cocos2dxActivity.getContext();

    // first, try to get mac from wifi manager
    if (ctx.checkCallingPermission(permission.ACCESS_WIFI_STATE) == PackageManager.PERMISSION_GRANTED) {
        WifiManager wifi = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifi.getConnectionInfo();
        mac = info.getMacAddress();//from   w  w  w . j a  va2s. com
    }

    // if failed, try from network interface api
    if (TextUtils.isEmpty(mac)) {
        if (Build.VERSION.SDK_INT >= 9) {
            try {
                NetworkInterface ne = NetworkInterface
                        .getByInetAddress(InetAddress.getByName(getLocalIpAddress()));
                byte[] b = ne.getHardwareAddress();
                mac = byte2Hex(b);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    // if failed, use fake
    if (TextUtils.isEmpty(mac)) {
        mac = "00:00:00:00:00:00";
    }

    // return
    return mac;
}

From source file:org.basdroid.common.NetworkUtils.java

/**
 * enable wifi device/* w  w  w. j a v a 2s.  c  om*/
 * @param context
 */
public static void enableWifiNetwork(Context context) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    if (wifiManager.isWifiEnabled()) {
        // WIFI ALREADY ENABLED. GRAB THE MAC ADDRESS HERE
        Log.d(TAG, "WIFI ALREADY ENABLED.");
    } else {
        // ENABLE THE WIFI FIRST
        Log.d(TAG, "WIFI DISABLED. ENABLE THE WIFI FIRST");
        wifiManager.setWifiEnabled(true);
    }

    return;
}

From source file:nuclei.media.playback.ExoPlayerPlayback.java

public ExoPlayerPlayback(MediaService service) {
    mService = service;//from  w ww. j  av a  2  s  . c om
    mHandler = new Handler();
    final Context ctx = service.getApplicationContext();
    mAudioManager = (AudioManager) ctx.getSystemService(Context.AUDIO_SERVICE);
    PowerManager powerManager = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
    WifiManager wifiManager = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE);
    // Create the Wifi lock (this does not acquire the lock, this just creates it)
    mWifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL, "nuclei_media_wifi_lock");
    mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "nuclei_media_cpu_lock");
}