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.example.android.bluetoothlegatt.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  . jav  a2  s . c  o m*/
    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

private void dispatchBtDevices(BluetoothDevice device, int rssi, final byte[] scanRecord) {

    if (scanningList.containsKey(device.getAddress())) {

    } else {//from  w w  w . j  av  a2 s. c o m

        Log.v(TAG, "found a new Bluetooth device : " + device.getName() + " : " + device.getAddress());

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

        try {
            JSONObject object = new JSONObject();
            object.put(JsonConstants.BT_ADDRESS, device.getAddress());
            object.put(JsonConstants.BT_DEVICE_NAME, device.getName());
            object.put(JsonConstants.BT_ADVERTISING_INTERVAL, -1);

            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.waleed.arduino101led.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 ww w.j  av  a 2 s.c  o m*/
    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.bleanalyzer.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  w w. j  ava2  s. 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) {

                if (device.getName().equals("RFdroid")) {
                    dispatchRFdroid(device, rssi, scanRecord);
                } else {
                    dispatchBtDevices(device, rssi, scanRecord);
                }
            }
        }
    };
}

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

@RequiresPermission(Manifest.permission.BLUETOOTH)
public NativeBluetoothStack(@NonNull Context applicationContext, @NonNull ErrorListener errorListener,
        @NonNull LoggerFacade logger) {/*  www.j  a v a  2 s . com*/
    this.applicationContext = applicationContext;
    this.errorListener = errorListener;
    this.logger = logger;

    this.bluetoothManager = (BluetoothManager) applicationContext.getSystemService(Context.BLUETOOTH_SERVICE);
    this.adapter = bluetoothManager.getAdapter();
    if (adapter != null) {
        final BroadcastReceiver powerStateReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                final int newState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
                if (newState == BluetoothAdapter.STATE_ON) {
                    enabled.onNext(true);
                } else if (newState == BluetoothAdapter.STATE_OFF || newState == BluetoothAdapter.ERROR) {
                    enabled.onNext(false);
                }
            }
        };
        applicationContext.registerReceiver(powerStateReceiver,
                new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
        enabled.onNext(adapter.isEnabled());

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            final BroadcastReceiver pairingReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    final Intent broadcast = new Intent(ACTION_PAIRING_REQUEST);
                    broadcast.putExtra(GattPeripheral.EXTRA_NAME, device.getName());
                    broadcast.putExtra(GattPeripheral.EXTRA_ADDRESS, device.getAddress());
                    LocalBroadcastManager.getInstance(context).sendBroadcast(broadcast);
                }
            };
            applicationContext.registerReceiver(pairingReceiver,
                    new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST));
        }
    } else {
        logger.warn(LOG_TAG, "Host device has no bluetooth hardware!");
        enabled.onNext(false);
    }
}

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

private void dispatchBtDevices(BluetoothDevice device, int rssi, final byte[] scanRecord) {

    if (scanningList.containsKey(device.getAddress())) {

        if (adListener != null && measurement.getBtDevice() != null
                && measurement.getBtDevice().getDeviceAddress().equals(device.getAddress())) {

            long ts = new Date().getTime();
            measurement.getHistoryList().add(ts);
            adListener.onADframeReceived(ts, measurement.getHistoryList());
        }/*w  w w  .  j ava  2 s. c o m*/
    } else {

        Log.i(TAG, "found a new Bluetooth device : " + device.getName() + " : " + device.getAddress());

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

        try {
            JSONObject object = new JSONObject();
            object.put(JsonConstants.BT_ADDRESS, device.getAddress());
            object.put(JsonConstants.BT_DEVICE_NAME, device.getName());
            object.put(JsonConstants.BT_ADVERTISING_INTERVAL, -1);

            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.xiaocheng.xc_application.bluetoothutils.DeviceScanActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view_bluetooth_list);

    //getActionBar().setTitle(R.string.title_devices);
    mHandler = new Handler();

    //???//from  w  w w . ja va2  s  .  c  om
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        //??
        ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.ACCESS_COARSE_LOCATION },
                /*MY_PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION*/0);
        //?? ????
        if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_CONTACTS)) {
            Toast.makeText(this, "shouldShowRequestPermissionRationale", Toast.LENGTH_SHORT).show();
        }
    }

    // Use this check to determine whether BLE is supported on the device.  Then you can
    // selectively disable BLE-related features.
    if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
        Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
        finish();
    }

    // Initializes a Bluetooth adapter.  For API level 18 and above, get a reference to
    // BluetoothAdapter through BluetoothManager.
    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();

    // Checks if Bluetooth is supported on the device.
    if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
        /* Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show();
         finish();
         return;*/
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);

    }

    lv_bleList = (ListView) findViewById(R.id.lv_bleList);

    mDevListAdapter = new DeviceListAdapter();
    lv_bleList.setAdapter(mDevListAdapter);

    lv_bleList.setOnItemClickListener(new OnItemClickListener() {

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

                BluetoothDevice device1 = mDevListAdapter.getItem(position);
                if (device1 == null)
                    return;
                Intent intent1 = new Intent();
                intent1.putExtra(EXTRAS_DEVICE_NAME, device1.getName());
                intent1.putExtra(EXTRAS_DEVICE_ADDRESS, device1.getAddress());
                if (mScanning) {
                    mBluetoothAdapter.stopLeScan(mLeScanCallback);
                    mScanning = false;
                }

                // setResult  intent data Activity
                setResult(Activity.RESULT_OK, intent1);
                //? Activity
                finish();
                //                    startActivity(intent1);
            }
        }
    });
}

From source file:activities.GatewayActivity.java

@Override
public void WATCHiTSwitchClicked(boolean on) {
    if (on) {/*w  w w.j a v a  2 s.c om*/
        if (sApp.currentActiveSpace == null) {
            showToast(getString(R.string.register_event_first));
            configFragment.updateWATCHiTView(false);
            return;
        }
        if (!btAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivity(enableBtIntent);
            sApp.isWATChiTOn = false;
            return;
        } else {
            sApp.service.start();
            //sApp.isWATChiTOn = true;
            Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();
            arrayAdapter = new ArrayList<String>();
            devices = new ArrayList<BluetoothDevice>();
            if (pairedDevices.size() > 0) {
                for (BluetoothDevice device : pairedDevices) {
                    arrayAdapter.add("Name: " + device.getName() + "\n" + getString(R.string.adress) + " :"
                            + device.getAddress());
                    devices.add(device);
                    //sApp.arrayAdapter.add()
                }
                sApp.bluetoothDevices = devices;
            }
            Bundle b = new Bundle();
            b.putStringArrayList("adapter", arrayAdapter);
            ChooseBlueToothDeviceDialog dialog = new ChooseBlueToothDeviceDialog();
            dialog.setArguments(b);
            dialog.show(getSupportFragmentManager(), "pairedbluetoothdevices");
        }
    }
    if (!on) {
        if (sApp.service.isRunning())
            sApp.service.stop();
        showToast(getString(R.string.toast_stopped_watchit_service));
        sApp.isWATChiTOn = false;
    }
    configFragment.updateWATCHiTView(sApp.isWATChiTOn);
}

From source file:ch.ethz.inf.vs.a1.fabischn.ble.MainActivity.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.  java2 s. c o m*/
    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);
        mBluetoothLeScanner.stopScan(mScanCallback);
        mScanning = false;
    }
    startActivity(intent);
}

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

private void dispatchRFdroid(BluetoothDevice device, int rssi, final byte[] scanRecord) {

    if (scanningList.containsKey(device.getAddress())) {

        if (adListener != null && measurement.getBtDevice() != null
                && measurement.getBtDevice().getDeviceAddress().equals(device.getAddress())) {

            long ts = new Date().getTime();
            measurement.getHistoryList().add(ts);
            adListener.onADframeReceived(ts, measurement.getHistoryList());
        }/*  w  w w  .  j  a  v a  2  s .c  om*/
    } else {
        Log.i(TAG, "found a RFdroid");

        List<ADStructure> structures = ADPayloadParser.getInstance().parse(scanRecord);

        int advInterval = -1;

        for (ADStructure structure : structures) {

            if (structure instanceof ADManufacturerSpecific) {

                ADManufacturerSpecific data = (ADManufacturerSpecific) structure;

                if (data.getData().length == 9) {

                    byte[] name = new byte[7];
                    System.arraycopy(data.getData(), 0, name, 0, 7);

                    String nameStr = new String(name);
                    if (nameStr.equals("RFdroid")) {
                        advInterval = (data.getData()[7] << 8) + (data.getData()[8] & 0xFF);
                        Log.i(TAG, "current scan interval : " + advInterval);
                    }
                }
            }
        }

        if (advInterval != -1) {

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

            if (!measurement.isSelectionningDevice())
                measurement.setBtDevice(new BluetoothObject(device.getAddress(), device.getName(),
                        (int) (advInterval * 0.625)));

            try {
                JSONObject object = new JSONObject();
                object.put(JsonConstants.BT_ADDRESS, device.getAddress());
                object.put(JsonConstants.BT_DEVICE_NAME, device.getName());
                object.put(JsonConstants.BT_ADVERTISING_INTERVAL, (int) (advInterval * 0.625));

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

                broadcastUpdateStringList(BluetoothEvents.BT_EVENT_DEVICE_DISCOVERED, deviceInfo);

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

}