Example usage for android.bluetooth BluetoothAdapter ACTION_STATE_CHANGED

List of usage examples for android.bluetooth BluetoothAdapter ACTION_STATE_CHANGED

Introduction

In this page you can find the example usage for android.bluetooth BluetoothAdapter ACTION_STATE_CHANGED.

Prototype

String ACTION_STATE_CHANGED

To view the source code for android.bluetooth BluetoothAdapter ACTION_STATE_CHANGED.

Click Source Link

Document

Broadcast Action: The state of the local Bluetooth adapter has been changed.

Usage

From source file:com.google.android.gcm.demo.app.BluetoothHDPActivity.java

private IntentFilter initIntentFilter() {
    IntentFilter filter = new IntentFilter();
    filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
    return filter;
}

From source file:org.chromium.ChromeBluetooth.java

private void registerAdapterStateReceiver() {
    IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    filter.addAction(BluetoothDevice.ACTION_FOUND);
    webView.getContext().registerReceiver(adapterStateReceiver, filter);
}

From source file:com.cypress.cysmart.HomePageActivity.java

@Override
protected void onResume() {
    mApplicationInBackground = false;//from  w  w  w .  j ava  2  s.  com
    BLUETOOTH_STATUS_FLAG = true;
    final IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
    intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
    intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
    registerReceiver(mBondStateReceiver, intentFilter);
    super.onResume();
}

From source file:org.bart452.runningshoesensor.MainActivity.java

@Override
protected void onResume() {
    registerReceiver(mBleReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
    super.onResume();
}

From source file:mobisocial.musubi.ui.NearbyActivity.java

private void findBluetooth() {
    if (!BluetoothAdapter.getDefaultAdapter().isEnabled()) {
        Intent bt = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(bt, RESULT_BT_ENABLE);
        return;//from ww  w . j  a  va2s  .  c o m
    }

    // Create a BroadcastReceiver for ACTION_FOUND
    final IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);

    final BroadcastReceiver receiver = new BroadcastReceiver() {
        public void onReceive(final Context context, final Intent intent) {
            String action = intent.getAction();
            // When discovery finds a device
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                new Thread() {
                    public void run() {
                        BluetoothBeacon.OnDiscovered discovered = new BluetoothBeacon.OnDiscovered() {
                            @Override
                            public void onDiscovered(final byte[] data) {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        try {
                                            JSONObject obj = new JSONObject(new String(data));
                                            BJDNotImplementedException.except("bluetooth not implemented");
                                            //                                                mNearbyList.add(new NearbyFeed(NearbyActivity.this,
                                            //                                                        obj.getString("name"), Uri.parse(obj
                                            //                                                                .getString("dynuri"))));
                                            mAdapter.notifyDataSetChanged();
                                        } catch (JSONException e) {
                                            Log.e(TAG, "Error getting group info over bluetooth", e);
                                        }
                                    }
                                });
                            }
                        };
                        // Get the BluetoothDevice object from the Intent
                        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                        BluetoothBeacon.discover(NearbyActivity.this, device, discovered);
                    };
                }.start();
            }
            if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
                unregisterReceiver(this);
            }
        }
    };

    registerReceiver(receiver, filter); // Don't forget to unregister during
                                        // onDestroy
    BluetoothAdapter.getDefaultAdapter().startDiscovery();
    Toast.makeText(this, "Scanning Bluetooth...", 500).show();
}

From source file:com.guangyao.bluetoothtest.service.BluetoothLeService.java

@Override
public void onCreate() {
    super.onCreate();
    sendDataToBleReceiver = new SendDataToBleReceiver();
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(BleConstans.ACTION_SEND_DATA_TO_BLE);
    LocalBroadcastManager.getInstance(this).registerReceiver(sendDataToBleReceiver, intentFilter);

    //??/*  w  ww.jav a  2  s.c o m*/
    IntentFilter filter = new IntentFilter();
    filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
    this.registerReceiver(bluetoothStatusChangeReceiver, filter);

}

From source file:com.android.settings.Settings.java

@Override
public void onResume() {
    super.onResume();

    mDevelopmentPreferencesListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
        @Override/*  w  w  w . j a  v a  2 s . c  o  m*/
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
            invalidateHeaders();
        }
    };
    mDevelopmentPreferences.registerOnSharedPreferenceChangeListener(mDevelopmentPreferencesListener);
    // SPRD:ADD register receiver.
    ListAdapter listAdapter = getListAdapter();
    if (listAdapter instanceof HeaderAdapter) {
        // add for tab style
        ((HeaderAdapter) listAdapter).flushViewCache();
        // add for tab style
        ((HeaderAdapter) listAdapter).resume();
    }
    // SPRD:ADD update Header
    invalidateHeaders();

    registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

    //revo lyq 2014 for advan settings
    if (is_advan_settings) {
        IntentFilter mIntentFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
        mIntentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        registerReceiver(mReceiver, mIntentFilter);
    }

}

From source file:com.jordanfitzgibbon.rfduinohrm.RFduinoService.java

public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i(TAG, "onStartCommand()");

    if (intent.getAction().equals("RFduinoService_StartForeground")) {
        //TODO: use proper notification ID here!
        startForeground(101, buildServiceNotification().build());
    } else if (intent.getAction().equals("ACTION_DISCONNECT")) {
        Log.i(TAG, "disconnect clicked");
        if (dataBundle.state_ == 4) {
            disconnect();/* w w  w . j a va2  s .co m*/
            close();
            //TODO: States need to be handled generally
            dataBundle.state_ = 2; // Disconnected state
            NotificationCompat.Builder mBuilder = buildServiceNotification();
            mBuilder.setContentText("RFDuino disconnected");
            //TODO: use proper notification ID here!
            mNotificationManager.notify(101, mBuilder.build());
        }
    } else if (intent.getAction().equals("ACTION_CONNECT")) {
        Log.i(TAG, "connect clicked");
        if (dataBundle.state_ == 2) {
            connect(dataBundle.device.getAddress());
            dataBundle.state_ = 4;
            //TODO: use proper notification ID here!
            mNotificationManager.notify(101, buildServiceNotification().build());
        }
    } else if (intent.getAction().equals("RFduinoService_Stop")) {
        Log.i(TAG, "Background service stop received");
        if (dataBundle != null) {
            if (dataBundle.state_ == 4) {
                disconnect();
            }
        }
        // Saving to sharedPreferences that the service is not running in foreground anymore
        //            SharedPreferences sharedPref = getApplicationContext().getSharedPreferences(
        //                    getString(R.string.preference_file_key), Context.MODE_PRIVATE);
        //            SharedPreferences.Editor editor = sharedPref.edit();
        //            editor.putBoolean("foregroundServiceRunning", false);
        //            editor.commit();

        stopForeground(true);
        stopSelf();
    } else if (intent.getAction().equals("RFduinoService_StopForeground")) {
        Log.i(TAG, "service stop foreground received");
        stopForeground(true);
    }

    registerReceiver(bluetoothStateReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
    receiverRegistered = true;

    return START_STICKY;
}

From source file:org.deviceconnect.android.deviceplugin.sphero.SpheroDeviceService.java

/**
 * Bluetooth?./*  ww w .  j a v a 2 s.  c  om*/
 */
private void registerBluetoothFilter() {
    IntentFilter filter = new IntentFilter();
    filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
    filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
    registerReceiver(mSensorReceiver, filter, null, mHandler);
}

From source file:com.example.bluetooth.RFduinoService.java

public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i(TAG, "onStartCommand()");

    if (intent.getAction().equals("RFduinoService_StartForeground")) {
        //TODO: use proper notification ID here!
        startForeground(101, buildServiceNotification().build());
    } else if (intent.getAction().equals("ACTION_DISCONNECT")) {
        Log.i(TAG, "disconnect clicked");
        if (dataBundle.state_ == 4) {
            disconnect();/*w  ww  . j ava 2  s .  com*/
            close();
            //TODO: States need to be handled generally
            dataBundle.state_ = 2; // Disconnected state
            NotificationCompat.Builder mBuilder = buildServiceNotification();
            mBuilder.setContentText("RFDuino disconnected");
            //TODO: use proper notification ID here!
            mNotificationManager.notify(101, mBuilder.build());
        }
    } else if (intent.getAction().equals("ACTION_CONNECT")) {
        Log.i(TAG, "connect clicked");
        if (dataBundle.state_ == 2) {
            connect(dataBundle.device.getAddress());
            dataBundle.state_ = 4;
            //TODO: use proper notification ID here!
            mNotificationManager.notify(101, buildServiceNotification().build());
        }
    } else if (intent.getAction().equals("RFduinoService_Stop")) {
        Log.i(TAG, "Background service stop received");
        if (dataBundle != null) {
            if (dataBundle.state_ == 4) {
                disconnect();
            }
        }
        // Saving to sharedPreferences that the service is not running in foreground anymore
        SharedPreferences sharedPref = getApplicationContext()
                .getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putBoolean("foregroundServiceRunning", false);
        editor.commit();

        stopForeground(true);
        stopSelf();
    } else if (intent.getAction().equals("RFduinoService_StopForeground")) {
        Log.i(TAG, "service stop foreground received");
        stopForeground(true);
    }

    registerReceiver(bluetoothStateReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
    receiverRegistered = true;

    return START_STICKY;
}