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.github.w666.ezonwatch.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 w  w  .j a  va2s .  co  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());
    Preferences.writeDeviceName(getApplicationContext(), device.getName());
    Preferences.writeDeviceAddress(getApplicationContext(), device.getAddress());
    if (mScanning) {
        mBluetoothAdapter.stopLeScan(mLeScanCallback);
        mScanning = false;
    }
    startActivity(intent);
}

From source file:no.android.proxime.profile.BleProfileServiceReadyActivity.java

@Override
public void onDeviceSelected(final BluetoothDevice device, final String name) {
    final int titleId = getLoggerProfileTitle();
    if (titleId > 0)
        mLogSession = Logger.newSession(getApplicationContext(), getString(titleId), device.getAddress(), name);
    mDeviceNameView.setText(mDeviceName = name);
    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 w  w .j  ava 2  s .c om
    bindService(service, mServiceConnection, 0);
}

From source file:dev.coatl.co.urband_basic.presenter.activities.StartSearchDevice.java

private void setResources() {
    handler = new Handler();
    listDevices = (ListView) findViewById(R.id.urband_device_list);
    listDevices.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override//from  ww w.ja v  a  2  s. c  o m
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            final BluetoothDevice device = ((BluetoothUrbandDeviceItem) parent.getItemAtPosition(position))
                    .getDevice();
            if (device == null) {
                return;
            }
            intent = new Intent(getApplicationContext(), ActivityHomePanel.class);
            intent.putExtra(EXTRAS_DEVICE_NAME, device.getName());
            intent.putExtra(EXTRAS_DEVICE_ADDRESS, device.getAddress());
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            if (scanning) {
                bluetoothAdapter.stopLeScan(scanDevices);
                scanning = false;
            }
            startActivity(intent);
        }
    });
    if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
        Toast.makeText(this, "Este dispositivo no soporta Bluetooth", Toast.LENGTH_SHORT).show();
        finish();
    }
    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    bluetoothAdapter = bluetoothManager.getAdapter();
}

From source file:org.bcsphere.bluetooth.BluetoothG43plus.java

@Override
public void connect(JSONArray json, CallbackContext callbackContext) {
    Log.i(TAG, "connect");
    String deviceAddress = Tools.getData(json, Tools.DEVICE_ADDRESS);
    if (connectedDevice.get(deviceAddress) != null) {
        Tools.sendSuccessMsg(callbackContext);
        return;/*w  ww. ja  v a  2 s.c  o m*/
    }
    connectCC.put(deviceAddress, callbackContext);
    BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(deviceAddress);
    mBluetoothGatts.put(device.getAddress(), device.connectGatt(mContext, false, mGattCallback));
}

From source file:org.deviceconnect.android.deviceplugin.health.fragment.HealthCareDeviceSettingsFragment.java

/**
 * Create a DeviceContainer from BluetoothDevice.
 * @param device Instance of BluetoothDevice
 * @return Instance of DeviceContainer/*  ww  w. j  a  va  2 s  . c o m*/
 */
private DeviceContainer createContainer(final BluetoothDevice device) {
    DeviceContainer container = new DeviceContainer();
    container.setName(device.getName());
    container.setAddress(device.getAddress());
    return container;
}

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

private void btDeviceClicked(String clickedAddress) {
    if (mBtPairer != null && !mBtPairer.isInProgress()) {
        if (mBtPairer.getStatus() == InputPairer.STATUS_WAITING_TO_PAIR
                && mBtPairer.getTargetDevice() != null) {
            cancelBtPairing();//ww  w. j  a v  a  2 s  .c o  m
        } else {
            if (DEBUG) {
                Log.d(TAG, "Looking for " + clickedAddress + " in available devices to start pairing");
            }
            for (BluetoothDevice target : mBtDevices) {
                if (target.getAddress().equalsIgnoreCase(clickedAddress)) {
                    if (DEBUG) {
                        Log.d(TAG, "Found it!");
                    }
                    mCancelledAddress = ADDRESS_NONE;
                    setPairingBluetooth(true);
                    mBtPairer.startPairing(target);
                    break;
                }
            }
        }
    }
}

From source file:top.isense.demo.testsensor.DeviceScanActivity.java

private void initial_naviview(Activity act) {
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    //setSupportActionBar(toolbar);

    mListView = (ListView) findViewById(R.id.listView);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override//from  w  ww .  j  ava 2 s  .c  om
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG).setAction("Action", null)
                    .show();
        }
    });

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
            R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    if (null != mListView) {
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                //protected void onListItemClick(ListView l, View v, int position, long id) {
                final BluetoothDevice device = mLeDeviceListAdapter.getDevice(position);
                if (device == null)
                    return;
                final Intent intent = new Intent(parent.getContext(), 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.amti.vela.bluetoothlegatt.bluetooth.DeviceScanActivity.java

void initGui() {
    leScanHandler = new Handler();

    //widgets//from  ww  w .  j a va2  s .  c  o m
    deviceListView = (ListView) findViewById(R.id.listView);
    swipeContainer = (SwipeRefreshLayout) findViewById(R.id.swipeContainer);

    mLeDevicesList = new ArrayList<BluetoothDevice>();
    devicesAdapter = new CustomListAdapter(this, R.layout.custom_listview_item);
    deviceListView.setAdapter(devicesAdapter);

    // Configure the refreshing colors
    swipeContainer.setColorSchemeResources(R.color.action_bar_light_blue, R.color.swipe_refresh_dark_green,
            android.R.color.holo_orange_light, android.R.color.holo_red_light);

    notificationEnableDialog = new AlertDialog.Builder(DeviceScanActivity.this);
    notificationEnableDialog.setMessage("This app wants to enable notification access in the settings app.")
            .setPositiveButton("OK", notificationDialogClickListener)
            .setNegativeButton("Cancel", notificationDialogClickListener).setCancelable(false);

    //action bar
    Toolbar actionBarToolBar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(actionBarToolBar);
    actionBarToolBar.setTitle("Choose a Device");
    actionBarToolBar.setTitleTextColor(ContextCompat.getColor(this, R.color.action_bar_text_gray));

    //status bar color
    Window window = getWindow();
    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        window.setStatusBarColor(ContextCompat.getColor(this, R.color.action_bar_dark_blue));
    }

    deviceListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapter, View v, int position, long arg3) {
            BluetoothDevice device = mLeDevicesList.get(position);
            if (device == null)
                return;
            final Intent intent = new Intent(DeviceScanActivity.this, MainActivity.class);
            String deviceName = device.getName() == null ? "Unknown Device" : device.getName();
            intent.putExtra(MainActivity.EXTRAS_DEVICE, deviceName + "\n" + device.getAddress());
            if (mScanning) {
                scanLeDevice(false);
                mScanning = false;
            }
            if (notificationListenerInit())
                ;
            startActivity(intent);
        }
    });

    swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            // Your code to refresh the list here.
            // Make sure you call swipeContainer.setRefreshing(false)
            // once the bt request has completed successfully
            clearDevices();
            scanLeDevice(true);
        }
    });
}

From source file:org.bcsphere.bluetooth.BluetoothG43plus.java

private void startScanManage(BluetoothDevice device, int rssi, byte[] scanRecord) {
    JSONObject obj = new JSONObject();
    Tools.addProperty(obj, Tools.DEVICE_ADDRESS, device.getAddress());
    Tools.addProperty(obj, Tools.DEVICE_NAME, device.getName());
    Tools.addProperty(obj, Tools.IS_CONNECTED, Tools.IS_FALSE);
    Tools.addProperty(obj, Tools.RSSI, rssi);
    Tools.addProperty(obj, Tools.ADVERTISEMENT_DATA, Tools.decodeAdvData(scanRecord));
    Tools.addProperty(obj, Tools.TYPE, "BLE");
    PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, obj);
    pluginResult.setKeepCallback(true);/*w  ww  .ja va 2  s . c  o  m*/
    addEventListenerCC.get(Tools.NEW_ADV_PACKET).sendPluginResult(pluginResult);
}

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

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
    Log.d(LOG_TAG, "Plugin Called");
    this.callbackContext = callbackContext;
    PluginResult result = null;/*from w  w w .  jav  a  2 s  .  c  om*/

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

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

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

            found_devices.clear();
            discovering = true;

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

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

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

    } else if (ACTION_IS_BT_ENABLED.equals(action)) {
        try {
            Log.d(LOG_TAG, "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(PluginResult.Status.OK, isEnabled);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_IS_BT_ENABLED, "Got Exception " + Ex.getMessage());
            result = new PluginResult(PluginResult.Status.ERROR);
        }

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

            boolean enabled = false;

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

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

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

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

            boolean disabled = false;

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

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

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

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

            String addressDevice = args.getString(0);

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

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

            Log.d(LOG_TAG, "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(PluginResult.Status.OK, paired);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_PAIR_BT, "Got Exception " + Ex.getMessage());
            result = new PluginResult(PluginResult.Status.ERROR);
        }

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

            String addressDevice = args.getString(0);

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

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

            Log.d(LOG_TAG, "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(PluginResult.Status.OK, unpaired);
        } catch (Exception Ex) {
            Log.d("BluetoothPlugin - " + ACTION_UNPAIR_BT, "Got Exception " + Ex.getMessage());
            result = new PluginResult(PluginResult.Status.ERROR);
        }

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

            Log.d(LOG_TAG, "Getting paired devices...");
            Set<BluetoothDevice> pairedDevices = btadapter.getBondedDevices();
            int count = 0;
            String resultBoundDevices = "[ ";
            if (pairedDevices.size() > 0) {
                for (BluetoothDevice device : pairedDevices) {
                    Log.i(LOG_TAG, 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(LOG_TAG, device.getName() + " Problems retrieving attributes. Device not added ");
                    count++;
                }

            }

            resultBoundDevices = resultBoundDevices + "] ";

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

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

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

            boolean stopped = true;

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

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

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

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

            boolean state = false;

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

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

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

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

        /*
              } else if ( ACTION_READ.equals(action) )  {
                 final int socketId = args.getInt(0);
                 final int bufferSize = args.getInt(1);
                 this.callback_read = callbackContext;
                 ReadThread readThread = new ReadThread(
                       m_sockets.get(socketId),socketId,bufferSize);
                 readThread.start();
                 m_readThreads.add(readThread);
                 PluginResult pluginResult = new PluginResult(
                       PluginResult.Status.NO_RESULT);
                 pluginResult.setKeepCallback(true);
                 callbackContext.sendPluginResult(pluginResult);
                 return true;
        */

    } else {
        result = new PluginResult(PluginResult.Status.INVALID_ACTION);
        Log.d(LOG_TAG, "Invalid action : " + action + " passed");
    }
    this.callbackContext.sendPluginResult(result);
    return true;
}