Example usage for android.bluetooth BluetoothDevice ACTION_BOND_STATE_CHANGED

List of usage examples for android.bluetooth BluetoothDevice ACTION_BOND_STATE_CHANGED

Introduction

In this page you can find the example usage for android.bluetooth BluetoothDevice ACTION_BOND_STATE_CHANGED.

Prototype

String ACTION_BOND_STATE_CHANGED

To view the source code for android.bluetooth BluetoothDevice ACTION_BOND_STATE_CHANGED.

Click Source Link

Document

Broadcast Action: Indicates a change in the bond state of a remote device.

Usage

From source file:Main.java

/**
 * register bluetooth receiver// www  .  ja  v  a2s .  c  om
 *
 * @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:com.bluetooth.mwoolley.microbitbledemo.ui.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {

    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    setButtonText();/*  ww w  . j a  v a2s  .  c  o  m*/
    getSupportActionBar().setTitle(R.string.screen_title_main);
    showMsg(Utility.htmlColorGreen("Ready"));

    Settings.getInstance().restore(this);

    ble_device_list_adapter = new ListAdapter();

    ListView listView = (ListView) this.findViewById(R.id.deviceList);
    listView.setAdapter(ble_device_list_adapter);

    registerReceiver(broadcastReceiver, new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED));

    ble_scanner = BleScannerFactory.getBleScanner(this.getApplicationContext());
    ble_scanner.setDevice_name_start(DEVICE_NAME_START);
    ble_scanner.setSelect_bonded_devices_only(true);

    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            if (ble_scanning) {
                setScanState(false);
                ble_scanner.stopScanning();
            }

            BluetoothDevice device = ble_device_list_adapter.getDevice(position);
            if (device.getBondState() == BluetoothDevice.BOND_NONE
                    && Settings.getInstance().isFilter_unpaired_devices()) {
                device.createBond();
                showMsg(Utility.htmlColorRed("Selected micro:bit must be paired - pairing now"));
                return;
            }
            try {
                MainActivity.this.unregisterReceiver(broadcastReceiver);
            } catch (Exception e) {
                // ignore!
            }
            if (toast != null) {
                toast.cancel();
            }
            MicroBit microbit = MicroBit.getInstance();
            microbit.setBluetooth_device(device);
            Intent intent = new Intent(MainActivity.this, MenuActivity.class);
            intent.putExtra(MenuActivity.EXTRA_NAME, device.getName());
            intent.putExtra(MenuActivity.EXTRA_ID, device.getAddress());
            startActivity(intent);

        }
    });
}

From source file:com.wolkabout.hexiwear.service.BluetoothService.java

@Receiver(actions = BluetoothDevice.ACTION_BOND_STATE_CHANGED)
void onBondStateChanged(Intent intent) {
    final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    final int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1);
    final int previousBondState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, -1);

    Log.d(TAG, "Bond state changed for: " + device.getAddress() + " new state: " + bondState + " previous: "
            + previousBondState);/*  w w w  .ja  va 2  s .  com*/

    if (bondState == BluetoothDevice.BOND_BONDED) {
        Log.i(TAG, "Bonded");
        createGATT(device);
    } else if (bondState == BluetoothDevice.BOND_NONE) {
        device.createBond();
    }
}

From source file:com.abs.telecam.gui.ControllerViewer.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mPageNumber = getArguments().getInt(ARG_PAGE);
    controllerBindings = new ControllerBindings(getActivity());
    galleryBindings = new GalleryBindings(getActivity());
    dialogHelper = new DialogHelper(getActivity());
    TeleCam.newDevicesArrayAdapter = new DeviceBluetoothAdapter(getActivity(),
            android.R.layout.select_dialog_singlechoice);
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    getActivity().registerReceiver(mReceiver, filter);
}

From source file:com.wolkabout.hexiwear.activity.MainActivity.java

@Receiver(actions = BluetoothDevice.ACTION_BOND_STATE_CHANGED)
void onBondStateChanged(Intent intent) {
    final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    final int previousBondState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, -1);
    final int newBondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1);

    Log.d(TAG, device.getName() + "(" + device.getAddress() + ") changed state: " + previousBondState + " -> "
            + newBondState);/*  w ww . jav a  2  s .co m*/
    adapter.notifyDataSetChanged();

    if (newBondState == BluetoothDevice.BOND_BONDED) {
        onBonded(device);
    } else if (previousBondState == BluetoothDevice.BOND_BONDING && newBondState == BluetoothDevice.BOND_NONE) {
        device.createBond();
    }
}

From source file:nodomain.freeyourgadget.gadgetbridge.devices.pebble.PebblePairingActivity.java

private void startPairing(GBDevice gbDevice) {
    isPairing = true;//from ww  w. j  a  va  2  s .c o  m
    message.setText(getString(R.string.pairing, macAddress));

    IntentFilter filter = new IntentFilter(GBDevice.ACTION_DEVICE_CHANGED);
    LocalBroadcastManager.getInstance(this).registerReceiver(mPairingReceiver, filter);
    filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
    registerReceiver(mBondingReceiver, filter);

    performPair(gbDevice);
}

From source file:com.t2.compassionMeditation.DeviceManagerActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mInstance = this;
    sharedPref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

    //this.sendBroadcast(new Intent(BioFeedbackService.ACTION_SERVICE_START));

    this.setContentView(R.layout.device_manager_activity_layout);

    this.findViewById(R.id.bluetoothSettingsButton).setOnClickListener(this);

    Resources resources = this.getResources();
    AssetManager assetManager = resources.getAssets();
    try {//from  w w  w.  j av  a2  s.co  m
        mSpineManager = SPINEFactory.createSPINEManager("SPINETestApp.properties", resources);
    } catch (InstantiationException e) {
        Log.e(TAG, this.getClass().getSimpleName() + " Exception creating SPINE manager: " + e.toString());
        e.printStackTrace();
    }
    // ... then we need to register a SPINEListener implementation to the SPINE manager instance
    // to receive sensor node data from the Spine server
    // (I register myself since I'm a SPINEListener implementation!)
    mSpineManager.addListener(this);

    // Create a broadcast receiver. Note that this is used ONLY for command messages from the service
    // All data from the service goes through the mail SPINE mechanism (received(Data data)).
    // See public void received(Data data)
    this.mCommandReceiver = new SpineReceiver(this);
    // Set up filter intents so we can receive broadcasts
    IntentFilter filter = new IntentFilter();
    filter.addAction("com.t2.biofeedback.service.status.BROADCAST");
    filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
    filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
    filter.addAction(BioFeedbackService.ACTION_STATUS_BROADCAST);

    this.registerReceiver(this.mCommandReceiver, filter);

    // Tell the bluetooth service to send us a list of bluetooth devices and system status
    // Response comes in public void onStatusReceived(BioFeedbackStatus bfs) STATUS_PAIRED_DEVICES      
    mSpineManager.pollBluetoothDevices();

    //      this.deviceManager = DeviceManager.getInstance(this.getBaseContext(), null);
    this.deviceList = (ListView) this.findViewById(R.id.list);

    this.deviceListAdapter = new ManagerItemAdapter(this, R.layout.manager_item);
    this.deviceList.setAdapter(this.deviceListAdapter);

    this.bluetoothDisabledDialog = new AlertDialog.Builder(this).setMessage("Bluetooth is not enabled.")
            .setOnCancelListener(new OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {
                    finish();
                }
            }).setPositiveButton("Setup Bluetooth", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    startBluetoothSettings();
                }
            }).create();

    try {
        PackageManager packageManager = this.getPackageManager();
        PackageInfo info = packageManager.getPackageInfo(this.getPackageName(), 0);
        mVersionName = info.versionName;
        Log.i(TAG, this.getClass().getSimpleName() + " Spine server Test Application Version " + mVersionName);
    } catch (NameNotFoundException e) {
        Log.e(TAG, this.getClass().getSimpleName() + e.toString());
    }
}

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

@Override
protected void onResume() {
    mApplicationInBackground = false;// w  w w  .j a v a2s.c  om
    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:com.cypress.cysmart.RDKEmulatorView.RemoteControlEmulatorFragment.java

@Override
public void onResume() {
    super.onResume();
    HANDLER_FLAG = true;/*from  w  w w  .  j av  a  2s.c om*/
    Utils.setUpActionBar(getActivity(), getResources().getString(R.string.rdk_emulator_view));
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
    getActivity().registerReceiver(mGattUpdateReceiver, intentFilter);
    initializeBondingIFnotBonded();
}

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

/**
 * Bluetooth?./*from   w w w  .  jav a 2  s .  co  m*/
 */
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);
}