List of usage examples for android.bluetooth BluetoothDevice getName
@RequiresPermission(Manifest.permission.BLUETOOTH)
public String getName()
From source file:org.altusmetrum.AltosDroid.AltosDroid.java
private void connectDevice(Intent data) { // Get the device MAC address String address = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS); // Get the BLuetoothDevice object BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); // Attempt to connect to the device try {/*from ww w. j a v a2 s . c o m*/ if (D) Log.d(TAG, "Connecting to " + device.getName()); mService.send(Message.obtain(null, TelemetryService.MSG_CONNECT, device)); } catch (RemoteException e) { } }
From source file:com.amti.vela.bluetoothlegatt.bluetooth.DeviceScanActivity.java
void initGui() { leScanHandler = new Handler(); //widgets// w w w .j av a 2 s . co 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: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 ww w .j a v 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()); Preferences.writeDeviceName(getApplicationContext(), device.getName()); Preferences.writeDeviceAddress(getApplicationContext(), device.getAddress()); if (mScanning) { mBluetoothAdapter.stopLeScan(mLeScanCallback); mScanning = false; } startActivity(intent); }
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/*w w w. java 2 s . c o m*/ */ private DeviceContainer createContainer(final BluetoothDevice device) { DeviceContainer container = new DeviceContainer(); container.setName(device.getName(), device.getAddress()); return container; }
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 .ja va2s. co 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.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;/*w ww. j a v a 2s . 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.phonegap.plugin.bluetooth.BluetoothPlugin.java
@Override public PluginResult execute(String action, JSONArray arg1, String callbackId) { Log.d("BluetoothPlugin", "Plugin Called"); PluginResult result = null;/*from w ww. jav a 2s .c om*/ 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; }
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 ava 2 s . c o m*/ 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: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 . j a v a 2s . 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: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 av a 2 s.co m 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); //} } }); } }