Example usage for android.bluetooth BluetoothDevice getAddress

List of usage examples for android.bluetooth BluetoothDevice getAddress

Introduction

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

Prototype

public String getAddress() 

Source Link

Document

Returns the hardware address of this BluetoothDevice.

Usage

From source file:com.microchip.pcs.DeviceScanActivity.java

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    final BluetoothDevice device = mLeDeviceListAdapter.getDevice(position); //Get the Bluetooth device from the list adapter
    if (device == null) //Ignore if device is not valid
        return;//from  w  ww .  ja v  a 2 s .  c o  m
    final Intent intent = new Intent(this, DeviceControlActivity.class); //Create Intent to start the DeviceControlActivity 
    intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_NAME, device.getName()); //Add BLE device name to the intent (for info, not needed)
    intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_ADDRESS, device.getAddress()); //Add BLE device address to the intent
    if (mScanning) { //See if still scanning
        mBluetoothAdapter.stopLeScan(mLeScanCallback); //Stop the scan in progress
        mScanning = false; //Indicate that we are not scanning
    }
    startActivity(intent); //Start the DeviceControlActivity
}

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  . j av  a 2s .c o  m

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

From source file:it.angrydroids.epub3reader.MainActivity.java

private void addDevice(BluetoothDevice device, int rssi) {
    boolean deviceFound = false;

    for (BluetoothDevice listDev : deviceList) {
        if (listDev.getAddress().equals(device.getAddress())) {
            deviceFound = true;/*from  w  w  w  . j a va 2  s.  c  o  m*/
            break;
        }
    }

    devRssiValues.put(device.getAddress(), rssi);
    if (!deviceFound) {
        deviceList.add(device);
    }
}

From source file:com.filterdevice.ProfileScanningFragment.java

/**
 * Method to connect to the device selected. The time allotted for having a
 * connection is 8 seconds. After 8 seconds it will disconnect if not
 * connected and initiate scan once more
 *
 * @param device// w  w w  .j a  v a2s .com
 */

private void connectDevice(BluetoothDevice device, boolean isFirstConnect) {
    mDeviceAddress = device.getAddress();
    mDeviceName = device.getName();
    // Get the connection status of the device
    if (bluetoothLeService.getConnectionState() == bluetoothLeService.STATE_DISCONNECTED) {
        Logger.v("BLE DISCONNECTED STATE");
        // Disconnected,so connect
        bluetoothLeService.connect(mDeviceAddress);
        showConnectAlertMessage(mDeviceName, mDeviceAddress);
    } else {
        Logger.v("BLE OTHER STATE-->" + bluetoothLeService.getConnectionState());
        // Connecting to some devices,so disconnect and then connect
        bluetoothLeService.disconnect();
        Handler delayHandler = new Handler();
        delayHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                bluetoothLeService.connect(mDeviceAddress);
                showConnectAlertMessage(mDeviceName, mDeviceAddress);
            }
        }, DELAY_PERIOD);

    }
    if (isFirstConnect) {
        startConnectTimer();
        mConnectTimerON = true;
    }

}

From source file:com.molidt.easyandroid.bluetooth.BluetoothFragmentV4.java

private String getDeviceKey(BluetoothDevice device, UUID uuid) {
    return device.getAddress() + "_" + uuid.toString();
}

From source file:org.lsc.hellocordova.BluetoothPlugin.java

@Override
public PluginResult execute(String action, JSONArray arg1, String callbackId) {
    Log.d("BluetoothPlugin", "Plugin Called");
    PluginResult result = null;/*from   ww  w .java 2 s  .  co m*/
    context = (Context) this.ctx;

    // Register for broadcasts when a device is discovered
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    context.registerReceiver(mReceiver, filter);

    // Register for broadcasts when discovery starts
    filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    context.registerReceiver(mReceiver, filter);

    // Register for broadcasts when discovery has finished
    filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    context.registerReceiver(mReceiver, filter);

    // Register for broadcasts when connectivity state changes
    filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
    context.registerReceiver(mReceiver, filter);

    Looper.prepare();
    btadapter = BluetoothAdapter.getDefaultAdapter();
    found_devices = new ArrayList<BluetoothDevice>();

    if (ACTION_DISCOVER_DEVICES.equals(action)) {
        try {

            Log.d("BluetoothPlugin", "We're in " + ACTION_DISCOVER_DEVICES);

            found_devices.clear();
            discovering = true;

            if (btadapter.isDiscovering()) {
                btadapter.cancelDiscovery();
            }

            Log.i("BluetoothPlugin", "Discovering devices...");
            btadapter.startDiscovery();

            while (discovering) {
            }

            String devicesFound = null;
            int count = 0;
            devicesFound = "[";
            for (BluetoothDevice device : found_devices) {
                Log.i("BluetoothPlugin",
                        device.getName() + " " + device.getAddress() + " " + device.getBondState());
                if ((device.getName() != null) && (device.getBluetoothClass() != null)) {
                    devicesFound = devicesFound + " { \"name\" : \"" + device.getName() + "\" ,"
                            + "\"address\" : \"" + device.getAddress() + "\" ," + "\"class\" : \""
                            + device.getBluetoothClass().getDeviceClass() + "\" }";
                    if (count < found_devices.size() - 1)
                        devicesFound = devicesFound + ",";
                } else
                    Log.i("BluetoothPlugin",
                            device.getName() + " Problems retrieving attributes. Device not added ");
                count++;
            }

            devicesFound = devicesFound + "] ";

            Log.d("BluetoothPlugin - " + ACTION_DISCOVER_DEVICES, "Returning: " + devicesFound);
            result = new PluginResult(Status.OK, devicesFound);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_DISCOVER_DEVICES, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_IS_BT_ENABLED.equals(action)) {
        try {
            Log.d("BluetoothPlugin", "We're in " + ACTION_IS_BT_ENABLED);

            boolean isEnabled = btadapter.isEnabled();

            Log.d("BluetoothPlugin - " + ACTION_IS_BT_ENABLED,
                    "Returning " + "is Bluetooth Enabled? " + isEnabled);
            result = new PluginResult(Status.OK, isEnabled);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_IS_BT_ENABLED, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_ENABLE_BT.equals(action)) {
        try {
            Log.d("BluetoothPlugin", "We're in " + ACTION_ENABLE_BT);

            boolean enabled = false;

            Log.d("BluetoothPlugin", "Enabling Bluetooth...");

            if (btadapter.isEnabled()) {
                enabled = true;
            } else {
                enabled = btadapter.enable();
            }

            Log.d("BluetoothPlugin - " + ACTION_ENABLE_BT, "Returning " + "Result: " + enabled);
            result = new PluginResult(Status.OK, enabled);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_ENABLE_BT, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_DISABLE_BT.equals(action)) {
        try {
            Log.d("BluetoothPlugin", "We're in " + ACTION_DISABLE_BT);

            boolean disabled = false;

            Log.d("BluetoothPlugin", "Disabling Bluetooth...");

            if (btadapter.isEnabled()) {
                disabled = btadapter.disable();
            } else {
                disabled = true;
            }

            Log.d("BluetoothPlugin - " + ACTION_DISABLE_BT, "Returning " + "Result: " + disabled);
            result = new PluginResult(Status.OK, disabled);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_DISABLE_BT, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_PAIR_BT.equals(action)) {
        try {
            Log.d("BluetoothPlugin", "We're in " + ACTION_PAIR_BT);

            String addressDevice = arg1.getString(0);

            if (btadapter.isDiscovering()) {
                btadapter.cancelDiscovery();
            }

            BluetoothDevice device = btadapter.getRemoteDevice(addressDevice);
            boolean paired = false;

            Log.d("BluetoothPlugin", "Pairing with Bluetooth device with name " + device.getName()
                    + " and address " + device.getAddress());

            try {
                Method m = device.getClass().getMethod("createBond");
                paired = (Boolean) m.invoke(device);
            } catch (Exception e) {
                e.printStackTrace();
            }

            Log.d("BluetoothPlugin - " + ACTION_PAIR_BT, "Returning " + "Result: " + paired);
            result = new PluginResult(Status.OK, paired);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_PAIR_BT, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_UNPAIR_BT.equals(action)) {
        try {
            Log.d("BluetoothPlugin", "We're in " + ACTION_UNPAIR_BT);

            String addressDevice = arg1.getString(0);

            if (btadapter.isDiscovering()) {
                btadapter.cancelDiscovery();
            }

            BluetoothDevice device = btadapter.getRemoteDevice(addressDevice);
            boolean unpaired = false;

            Log.d("BluetoothPlugin", "Unpairing Bluetooth device with " + device.getName() + " and address "
                    + device.getAddress());

            try {
                Method m = device.getClass().getMethod("removeBond");
                unpaired = (Boolean) m.invoke(device);
            } catch (Exception e) {
                e.printStackTrace();
            }

            Log.d("BluetoothPlugin - " + ACTION_UNPAIR_BT, "Returning " + "Result: " + unpaired);
            result = new PluginResult(Status.OK, unpaired);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_UNPAIR_BT, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_LIST_BOUND_DEVICES.equals(action)) {
        try {
            Log.d("BluetoothPlugin", "We're in " + ACTION_LIST_BOUND_DEVICES);

            Log.d("BluetoothPlugin", "Getting paired devices...");
            Set<BluetoothDevice> pairedDevices = btadapter.getBondedDevices();
            int count = 0;
            String resultBoundDevices = "[ ";
            if (pairedDevices.size() > 0) {
                for (BluetoothDevice device : pairedDevices) {
                    Log.i("BluetoothPlugin",
                            device.getName() + " " + device.getAddress() + " " + device.getBondState());

                    if ((device.getName() != null) && (device.getBluetoothClass() != null)) {
                        resultBoundDevices = resultBoundDevices + " { \"name\" : \"" + device.getName() + "\" ,"
                                + "\"address\" : \"" + device.getAddress() + "\" ," + "\"class\" : \""
                                + device.getBluetoothClass().getDeviceClass() + "\" }";
                        if (count < pairedDevices.size() - 1)
                            resultBoundDevices = resultBoundDevices + ",";
                    } else
                        Log.i("BluetoothPlugin",
                                device.getName() + " Problems retrieving attributes. Device not added ");
                    count++;
                }

            }

            resultBoundDevices = resultBoundDevices + "] ";

            Log.d("BluetoothPlugin - " + ACTION_LIST_BOUND_DEVICES, "Returning " + resultBoundDevices);
            result = new PluginResult(Status.OK, resultBoundDevices);

        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_LIST_BOUND_DEVICES, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_STOP_DISCOVERING_BT.equals(action)) {
        try {
            Log.d("BluetoothPlugin", "We're in " + ACTION_STOP_DISCOVERING_BT);

            boolean stopped = true;

            Log.d("BluetoothPlugin", "Stop Discovering Bluetooth Devices...");

            if (btadapter.isDiscovering()) {
                Log.i("BluetoothPlugin", "Stop discovery...");
                stopped = btadapter.cancelDiscovery();
                discovering = false;
            }

            Log.d("BluetoothPlugin - " + ACTION_STOP_DISCOVERING_BT, "Returning " + "Result: " + stopped);
            result = new PluginResult(Status.OK, stopped);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_STOP_DISCOVERING_BT, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_IS_BOUND_BT.equals(action)) {
        try {
            Log.d("BluetoothPlugin", "We're in " + ACTION_IS_BOUND_BT);
            String addressDevice = arg1.getString(0);
            BluetoothDevice device = btadapter.getRemoteDevice(addressDevice);
            Log.i("BluetoothPlugin", "BT Device in state " + device.getBondState());

            boolean state = false;

            if (device != null && device.getBondState() == 12)
                state = true;
            else
                state = false;

            Log.d("BluetoothPlugin", "Is Bound with " + device.getName() + " - address " + device.getAddress());

            Log.d("BluetoothPlugin - " + ACTION_IS_BOUND_BT, "Returning " + "Result: " + state);
            result = new PluginResult(Status.OK, state);

        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_IS_BOUND_BT, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_OPEN_SOCKET.equals(action)) {
        Log.d("BluetoothPlugin", "We're in " + ACTION_OPEN_SOCKET);
        try {
            String addressDevice = arg1.getString(0);
            openSocket(addressDevice);
            result = new PluginResult(Status.OK, "true");

        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_OPEN_SOCKET, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_MESSAGE_SOCKET.equals(action)) {
        Log.d("BluetoothPlugin", "We're in " + ACTION_MESSAGE_SOCKET);
        try {
            String msg = arg1.getString(0);
            sendData(msg);
            result = new PluginResult(Status.OK, "true");

        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_MESSAGE_SOCKET, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_CLOSE_SOCKET.equals(action)) {
        Log.d("BluetoothPlugin", "We're in " + ACTION_CLOSE_SOCKET);
        try {
            closeSocket();
            result = new PluginResult(Status.OK, "true");

        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_CLOSE_SOCKET, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    } else if (ACTION_PING_SOCKET.equals(action)) {
        try {

            Log.d("BluetoothPlugin", "We're in " + ACTION_PING_SOCKET);

            String data = "{";
            data = data + "\"count\": " + Integer.toString(pingCount) + ",";

            String items = "";
            for (String item : pingData) {
                items = items + ",\"" + item + "\"";
            }

            data = data + "  \"data\" : [" + items.substring(1) + " ]";
            data = data + "}";

            result = new PluginResult(Status.OK, data);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_PING_SOCKET, "Got Exception " + Ex.getMessage());
            result = new PluginResult(Status.ERROR);
        }

    }

    else {
        result = new PluginResult(Status.INVALID_ACTION);
        Log.d("BluetoothPlugin", "Invalid action : " + action + " passed");
    }
    return result;
}

From source file:com.elitise.appv2.Peripheral.java

private void disconnectFromDevices() {
    Log.d(TAG, "Disconnecting devices...");
    for (BluetoothDevice device : mBluetoothManager.getConnectedDevices(BluetoothGattServer.GATT)) {
        Log.d(TAG, "Devices: " + device.getAddress() + " " + device.getName());
        mGattServer.cancelConnection(device);
        //mGattServer.close();
    }/*w w  w .  j a  v  a  2s  .  co  m*/

}

From source file:com.android.tv.settings.accessories.AddAccessoryActivity.java

protected void updateView() {
    if (mActionView == null || mStartTime == 0) {
        // view not yet ready, update will happen on first layout event
        return;//from w w  w.  ja  v  a  2 s .  c  o  m
    }

    synchronized (mLock) {
        int prevNumDevices = mActions.size();
        mActions.clear();

        if (mActionFragment != null && mBtPairer != null) {
            // Add entries for the discovered Bluetooth devices
            for (BluetoothDevice bt : mBtDevices) {
                String title = bt.getName();
                String desc;
                if (mCurrentTargetAddress.equalsIgnoreCase(bt.getAddress())
                        && !mCurrentTargetStatus.isEmpty()) {
                    desc = mCurrentTargetStatus;
                } else if (mCancelledAddress.equalsIgnoreCase(bt.getAddress())) {
                    desc = getString(R.string.accessory_state_canceled);
                } else {
                    desc = bt.getAddress();
                }
                mActions.add(
                        new Action.Builder().key(KEY_BT_DEVICE).title(title).description(desc.toUpperCase())
                                .drawableResource(AccessoryUtils.getImageIdForDevice(bt)).build());
            }
        }

        // Update the main fragment.
        ActionAdapter adapter = (ActionAdapter) mActionFragment.getAdapter();
        if (adapter != null) {
            adapter.setActions(mActions);
        }

        if (!mActionsVisible && mActions.size() > 0) {
            mActionsVisible = true;
            long delay = ANIMATE_IN_DELAY - (SystemClock.elapsedRealtime() - mStartTime);
            if (delay > 0) {
                // Make sure we have a little bit of time after the activity
                // fades in
                // before we animate the actions in
                mActionView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        animateActionsIn();
                    }
                }, delay);
            } else {
                animateActionsIn();
            }
        }

        if (mNoInputMode) {
            if (DEBUG)
                Log.d(TAG, "stopping auto-exit timer");
            mAutoExitHandler.removeCallbacks(mAutoExitRunnable);
            if (mActions.size() == 1 && prevNumDevices == 0) {
                // first device added, start counter for autopair
                mMsgHandler.sendEmptyMessageDelayed(MSG_START_AUTOPAIR_COUNTDOWN, TIME_TO_START_AUTOPAIR_COUNT);
            } else {

                // Start timer count down for exiting activity.
                if (DEBUG)
                    Log.d(TAG, "starting auto-exit timer");
                mAutoExitHandler.postDelayed(mAutoExitRunnable, EXIT_TIMEOUT_MILLIS);

                if (mActions.size() > 1) {
                    // More than one device found, cancel auto pair
                    cancelPairingCountdown();

                    if (!mShowingMultiFragment && !mFragmentTransactionPending) {
                        if (mActionsAnimationDone) {
                            switchToMultipleDevicesFragment();
                        } else {
                            mFragmentTransactionPending = true;
                        }
                    }
                }
            }
        }
    }
}

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  2  s .c om*/
            .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:it.angrydroids.epub3reader.MainActivity.java

private void ConnectBLEDevice() {
    for (BluetoothDevice listDev : deviceList) {
        if (listDev.getName().equals(UartService.BLEDeviceName)) {
            mDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(listDev.getAddress());
            mService.connect(listDev.getAddress());
            break;
        }//from  w  ww .j a  va2  s. c  o m
    }
}