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:Main.java

/**
 * register bluetooth receiver//from w  ww  .j ava2 s  . c o m
 *
 * @param receiver bluetooth broadcast receiver
 * @param activity activity
 */
public static void registerBluetoothReceiver(BroadcastReceiver receiver, Activity activity) {
    if (null == receiver || null == activity) {
        return;
    }
    IntentFilter intentFilter = new IntentFilter();
    //start discovery
    intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    //finish discovery
    intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    //bluetooth status change
    intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
    //found device
    intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
    //bond status change
    intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
    //pairing device
    intentFilter.addAction("android.bluetooth.device.action.PAIRING_REQUEST");
    activity.registerReceiver(receiver, intentFilter);
}

From source file:org.libreoffice.impressremote.communication.BluetoothServersFinder.java

@Override
public void startSearch() {
    if (btAdapter == null) {
        return;//w ww . j  a v  a  2 s .c  om
    }
    IntentFilter aBluetoothActionsFilter = new IntentFilter();
    aBluetoothActionsFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
    aBluetoothActionsFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    aBluetoothActionsFilter.addAction(BluetoothDevice.ACTION_FOUND);

    mContext.registerReceiver(this, aBluetoothActionsFilter);
    Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();
    if (pairedDevices.size() > 0) {
        for (BluetoothDevice device : pairedDevices) {
            addServer(device);
        }
    } else {
        if (btAdapter.isDiscovering()) {
            return;
        }
        btAdapter.startDiscovery();
    }
}

From source file:org.libreoffice.impressremote.communication.BluetoothServersFinder.java

@Override
public void onReceive(Context aContext, Intent aIntent) {
    if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(aIntent.getAction())) {
        switch (aIntent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0)) {
        case BluetoothAdapter.STATE_ON:
            startSearch();/* w  w w .j  ava2 s.  c  o m*/
            return;

        default:
            return;
        }
    }

    if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(aIntent.getAction())) {
        LocalBroadcastManager.getInstance(mContext)
                .sendBroadcast(new Intent(Intents.Actions.BT_DISCOVERY_CHANGED));
        return;
    }

    if (BluetoothDevice.ACTION_FOUND.equals(aIntent.getAction())) {
        BluetoothDevice aBluetoothDevice = aIntent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

        addServer(aBluetoothDevice);
    }
}

From source file:uk.ac.horizon.ubihelper.service.channel.BluetoothDiscoveryChannel.java

/**
 * @param handler/*from  w  ww .j av  a  2s  .c  o m*/
 * @param name
 */
public BluetoothDiscoveryChannel(Service service, Handler handler, String name) {
    super(handler, name);
    bluetooth = BluetoothAdapter.getDefaultAdapter();
    this.service = service;
    if (bluetooth == null)
        Log.w(TAG, "No BluetoothAdapter");
    if (!bluetooth.isEnabled()) {
        Intent i = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        service.sendBroadcast(i);
    }
    IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
    filter.addAction(BluetoothDevice.ACTION_FOUND);
    filter.addAction(BluetoothDevice.ACTION_NAME_CHANGED);
    this.service.registerReceiver(receiver, filter);
}

From source file:com.evothings.BLE.java

@Override
public void initialize(final CordovaInterface cordova, CordovaWebView webView) {
    super.initialize(cordova, webView);
    mContext = webView.getContext();// w  ww . j a v  a  2 s.  co  m

    if (!mRegisteredReceiver) {
        mContext.registerReceiver(new BluetoothStateReceiver(),
                new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
        mRegisteredReceiver = true;
    }
}

From source file:pl.mrwojtek.sensrec.app.SettingsActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings_activity);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);// w w w . ja v a2 s  . c o  m

    IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
    registerReceiver(bluetoothStateReceiver, filter);
}

From source file:org.bcsphere.bluetooth.BCBluetooth.java

@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
    super.initialize(cordova, webView);
    myContext = cordova.getActivity().getApplicationContext();
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
    intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
    myContext.registerReceiver(receiver, intentFilter);
    sp = myContext.getSharedPreferences("VERSION_OF_API", 1);
    BluetoothDetection.detectionBluetoothAPI(myContext);
    try {/*ww  w .j  av a 2s .  co  m*/
        if ((versionOfAPI = sp.getString("API", "no_google")).equals("google")) {
            bluetoothAPI = (IBluetooth) Class.forName("org.bcsphere.bluetooth.BluetoothG43plus").newInstance();
        } else if ((versionOfAPI = sp.getString("API", "no_samsung")).equals("samsung")) {
            bluetoothAPI = (IBluetooth) Class.forName("org.bcsphere.bluetooth.BluetoothSam42").newInstance();
        }
        /*else if ((versionOfAPI = sp.getString("API", "no_htc"))
              .equals("htc")) {
           bluetoothAPI = (IBluetooth) Class.forName(
          "org.bcsphere.bluetooth.BluetoothHTC41").newInstance();
        }*/
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.amazonaws.devicefarm.android.referenceapp.Fragments.FixturesFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, final Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fixtures_layout, container, false);
    ButterKnife.inject(this, view);
    buildGoogleApiClient();//  w  ww.  j a va2s  . co m

    //Registering events to detect radio changes
    final BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
                updateBluetoothStatusDisplay();
            } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
                updateWifiStatusDisplay();
            } else if (action.equals(LocationManager.PROVIDERS_CHANGED_ACTION)) {
                updateGPSStatusDisplay();
            } else if (action.equals(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED)) {
                updateNFCStatusDisplay();
            }
        }
    };
    IntentFilter filter = new IntentFilter();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        filter.addAction(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED);
    }

    filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
    filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
    filter.addAction(LocationManager.PROVIDERS_CHANGED_ACTION);

    updateWifiStatusDisplay();
    updateBluetoothStatusDisplay();
    updateGPSStatusDisplay();
    updateNFCStatusDisplay();
    getActivity().registerReceiver(receiver, filter);
    return view;
}

From source file:com.dipesan.miniatm.miniatm.services.BluetoothConnexionManager.java

@Override
public void onReceive(Context context, Intent intent) {
    switch (intent.getAction()) {
    case BluetoothAdapter.ACTION_STATE_CHANGED:
        if (BluetoothAdapter.getDefaultAdapter().isEnabled()) {
            connect();/*w ww  .  j  a v a  2  s .  c om*/
        }
        break;
    }
}

From source file:de.schildbach.wallet.offline.AcceptBluetoothService.java

@Override
public void onCreate() {
    serviceCreatedAt = System.currentTimeMillis();
    log.debug(".onCreate()");

    super.onCreate();
    this.application = (WalletApplication) getApplication();
    final BluetoothAdapter bluetoothAdapter = checkNotNull(BluetoothAdapter.getDefaultAdapter());
    final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);

    wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
    wakeLock.acquire();// w w  w  . j  ava  2s  . c o m

    registerReceiver(bluetoothStateChangeReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));

    try {
        classicThread = new AcceptBluetoothThread.ClassicBluetoothThread(bluetoothAdapter) {
            @Override
            public boolean handleTx(final Transaction tx) {
                return AcceptBluetoothService.this.handleTx(tx);
            }
        };
        paymentProtocolThread = new AcceptBluetoothThread.PaymentProtocolThread(bluetoothAdapter) {
            @Override
            public boolean handleTx(final Transaction tx) {
                return AcceptBluetoothService.this.handleTx(tx);
            }
        };
    } catch (final IOException x) {
        new Toast(this).longToast(R.string.error_bluetooth, x.getMessage());
        log.warn("problem with listening, stopping service", x);
        CrashReporter.saveBackgroundTrace(x, application.packageInfo());
        stopSelf();
    }

    wallet = new WalletLiveData(application);
    wallet.observe(this, new Observer<Wallet>() {
        @Override
        public void onChanged(final Wallet wallet) {
            classicThread.start();
            paymentProtocolThread.start();
        }
    });
}