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:org.deviceconnect.android.deviceplugin.heartrate.fragment.HeartRateDeviceSettingsFragment.java

private void updateView(final List<BluetoothDevice> devices) {
    if (mDeviceAdapter == null) {
        return;/* w  w  w  .j a  va  2s  .c  o m*/
    }
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            mDeviceAdapter.clear();
            mDeviceAdapter.addAll(createDeviceContainers());
            if (devices != null) {
                for (BluetoothDevice device : devices) {
                    if (!containAddressForAdapter(device.getAddress())) {
                        mDeviceAdapter.add(createContainer(device));
                    }
                }
            }
            mDeviceAdapter.notifyDataSetChanged();
        }
    });
}

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

public void startReading(BluetoothDevice device) {
    Log.i(TAG, "Starting to read data for device: " + device.getName());
    hexiwearDevice = hexiwearDevices.getDevice(device.getAddress());
    bluetoothDevice = device;/*from   w  ww. ja  v  a2 s  . c om*/
    createGATT(device);

    if (credentials.username().get().equals("Demo")) {
        return;
    }

    wolk = new Wolk(hexiwearDevice);
    wolk.setLogger(new Logger() {
        @Override
        public void info(final String message) {
            Log.i(TAG, message);
        }

        @Override
        public void error(final String message, final Throwable e) {
            Log.e(TAG, message, e);
        }
    });

    if (hexiwearDevices.shouldTransmit(device)) {
        final int publishInterval = hexiwearDevices.getPublishInterval(hexiwearDevice);
        wolk.startAutoPublishing(publishInterval);
    }
}

From source file:com.polyvi.xface.extension.bluetooth.XBluetoothExt.java

/**
 * ???//w  w  w.  j a v a  2  s.  c om
 *
 * @return PluginResult
 */
private PluginResult listBoundBluetooth() {
    Set<BluetoothDevice> pairedDevices = mBtadapter.getBondedDevices();
    JSONArray devicesBound = new JSONArray();
    if (pairedDevices.size() > 0) {
        for (BluetoothDevice device : pairedDevices) {
            if ((device.getName() != null) && (device.getBluetoothClass() != null)) {
                JSONObject boundDevice = new JSONObject();
                try {
                    boundDevice.put(DEVICE_NAME, device.getName());
                    boundDevice.put(DEVICE_MACADDRESSG, device.getAddress());
                    devicesBound.put(boundDevice);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }

    }
    return new PluginResult(PluginResult.Status.OK, devicesBound);
}

From source file:org.deviceconnect.android.deviceplugin.heartrate.fragment.HeartRateDeviceSettingsFragment.java

/**
 * Create a DeviceContainer from BluetoothDevice.
 *
 * @param device Instance of BluetoothDevice
 * @return Instance of DeviceContainer//from  www.  j av a2 s .  co m
 */
private DeviceContainer createContainer(final BluetoothDevice device) {
    DeviceContainer container = new DeviceContainer();
    container.setName(device.getName(), device.getAddress());
    return container;
}

From source file:com.javadog.bluetoothproximitylock.BluetoothFragment.java

@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long rowId) {
    SharedPreferences.Editor prefsEditor = userPrefs.edit();
    switch (adapterView.getId()) {
    case R.id.bt_device_chooser:
        //Feed the selected device's address to BluetoothManager
        Set<BluetoothDevice> devicesSet = BluetoothManager.getAllBtDevices();
        BluetoothDevice chosenDevice = (BluetoothDevice) devicesSet.toArray()[position];
        BluetoothManager.setSelectedDevice(chosenDevice);

        //Update saved preference
        prefsEditor.putString(BluetoothFragment.PREF_BT_DEVICE_ADDRESS, chosenDevice.getAddress());
        break;//from  ww w.  j av a2s. c o  m

    case R.id.bt_lock_distances:
        //Update saved preference
        prefsEditor.putInt(PREF_LOCK_DISTANCE, lockDistance.getSelectedItemPosition());
        break;

    case R.id.bt_refresh_interval:
        //Save the spinner value into our instance variable here
        refreshInterval = interpretRefreshSpinner(refreshIntervalSpinner.getSelectedItemPosition());

        //Update saved preference
        prefsEditor.putInt(PREF_REFRESH_INTERVAL, refreshIntervalSpinner.getSelectedItemPosition());
        break;
    }
    prefsEditor.apply();

    //Call startService again (if the service is running) in order to update its settings based on new prefs.
    if (serviceBound) {
        startBtService();
    }
}

From source file:no.nordicsemi.android.BLE.profile.BleProfileServiceReadyActivity.java

@Override
public void onDeviceSelected(final BluetoothDevice device) {
    mLogSession = Logger.newSession(getApplicationContext(), getString(R.string.proximity_feature_title),
            device.getAddress(), device.getName());
    mDeviceNameView.setText(mDeviceName = device.getName());
    mConnectButton.setText(R.string.action_disconnect);

    // The device may not be in the range but the service will try to connect to it if it reach it
    Logger.v(mLogSession, "Creating service...");
    final Intent service = new Intent(this, getServiceClass());
    service.putExtra(BleProfileService.EXTRA_DEVICE_ADDRESS, device.getAddress());
    if (mLogSession != null)
        service.putExtra(BleProfileService.EXTRA_LOG_URI, mLogSession.getSessionUri());
    startService(service);//from w ww . jav a 2s . c om
    bindService(service, mServiceConnection, 0);
}

From source file:com.notalenthack.blaster.BluetoothSerialService.java

/**
 * Start the ConnectThread to initiate a connection to a remote device.
 * @param device  The BluetoothDevice to connect
 *//* ww  w. j a va  2s  . c  om*/
public synchronized void connect(BluetoothDevice device) {
    if (D)
        Log.d(TAG, "connect to: " + device);

    // Already connecting or connected...
    if (mDevice != null && mDevice.getAddress().equals(device.getAddress()) && getState() == STATE_CONNECTED) {
        // reset state to Connected so we get a message in the UI to
        // bring up our UI for set IP address
        setState(STATE_CONNECTED);
        return;
    }

    mDevice = device;

    // Cancel any thread attempting to make a connection
    if (mState == STATE_CONNECTING) {
        if (mConnectThread != null) {
            mConnectThread.cancel();
            mConnectThread = null;
        }
    }

    // Cancel any thread currently running a connection
    if (mConnectedThread != null) {
        mConnectedThread.cancel();
        mConnectedThread = null;
    }

    // Start the thread to connect with the given device
    mConnectThread = new ConnectThread(device);
    mConnectThread.start();
    setState(STATE_CONNECTING);
}

From source file:com.mchp.android.PIC32_BTSK.PIC32_BTSK.java

@Override
public synchronized void onResume() {
    super.onResume();
    if (D)/*from  w  w w.j av  a2 s. co  m*/
        Log.e(TAG, "+ ON RESUME +");

    // Performing this check in onResume() covers the case in which BT was
    // not enabled during onStart(), so we were paused to enable it...
    // onResume() will be called when ACTION_REQUEST_ENABLE activity returns.
    if (mBluetoothService != null) {
        // Only if the state is STATE_NONE, do we know that we haven't started already
        if (mBluetoothService.getState() == BluetoothService.STATE_NONE) {
            // Start the Bluetooth service
            mBluetoothService.start();
        }

        // Reconnect last connected device
        if (mBluetoothService.getState() != BluetoothService.STATE_CONNECTED) {
            SharedPreferences settings = getSharedPreferences(PREF_DEVICE_ADDR, 0);
            mLastConnectedDeviceAddr = settings.getString(DEVICE_ADDR, null);

            if (mLastConnectedDeviceAddr != null) {
                BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
                // Get a set of currently paired devices
                Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();
                for (BluetoothDevice device : pairedDevices) {
                    if (device.getAddress().equals(mLastConnectedDeviceAddr)) {
                        mBluetoothService.connect(device, false);
                        break;
                    }
                }
            }

        }
    }
}

From source file:org.harleydroid.HarleyDroidService.java

public void setInterfaceType(String interfaceType, BluetoothDevice dev) {
    boolean reconnect = (mInterface != null);

    if (mInterfaceType == null || !mInterfaceType.equals(interfaceType)
            || (!HarleyDroid.EMULATOR && (mDevice == null || !mDevice.getAddress().equals(dev.getAddress())))) {

        if (D)/*from  w  ww  .jav a 2 s .  co  m*/
            Log.d(TAG, "setInterfaceType(" + interfaceType + ")");

        mInterfaceType = interfaceType;
        mDevice = dev;
        if (reconnect)
            doDisconnect();
        if (HarleyDroid.EMULATOR)
            mInterface = new EmulatorInterface(this);
        else {
            if (interfaceType.equals("elm327"))
                mInterface = new ELM327Interface(this, dev);
            else if (interfaceType.equals("hdi"))
                mInterface = new HarleyDroidInterface(this, dev);
        }
        mCurrentState = STATE_DISCONNECT;
        if (reconnect)
            stateMachine();
    }
}

From source file:com.phonegap.plugin.bluetooth.BluetoothPlugin.java

@Override
public PluginResult execute(String action, JSONArray arg1, String callbackId) {
    Log.d("BluetoothPlugin", "Plugin Called");
    PluginResult result = null;/* w  w w  .  j  a  v a 2s  .  co m*/
    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 {
        result = new PluginResult(Status.INVALID_ACTION);
        Log.d("BluetoothPlugin", "Invalid action : " + action + " passed");
    }
    return result;
}