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:fr.bmartel.android.notti.service.bluetooth.BluetoothCustomManager.java

@SuppressLint("NewApi")
public void init(Context context) {

    // Initializes Bluetooth adapter.
    final BluetoothManager bluetoothManager = (BluetoothManager) context
            .getSystemService(Context.BLUETOOTH_SERVICE);

    mBluetoothAdapter = bluetoothManager.getAdapter();

    //init message handler
    mHandler = null;//  w w w.j  a  va2s.c o m
    mHandler = new Handler();

    scanCallback = new BluetoothAdapter.LeScanCallback() {

        @Override
        public void onLeScan(BluetoothDevice device, int rssi, final byte[] scanRecord) {

            if (device.getAddress() != null && device.getName() != null
                    && !scanningList.containsKey(device.getAddress())) {

                scanningList.put(device.getAddress(), device);

                try {
                    JSONObject object = new JSONObject();
                    object.put("address", device.getAddress());
                    object.put("deviceName", device.getName());

                    ArrayList<String> deviceInfo = new ArrayList<>();
                    deviceInfo.add(object.toString());

                    broadcastUpdateStringList(BluetoothEvents.BT_EVENT_DEVICE_DISCOVERED, deviceInfo);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    };
}

From source file:com.android.tv.settings.MainFragment.java

private void updateAccessories() {
    if (mAccessoriesGroup == null) {
        return;/*  ww  w. j  a v a  2 s . c o m*/
    }

    if (mBtAdapter == null) {
        mAccessoriesGroup.setVisible(false);
        mAccessoriesGroup.removeAll();
        return;
    }

    final Set<BluetoothDevice> bondedDevices = mBtAdapter.getBondedDevices();
    final Set<String> connectedBluetoothAddresses = BluetoothConnectionsManager.getConnectedSet(getContext());
    final Context themedContext = getPreferenceManager().getContext();

    final Set<String> touchedKeys = new ArraySet<>(bondedDevices.size() + 1);
    if (mAddAccessory != null) {
        touchedKeys.add(mAddAccessory.getKey());
    }

    for (final BluetoothDevice device : bondedDevices) {
        final String desc = connectedBluetoothAddresses.contains(device.getAddress())
                ? getString(R.string.accessory_connected)
                : null;

        final String key = "BluetoothDevice:" + device.getAddress();
        touchedKeys.add(key);
        Preference preference = mAccessoriesGroup.findPreference(key);
        if (preference == null) {
            preference = new Preference(themedContext);
            preference.setKey(key);
        }
        preference.setTitle(device.getName());
        preference.setSummary(desc);
        final int deviceImgId = AccessoryUtils.getImageIdForDevice(device);
        preference.setIcon(deviceImgId);
        preference.setFragment(BluetoothAccessoryFragment.class.getName());
        BluetoothAccessoryFragment.prepareArgs(preference.getExtras(), device.getAddress(), device.getName(),
                deviceImgId);
        mAccessoriesGroup.addPreference(preference);
    }

    for (int i = 0; i < mAccessoriesGroup.getPreferenceCount();) {
        final Preference preference = mAccessoriesGroup.getPreference(i);
        if (touchedKeys.contains(preference.getKey())) {
            i++;
        } else {
            mAccessoriesGroup.removePreference(preference);
        }
    }
}

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);//from   w w w .  j ava2s  . c om
    adapter.notifyDataSetChanged();

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

From source file:com.moto.miletus.application.ble.neardevice.NearDeviceHolder.java

/**
 * broadcastDeviceRoom// ww  w  .  j a  v  a  2  s .  c om
 *
 * @param device BluetoothDevice
 */
private void broadcastDeviceRoom(final BluetoothDevice device) {
    Intent i = new Intent(Strings.NEAR_DEVICE);

    if (device == null && getNearDevice() == null) {
        return;
    } else if (device != null && getNearDevice() != null && device.equals(getNearDevice())) {
        return;
    }

    context.sendBroadcast(i);

    if (device != null) {
        Log.i(TAG, "Near device: " + device.getAddress());
    } else {
        Log.i(TAG, "No near device.");
    }
}

From source file:com.bluetooth.mwoolley.microbitbledemo.ui.HrmListActivity.java

private void connectToDevice(BluetoothDevice device) {
    showMsg(Utility.htmlColorBlue("Connecting to device"));
    if (bluetooth_le_adapter.connect(device.getAddress())) {
    } else {/* ww w  . j  a  va2 s .  c  om*/
        showMsg(Utility.htmlColorRed("onConnect: failed to connect"));
    }
}

From source file:com.pileproject.drive.setting.machine.BluetoothMachineSelectFragment.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    resizeDialog();//from   w  w w  . j ava2s.com

    mPairedDevicesListView.setAdapter(new BluetoothMachineListAdapter(getActivity(),
            R.layout.view_bluetooth_machine_list, new LinkedList<BluetoothDevice>()));
    mPairedDevicesListView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
    mPairedDevicesListView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            ListView listView = (ListView) parent;
            BluetoothDevice bluetoothDevice = (BluetoothDevice) listView.getItemAtPosition(position);

            MachinePreferences.get(getActivity()).setMacAddress(bluetoothDevice.getAddress());

            Toast.makeText(getActivity(), getString(R.string.setting_bluetoothMachineSelect_toast_setDefault)
                    + "\n" + bluetoothDevice.getName(), Toast.LENGTH_LONG).show();
        }
    });

    mNewDevicesListView.setAdapter(new BluetoothMachineListAdapter(getActivity(),
            R.layout.view_bluetooth_machine_list, new LinkedList<BluetoothDevice>()));
    mNewDevicesListView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            ListView listView = (ListView) parent;
            BluetoothDevice bluetoothDevice = (BluetoothDevice) listView.getItemAtPosition(position);

            final String bluetoothDeviceName = bluetoothDevice.getName();

            ProgressDialogFragment.showDialog(getActivity(), getChildFragmentManager(),
                    R.string.setting_bluetoothMachineSelect_progress_connecting_title,
                    R.string.setting_bluetoothMachineSelect_progress_connecting_message, "tag");

            // cancel scanning process because this is very heavyweight
            BluetoothAdapter.getDefaultAdapter().cancelDiscovery();

            mSubscriptions.add(RxBluetoothConnector.pairing(bluetoothDevice)
                    .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BluetoothDevice>() {
                        @Override
                        public void onCompleted() {
                            updatePairedList();

                            BluetoothAdapter.getDefaultAdapter().startDiscovery();

                            ProgressDialogFragment.dismissDialog();
                        }

                        @Override
                        public void onError(Throwable e) {
                            BluetoothAdapter.getDefaultAdapter().startDiscovery();

                            Toast.makeText(getActivity(),
                                    getString(R.string.setting_bluetoothMachineSelect_toast_cannotConnect,
                                            bluetoothDeviceName),
                                    Toast.LENGTH_LONG).show();

                            ProgressDialogFragment.dismissDialog();
                        }

                        @Override
                        public void onNext(BluetoothDevice bluetoothDevice) {
                            MachinePreferences.get(getActivity()).setMacAddress(bluetoothDevice.getAddress());

                            Toast.makeText(getActivity(),
                                    getString(R.string.setting_bluetoothMachineSelect_toast_setDefault,
                                            bluetoothDeviceName),
                                    Toast.LENGTH_LONG).show();

                            BluetoothMachineListAdapter adapter = (BluetoothMachineListAdapter) mNewDevicesListView
                                    .getAdapter();
                            adapter.remove(bluetoothDevice);
                        }
                    }));
        }
    });
}

From source file:org.debian.paulliu.kajiradoubra.controller.KajiraDouBraController.java

@Override
public void onResume() {
    super.onResume();
    java.util.Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
    mBluetoothDevice = null;/*from  ww w  . j a  va  2  s .  c  o m*/
    // If there are paired devices
    if (pairedDevices.size() > 0) {
        // Loop through paired devices
        for (BluetoothDevice device : pairedDevices) {
            // Add the name and address to an array adapter to show in a ListView
            if (device.getName().compareTo("KajiraDouBra") == 0) {
                mBluetoothDevice = device;
                Log.i(LOGTAG, "BT Mac: " + device.getAddress());
                break;
            }
        }
    }
    if (mBluetoothDevice != null) {
        try {
            mBluetoothSocket = mBluetoothDevice.createRfcommSocketToServiceRecord(SPP_UUID);
        } catch (java.io.IOException e) {
            Log.w(LOGTAG, "Cannot get BluetoothSocket");
        }

        // Discovery is resource intensive.  Make sure it isn't going on
        // when you attempt to connect and pass your message.
        mBluetoothAdapter.cancelDiscovery();

        // Establish the connection.  This will block until it connects.
        Log.d(LOGTAG, "Connecting to Device...");
        try {
            mBluetoothSocket.connect();
            Log.d(LOGTAG, "Connection established and data link opened");
        } catch (java.io.IOException e) {
            try {
                mBluetoothSocket.close();
            } catch (java.io.IOException e2) {
                Log.e(LOGTAG, "In onResume() and unable to close socket during connection failure"
                        + e2.getMessage() + ".");
            }
        }
    }
}

From source file:com.example.shahrukhqasim2.deepmount.DeviceScanActivity.java

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    final BluetoothDevice device = mLeDeviceListAdapter.getDevice(position);
    if (device == null)
        return;//w  w  w .  ja  v  a2s.com
    final Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra(MainActivity.EXTRAS_DEVICE_NAME, device.getName());
    intent.putExtra(MainActivity.EXTRAS_DEVICE_ADDRESS, device.getAddress());
    if (mScanning) {
        mBluetoothAdapter.stopLeScan(mLeScanCallback);
        mScanning = false;
    }
    startActivity(intent);
}

From source file:com.hardwarebreakout.bleboosterdemo.DeviceScanActivity.java

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    final BluetoothDevice device = mLeDeviceListAdapter.getDevice(position);
    if (device == null)
        return;//from w ww.ja v  a 2s .  c om

    Intent resultIntent = new Intent();
    resultIntent.putExtra(NavigationActivity.EXTRAS_DEVICE_NAME, device.getName());
    resultIntent.putExtra(NavigationActivity.EXTRAS_DEVICE_ADDRESS, device.getAddress());
    setResult(Activity.RESULT_OK, resultIntent);
    finish();

    //        final Intent intent = new Intent(this, DeviceControlActivity.class);
    //        intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_NAME, device.getName());
    //        intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_ADDRESS, device.getAddress());
    //        if (mScanning) {
    //            mBluetoothAdapter.stopLeScan(mLeScanCallback);
    //            mScanning = false;
    //        }
    //        startActivity(intent);
}

From source file:com.github.akinaru.roboticbuttonpusher.bluetooth.BluetoothCustomManager.java

@SuppressLint("NewApi")
public void init(Context context) {

    // Initializes Bluetooth adapter.
    final BluetoothManager bluetoothManager = (BluetoothManager) context
            .getSystemService(Context.BLUETOOTH_SERVICE);

    mBluetoothAdapter = bluetoothManager.getAdapter();

    //init message handler
    mHandler = null;/*from w ww  .ja  v a2s  .  c o  m*/
    mHandler = new Handler();

    scanCallback = new BluetoothAdapter.LeScanCallback() {

        @Override
        public void onLeScan(BluetoothDevice device, int rssi, final byte[] scanRecord) {

            if (device.getAddress() != null && device.getName() != null) {

                dispatchBtDevices(device, rssi, scanRecord);
            }
        }
    };
}