Example usage for android.bluetooth BluetoothDevice EXTRA_DEVICE

List of usage examples for android.bluetooth BluetoothDevice EXTRA_DEVICE

Introduction

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

Prototype

String EXTRA_DEVICE

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

Click Source Link

Document

Used as a Parcelable BluetoothDevice extra field in every intent broadcast by this class.

Usage

From source file:com.cardiograph.view.BLEActivity.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {

    case REQUEST_SELECT_DEVICE:
        //When the DeviceListActivity return, with the selected device address
        if (resultCode == Activity.RESULT_OK && data != null) {
            String deviceAddress = data.getStringExtra(BluetoothDevice.EXTRA_DEVICE);
            mDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(deviceAddress);

            Log.d(TAG, "... onActivityResultdevice.address==" + mDevice + "mserviceValue" + mService);
            ((TextView) findViewById(R.id.deviceName)).setText(mDevice.getName() + " - connecting");
            mService.connect(deviceAddress);

        }/*from w  w  w .  j a v  a  2  s.  c o m*/
        break;
    case REQUEST_ENABLE_BT:
        // When the request to enable Bluetooth returns
        if (resultCode == Activity.RESULT_OK) {
            Toast.makeText(this, "Bluetooth has turned on ", Toast.LENGTH_SHORT).show();

        } else {
            // User did not enable Bluetooth or an error occurred
            Log.d(TAG, "BT not enabled");
            Toast.makeText(this, "Problem in BT Turning ON ", Toast.LENGTH_SHORT).show();
            finish();
        }
        break;
    default:
        Log.e(TAG, "wrong request code");
        break;
    }
}

From source file:com.wordpress.bennthomsen.ble_uart_remote.MainActivity.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {

    case REQUEST_SELECT_DEVICE:
        //When the DeviceListActivity return, with the selected device address
        if (resultCode == Activity.RESULT_OK && data != null) {
            String deviceAddress = data.getStringExtra(BluetoothDevice.EXTRA_DEVICE);
            mDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(deviceAddress);

            Log.d(TAG, "... onActivityResultdevice.address==" + mDevice + "mserviceValue" + mService);
            ((TextView) findViewById(R.id.deviceName)).setText(mDevice.getName() + " - connecting");
            mService.connect(deviceAddress);

        }//from ww w  . ja  va  2 s .co  m
        break;
    case REQUEST_ENABLE_BT:
        // When the request to enable Bluetooth returns
        if (resultCode == Activity.RESULT_OK) {
            Toast.makeText(this, "Bluetooth has turned on ", Toast.LENGTH_SHORT).show();

        } else {
            // User did not enable Bluetooth or an error occurred
            Log.d(TAG, "BT not enabled");
            Toast.makeText(this, "Problem in BT Turning ON ", Toast.LENGTH_SHORT).show();
            finish();
        }
        break;
    case MainActivity.REQUEST_SELECT_RECIPE:
        if (resultCode != Activity.RESULT_CANCELED) {
            value = null;
            bakery = (Bakery) data.getSerializableExtra("bakery");
            recipe = (Recipe) data.getSerializableExtra("recipe");

            value = bakery.setProgram(value);

            new AlertDialog.Builder(this).setTitle("Ateno").setMessage(
                    "Antes de continuar, cetifique-se que a bandeja foi inserida com todos os ingredientes.")
                    .setPositiveButton("Continuar", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //send program to the machine
                            mService.writeRXCharacteristic(value);
                        }
                    }).show();

            //register this operation in cloud
        }
        break;
    default:
        Log.e(TAG, "wrong request code");
        break;
    }
}

From source file:com.github.vgoliveira.panificadora.MainActivity.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {

    case REQUEST_SELECT_DEVICE:
        //When the DeviceListActivity return, with the selected device address
        if (resultCode == Activity.RESULT_OK && data != null) {
            String deviceAddress = data.getStringExtra(BluetoothDevice.EXTRA_DEVICE);
            mDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(deviceAddress);

            Log.d(TAG, "... onActivityResultdevice.address==" + mDevice + "mserviceValue" + mService);
            ((TextView) findViewById(R.id.deviceName)).setText(mDevice.getName() + " - connecting");
            mService.connect(deviceAddress);

        }/*from ww  w  .  ja v a  2 s.  co m*/
        break;
    case REQUEST_ENABLE_BT:
        // When the request to enable Bluetooth returns
        if (resultCode == Activity.RESULT_OK) {
            Toast.makeText(this, "Bluetooth has turned on ", Toast.LENGTH_SHORT).show();

        } else {
            // User did not enable Bluetooth or an error occurred
            Log.d(TAG, "BT not enabled");
            Toast.makeText(this, "Problem in BT Turning ON ", Toast.LENGTH_SHORT).show();
            finish();
        }
        break;
    case MainActivity.REQUEST_SELECT_RECIPE:
        if (resultCode != Activity.RESULT_CANCELED) {
            value = null;
            bakery = (Bakery) data.getSerializableExtra("bakery");
            recipe = (Recipe) data.getSerializableExtra("recipe");

            value = bakery.setProgram(value);

            new AlertDialog.Builder(this).setTitle("Ateno").setMessage(
                    "Antes de continuar, cetifique-se que a bandeja foi inserida com todos os ingredientes.")
                    .setPositiveButton("Continuar", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //send program to the machine
                            mService.writeRXCharacteristic(value);
                        }
                    }).show();

            //register this operation in cloud
        }
        break;
    case MainActivity.REQUEST_FACEBOOK_LOGIN:
        Toast.makeText(MainActivity.this, "Return Facebook Login", Toast.LENGTH_SHORT).show();
        String name = data.getStringExtra("name");
        String fbId = data.getStringExtra("id");
        String email = data.getStringExtra("email");
        String gender = data.getStringExtra("gender");
        String ageRange = data.getStringExtra("age_range");
        Toast.makeText(MainActivity.this, "User name: " + name, Toast.LENGTH_SHORT).show();
        Toast.makeText(MainActivity.this, "Facebook id: " + fbId, Toast.LENGTH_SHORT).show();
        Toast.makeText(MainActivity.this, "User email: " + email, Toast.LENGTH_SHORT).show();
        Toast.makeText(MainActivity.this, "Gender: " + gender, Toast.LENGTH_SHORT).show();
        Toast.makeText(MainActivity.this, "Age range: " + ageRange, Toast.LENGTH_SHORT).show();

        break;
    default:
        Log.e(TAG, "wrong request code");
        break;
    }
}

From source file:com.sdingba.su.lanya.MainActivity.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {

    case REQUEST_SELECT_DEVICE:
        //When the DeviceListActivity return, with the selected device address
        if (resultCode == Activity.RESULT_OK && data != null) {
            //???
            String deviceAddress = data.getStringExtra(BluetoothDevice.EXTRA_DEVICE);
            mDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(deviceAddress);

            Log.d(TAG, "... onActivityResultdevice.address==" + mDevice + "mserviceValue" + mService);
            ((TextView) findViewById(R.id.deviceName)).setText(mDevice.getName() + " - connecting");
            mService.connect(deviceAddress);
        }/*from ww  w .ja v a  2s. co  m*/
        break;
    case REQUEST_ENABLE_BT:
        // When the request to enable Bluetooth returns
        if (resultCode == Activity.RESULT_OK) {

            Toast.makeText(this, "Bluetooth has turned on for onActivityResult ", Toast.LENGTH_SHORT).show();

        } else {
            // User did not enable Bluetooth or an error occurred
            Log.d(TAG, "BT not enabled");
            Toast.makeText(this, "Problem in BT Turning ON for onActivityResult", Toast.LENGTH_SHORT).show();
            Log.i(TAG, "finish(); is acticiyty");
            finish();
        }
        break;
    default:
        Log.e(TAG, "wrong request code");
        break;
    }
}

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  av  a 2  s . co 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.nordicsemi.nrfUARTv2.MainActivity.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {

    case REQUEST_SELECT_DEVICE:
        //When the DeviceListActivity return, with the selected device address
        if (resultCode == Activity.RESULT_OK && data != null) {
            String deviceAddress = data.getStringExtra(BluetoothDevice.EXTRA_DEVICE);
            mDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(deviceAddress);

            Log.d(TAG, "... onActivityResultdevice.address==" + mDevice + "mserviceValue" + mService);
            ((TextView) findViewById(R.id.deviceName)).setText(mDevice.getName() + " - connecting");
            mService.connect(deviceAddress);

        }// w w w  .  j  av  a  2  s .  c  om
        break;
    case REQUEST_SELECT_DEVICE2:
        //When the DeviceListActivity return, with the selected device address
        if (resultCode == Activity.RESULT_OK && data != null) {
            String deviceAddress = data.getStringExtra(BluetoothDevice.EXTRA_DEVICE);
            mDevice2 = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(deviceAddress);

            Log.d(TAG, "... onActivityResultdevice.address==" + mDevice2 + "mserviceValue" + mService2);
            ((TextView) findViewById(R.id.deviceName)).setText(mDevice2.getName() + " - connecting");
            mService2.connect(deviceAddress);
            mCollecting = true;

        }
        break;
    case REQUEST_ENABLE_BT:
        // When the request to enable Bluetooth returns
        if (resultCode == Activity.RESULT_OK) {
            Toast.makeText(this, "Bluetooth has turned on ", Toast.LENGTH_SHORT).show();

        } else {
            // User did not enable Bluetooth or an error occurred
            Log.d(TAG, "BT not enabled");
            Toast.makeText(this, "Problem in BT Turning ON ", Toast.LENGTH_SHORT).show();
            finish();
        }
        break;
    default:
        Log.e(TAG, "wrong request code");
        break;
    }
}

From source file:is.hello.buruberi.bluetooth.stacks.android.NativeGattPeripheral.java

@RequiresPermission(Manifest.permission.BLUETOOTH)
private Observable<Intent> createBondReceiver() {
    return Rx//from w  ww  . j  ava  2s.com
            .fromBroadcast(stack.applicationContext,
                    new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED))
            .subscribeOn(stack.getScheduler()).filter(new Func1<Intent, Boolean>() {
                @Override
                public Boolean call(Intent intent) {
                    final BluetoothDevice bondedDevice = intent
                            .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    return (bondedDevice != null
                            && bondedDevice.getAddress().equals(bluetoothDevice.getAddress()));
                }
            });
}

From source file:com.example.lilach.alsweekathon_new.MainActivity.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {

    case REQUEST_SELECT_DEVICE:
        //When the DeviceListActivity return, with the selected device address
        if (resultCode == Activity.RESULT_OK && data != null) {
            String deviceAddress = data.getStringExtra(BluetoothDevice.EXTRA_DEVICE);
            mDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(deviceAddress);

            // Log.d(TAG, "... onActivityResultdevice.address==" + mDevice + "mserviceValue" + mService);
            ((TextView) findViewById(R.id.deviceName)).setText(mDevice.getName() + " - connecting");
            //  mService.connect(deviceAddress);

        }//from  www.  j  av a2s  .c  o m
        break;
    case REQUEST_ENABLE_BT:
        // When the request to enable Bluetooth returns
        if (resultCode == Activity.RESULT_OK) {
            Toast.makeText(this, "Bluetooth has turned on ", Toast.LENGTH_SHORT).show();

        } else {
            // User did not enable Bluetooth or an error occurred
            Log.d(TAG, "BT not enabled");
            Toast.makeText(this, "Problem in BT Turning ON ", Toast.LENGTH_SHORT).show();
            finish();
        }
        break;
    default:
        Log.e(TAG, "wrong request code");
        break;
    }
}

From source file:com.teeptrak.controller.MainActivity.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case REQUEST_SELECT_DEVICE: {
        //When the DeviceListActivity return, with the selected device address
        if (resultCode == Activity.RESULT_OK && data != null) {
            String deviceAddress = data.getStringExtra(BluetoothDevice.EXTRA_DEVICE);
            mBtDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(deviceAddress);

            Log.d(TAG, "... onActivityResultdevice.address==" + mBtDevice + "mserviceValue" + mUartService);

            mBtDeviceName.setText(mBtDevice.getName() + " - Connecting");

            if (mBtDevice.getName().equals("DfuTarg")) {
                if (mFwFilePath != null) {
                    final DfuServiceInitiator DFU = new DfuServiceInitiator(mBtDevice.getAddress())
                            .setDeviceName(mBtDevice.getName()).setKeepBond(false).setZip(null, mFwFilePath);
                    DFU.start(this, DfuService.class);
                } else {
                    printMessage("Selected Device is in DFU mode.", false);
                    printMessage("You must Set FirmWare file first!", false);
                    Toast.makeText(this, "You must Set FirmWare file first!", Toast.LENGTH_SHORT).show();
                }//from   w w  w . j  a v a 2  s  . co m
            } else {
                mUartService.connect(deviceAddress);
            }
        }
        break;
    }

    case REQUEST_ENABLE_BT: {
        // When the request to enable Bluetooth returns
        if (resultCode == Activity.RESULT_OK) {
            Toast.makeText(this, "Bluetooth has turned on ", Toast.LENGTH_SHORT).show();
        } else {
            // User did not enable Bluetooth or an error occurred
            Log.d(TAG, "BT not enabled");
            Toast.makeText(this, "Problem in BT Turning ON ", Toast.LENGTH_SHORT).show();
            finish();
        }
        break;
    }

    case REQUEST_SELECT_FILE: {
        // Clear previous data
        mFwFilePath = null;
        mCfgFilePath = null;
        mTstFilePath = null;
        //mFileUri = null;

        // Read new one
        final Uri uri = data.getData();
        /*
         * The URI returned from application may be in 'file' or 'content' schema.
         * 'File' schema allows us to create a File object and read details from it
         * directly. Data from 'Content' schema must be read by Content Provider.
         * To do that we are using a Loader.
         */
        if (uri.getScheme().equals("file")) {
            // the direct path to the file has been returned
            final String path = uri.getPath();
            final File file = new File(path);

            switch (mFileType) {
            case FILE_TYPE_ZIP:
                mFwFilePath = path;
                break;
            case FILE_TYPE_CFG:
                mCfgFilePath = path;
                break;
            case FILE_TYPE_TST:
                mTstFilePath = path;
                break;
            case FILE_TYPE_NONE:
            default:
                break;
            }

            updateFileInfo(file.getName(), file.length(), path, mFileType);
        } else if (uri.getScheme().equals("content")) {
            // an Uri has been returned
            //mFileUri = uri;
            // If application returned Uri for streaming, let's use it. Does it works?
            // FIXME both Uris works with Google Drive app. Why both? What's the difference?
            // How about other apps like DropBox?
            final Bundle extras = data.getExtras();
            //if(extras != null && extras.containsKey(Intent.EXTRA_STREAM))
            //    mFileUri = extras.getParcelable(Intent.EXTRA_STREAM);

            // File name and size must be obtained from Content Provider
            final Bundle bundle = new Bundle();
            bundle.putParcelable(EXTRA_URI, uri);
            getLoaderManager().restartLoader(REQUEST_SELECT_FILE, bundle, this);
        }
        break;
    }

    default:
        Log.e(TAG, "wrong request code");
        break;
    }
}

From source file:com.android.dragonkeyboardfirmwareupdater.KeyboardFirmwareUpdateService.java

/**
 * Handles intents ACTION_CONNECTION_STATE_CHANGED, ACTION_STATE_CHANGED,
 * ACTION_BOND_STATE_CHANGED, ACTION_KEYBOARD_UPDATE_CONFIRMED.
 * <p/>/* w  w  w  . ja v  a 2  s  . c om*/
 * [ACTION_STATE_CHANGED]
 * This action is used to keep track of ON/OFF state change on the system Bluetooth adapter.
 * The
 * purpose is to synchronize the local Bluetooth connectivity with system Bluetooth state.
 * <p/>
 * [ACTION_CONNECTION_STATE_CHANGED]
 * This action is used to keep track of the connection change on the target device. The purpose
 * is to synchronize the connection cycles of the local GATT connection and the system
 * Bluetooth
 * connection.
 * <p/>
 * [ACTION_BOND_STATE_CHANGED]
 * This action is used to keep track of the bond state change on the target device. The purpose
 * is to the connection cycles of the local GATT connection and the system Bluetooth
 * connection.
 * <p/>
 * [ACTION_KEYBOARD_UPDATE_CONFIRMED]
 * This action is used to receive the update confirmation from the user. The purpose is to
 * trigger DFU process.
 */
private void onHandleIntent(Context context, Intent intent) {
    final String action = intent.getAction();
    Log.d(TAG, "onHandleIntent: Received action: " + action);

    if (BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED.equals(action)) {

        if (!isBluetoothEnabled()) {
            Log.w(TAG, "onHandleIntent: Bluetooth connectivity not enabled");
            return;
        }

        // Match the connected device with the default keyboard name.
        Bundle extras = intent.getExtras();
        if (extras == null)
            return;
        final BluetoothDevice device = extras.getParcelable(BluetoothDevice.EXTRA_DEVICE);
        final int deviceConnectionState = extras.getInt(BluetoothAdapter.EXTRA_CONNECTION_STATE);

        Log.d(TAG, "onHandleIntent: " + device.getName() + " [" + device.getAddress() + "] change to state: "
                + deviceConnectionState);

        // Match the name of the target keyboard.
        if (!isTargetKeyboard(device))
            return;

        if (deviceConnectionState == BluetoothAdapter.STATE_CONNECTED) {
            // Prevent the second keyboard from using the service.
            if (isUpdateServiceInUse())
                return;

            obtainKeyboardInfo(device.getName(), device.getAddress());

            if (mDfuStatus != DFU_STATE_INFO_READY) {
                Log.w(TAG, "onHandleIntent: DFU preparation failed");
                changeDfuStatus(DFU_STATE_OBTAIN_INFO_ERROR);
                return;
            }

            showUpdateNotification();
        } else if (deviceConnectionState == BluetoothAdapter.STATE_DISCONNECTING) {
            handleGattDisconnection();
        }

    } else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
        final int adapterState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
        if (adapterState == BluetoothAdapter.STATE_ON) {
            if (!isBluetoothEnabled())
                enableBluetoothConnectivity();
        } else if (adapterState == BluetoothAdapter.STATE_TURNING_OFF) {
            // Terminate update process and disable Bluetooth connectivity.
            disableBluetoothConnectivity();

            // Since BluetoothAdapter has been disabled, the callback of disconnection would not
            // be called. Therefore a separate clean-up of GATT connection is need.
            cleanUpGattConnection();
        }

    } else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
        Bundle extras = intent.getExtras();
        if (extras == null)
            return;
        final BluetoothDevice device = extras.getParcelable(BluetoothDevice.EXTRA_DEVICE);
        final int deviceBondState = extras.getInt(BluetoothDevice.EXTRA_BOND_STATE);

        Log.d(TAG, "onHandleIntent: state change on device " + device.getName() + " [" + device.getAddress()
                + "], bond state: " + deviceBondState);

        if (!isTargetKeyboard(device))
            return;

        if (deviceBondState == BluetoothDevice.BOND_NONE) {
            handleGattDisconnection();
        }

    } else if (ACTION_KEYBOARD_UPDATE_CONFIRMED.equals(action)) {
        dismissUpdateNotification();

        if (mDfuStatus != DFU_STATE_INFO_READY || mDfuStatus == DFU_STATE_UPDATING) {
            Log.w(TAG, "onHandleIntent: DFP preparation not ready or DFU is in progress. ");
            changeDfuStatus(DFU_STATE_UPDATE_ABORTED);
            return;
        }

        String keyboardName = intent.getStringExtra(EXTRA_KEYBOARD_NAME);
        String keyboardAddress = intent.getStringExtra(EXTRA_KEYBOARD_ADDRESS);
        if (!mKeyboardName.equals(keyboardName) || !mKeyboardAddress.equals(keyboardAddress)) {
            Log.w(TAG, "onHandleIntent: No DFU service associated with " + keyboardName + " [" + keyboardAddress
                    + "]");
            return;
        }

        Log.d(TAG, "onHandleIntent: Start update process on " + keyboardName + " [" + keyboardAddress + "]");
        changeDfuStatus(DFU_STATE_SWITCHING_TO_DFU_MODE);

    } else if (ACTION_KEYBOARD_UPDATE_POSTPONED.equals(action)) {
        dismissUpdateNotification();
        // TODO(mcchou): Update the preference when the Settings keyboard entry is available.
    }
}