List of usage examples for android.bluetooth BluetoothDevice getName
@RequiresPermission(Manifest.permission.BLUETOOTH)
public String getName()
From source file:com.github.akinaru.bleanalyzer.bluetooth.BluetoothCustomManager.java
/** * Connect to device's GATT server//from w ww . ja v a 2 s. co m */ @SuppressLint("NewApi") public boolean connect(String address) { if (mBluetoothAdapter == null || address == null) { Log.w(TAG, "BluetoothAdapter not initialized or unspecified address."); return false; } BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); boolean alreadyInList = false; if (bluetoothConnectionList.containsKey(address)) { alreadyInList = true; } if (alreadyInList) { Log.i(TAG, "reusing same connection"); BluetoothDeviceConn conn = (BluetoothDeviceConn) bluetoothConnectionList.get(address); conn.setGatt(device.connectGatt(context, false, conn.getGattCallback())); } else { BluetoothDeviceConn conn = new BluetoothDeviceConn(address, device.getName(), this); bluetoothConnectionList.put(address, conn); Log.i(TAG, "new connection"); //connect to gatt server on the device conn.setGatt(device.connectGatt(context, false, conn.getGattCallback())); } return true; }
From source file:de.bogutzky.psychophysiocollector.app.MainActivity.java
private void connectBioHarness() { if (bluetoothAdapter == null) bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices(); if (pairedDevices.size() > 0) { for (BluetoothDevice device : pairedDevices) { if (device.getName().startsWith("BH")) { if (bluetoothAddresses.contains(device.getAddress())) { if (bioHarnessService != null) { bioHarnessService.connectBioHarness(device.getAddress(), new BioHarnessHandler(this, new int[] { 100, 1000 })); }//from w w w .ja v a 2 s .c o m } } } } }
From source file:de.bogutzky.psychophysiocollector.app.MainActivity.java
private void connectAllShimmerImus() { if (bluetoothAdapter == null) bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices(); int count = 0; if (pairedDevices.size() > 0) { for (BluetoothDevice device : pairedDevices) { if (device.getName().contains("RN42")) { if (bluetoothAddresses.contains(device.getAddress())) { if (shimmerImuService != null) { shimmerImuService.connectShimmerImu(device.getAddress(), Integer.toString(count), new ShimmerImuHandler(this, "imu-" + device.getName().toLowerCase() + ".csv", 2500)); count++;//from w ww. ja va 2 s. c om } } } } } }
From source file:org.protocoderrunner.apprunner.api.PNetwork.java
@ProtocoderScript @APIMethod(description = "Send a bluetooth serial message", example = "") @APIParam(params = { "string" }) public NativeArray getBluetoothBondedDevices() { startBluetooth();/*w w w.j a v a2s . co m*/ Set<BluetoothDevice> listDevices = simpleBT.listBondedDevices(); MLog.d(TAG, "listDevices " + listDevices); int listSize = listDevices.size(); ProtocoderNativeArray array = new ProtocoderNativeArray(listSize); MLog.d(TAG, "array " + array); int counter = 0; for (BluetoothDevice b : listDevices) { MLog.d(TAG, "b " + b); String s = b.getName() + " " + b.getAddress(); array.addPE(counter++, s); } return array; }
From source file:com.android.dragonkeyboardfirmwareupdater.KeyboardFirmwareUpdateService.java
private boolean isTargetKeyboard(BluetoothDevice device) { return (device != null && getString(R.string.target_keyboard_name).equals(device.getName())); }
From source file:org.bcsphere.bluetooth.BluetoothSam42.java
@Override public void getConnectedDevices(JSONArray json, CallbackContext callbackContext) { Log.i(TAG, "getConnectedDevices"); if (!isInitialized(callbackContext)) { return;//from ww w.ja v a2 s .com } @SuppressWarnings("unchecked") List<BluetoothDevice> bluetoothDevices = bluetoothGatt.getConnectedDevices(); JSONArray jsonDevices = new JSONArray(); for (BluetoothDevice device : bluetoothDevices) { JSONObject jsonDevice = new JSONObject(); Tools.addProperty(jsonDevice, Tools.DEVICE_ADDRESS, device.getAddress()); Tools.addProperty(jsonDevice, Tools.DEVICE_NAME, device.getName()); jsonDevices.put(jsonDevice); } callbackContext.success(jsonDevices); }
From source file:de.bogutzky.psychophysiocollector.app.MainActivity.java
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case REQUEST_ENABLE_BT: if (resultCode == RESULT_OK) { Toast.makeText(MainActivity.this, getString(R.string.bluetooth_activated), Toast.LENGTH_LONG) .show();//from w w w . ja v a2s . c om } break; case PERMISSIONS_REQUEST: if (resultCode == RESULT_OK) { Toast.makeText(MainActivity.this, getString(R.string.permission_granted), Toast.LENGTH_LONG).show(); } break; case MSG_BLUETOOTH_ADDRESS: if (resultCode == Activity.RESULT_OK) { String bluetoothAddress = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS); Log.d(TAG, "Bluetooth address: " + bluetoothAddress); // Check, if device is list view if (!getBluetoothAddresses().contains(bluetoothAddress)) { Log.d(TAG, "Bluetooth address of a new device"); addBluetoothAddress(bluetoothAddress); bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); BluetoothDevice device = bluetoothAdapter.getRemoteDevice(bluetoothAddress); String deviceName = device.getName(); if (deviceName == null) { Log.d(TAG, "Device has no device name"); deviceName = bluetoothAddress + ""; } // Change list view deviceNames.add(deviceName); arrayAdapter.notifyDataSetChanged(); // Check, if device is paired Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices(); boolean paired = false; for (BluetoothDevice pairedDevice : pairedDevices) { if (pairedDevice.getAddress().equals(bluetoothAddress)) { paired = true; Log.d(TAG, "Device already paired"); } } if (!paired) { pairBluetoothDevice(device); } if (device.getName().startsWith("RN42") & shimmerImuService == null) { Intent intent = new Intent(this, ShimmerImuService.class); startService(intent); getApplicationContext().bindService(intent, shimmerImuServiceConnection, Context.BIND_AUTO_CREATE); registerReceiver(shimmerImuReceiver, new IntentFilter("de.bogutzky.data_collector.app")); } if (device.getName().startsWith("BH") & bioHarnessService == null) { Intent intent = new Intent(this, BioHarnessService.class); startService(intent); getApplicationContext().bindService(intent, bioHarnessServiceConnection, Context.BIND_AUTO_CREATE); registerReceiver(bioHarnessReceiver, new IntentFilter("de.bogutzky.data_collector.app")); } } else { Toast.makeText(this, getString(R.string.device_is_already_in_list), Toast.LENGTH_LONG).show(); } } break; case REQUEST_MAIN_COMMAND_SHIMMER: if (resultCode == Activity.RESULT_OK) { int action = data.getIntExtra("action", 0); String bluetoothDeviceAddress = data.getStringExtra("bluetoothDeviceAddress"); int which = data.getIntExtra("which", 0); if (action == 13) { showGraph(bluetoothDeviceAddress, REQUEST_MAIN_COMMAND_SHIMMER, which); } } break; case REQUEST_MAIN_COMMAND_BIOHARNESS: if (resultCode == Activity.RESULT_OK) { int action = data.getIntExtra("action", 0); String bluetoothDeviceAddress = data.getStringExtra("bluetoothDeviceAddress"); if (action == 13) { showGraph(bluetoothDeviceAddress, REQUEST_MAIN_COMMAND_BIOHARNESS, -1); } } break; } }
From source file:obdii.starter.automotive.iot.ibm.com.iot4a_obdii.Home.java
private void runRealObdBluetoothScan(final int obd_timeout_ms, final ObdProtocols obd_protocol) { if (!obdBridgeBluetooth.isBluetoothEnabled()) { final Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBluetooth, BLUETOOTH_REQUEST); return;//ww w . j a v a 2 s . com } final Set<BluetoothDevice> pairedDevicesSet = obdBridgeBluetooth.getPairedDeviceSet(); // In case user clicks on Change Network, need to repopulate the devices list final ArrayList<String> deviceNames = new ArrayList<>(); final ArrayList<String> deviceAddresses = new ArrayList<>(); if (pairedDevicesSet != null && pairedDevicesSet.size() > 0) { for (BluetoothDevice device : pairedDevicesSet) { deviceNames.add(device.getName()); deviceAddresses.add(device.getAddress()); } final String preferredName = getPreference(SettingsFragment.BLUETOOTH_DEVICE_NAME, "obd").toLowerCase(); final AlertDialog.Builder alertDialog = new AlertDialog.Builder(Home.this, R.style.AppCompatAlertDialogStyle); final ArrayAdapter adapter = new ArrayAdapter(Home.this, android.R.layout.select_dialog_singlechoice, deviceNames.toArray(new String[deviceNames.size()])); int selectedDevice = -1; for (int i = 0; i < deviceNames.size(); i++) { if (deviceNames.get(i).toLowerCase().contains(preferredName)) { selectedDevice = i; } } alertDialog.setCancelable(false).setSingleChoiceItems(adapter, selectedDevice, null) .setTitle("Please Choose the OBDII Bluetooth Device") .setPositiveButton("Ok", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); final int position = ((AlertDialog) dialog).getListView().getCheckedItemPosition(); final String deviceAddress = deviceAddresses.get(position); final String deviceName = deviceNames.get(position); startConnectingBluetoothDevice(deviceAddress, deviceName, obd_timeout_ms, obd_protocol); setPreference(SettingsFragment.BLUETOOTH_DEVICE_NAME, deviceName); } }).show(); } else { Toast.makeText(getApplicationContext(), "Please pair with your OBDII device and restart the application!", Toast.LENGTH_SHORT).show(); } }
From source file:com.android.dragonkeyboardfirmwareupdater.KeyboardFirmwareUpdateService.java
private boolean enableBluetoothConnectivity() { Log.d(TAG, "EnableBluetoothConnectivity"); if (mBluetoothManager == null) { mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); if (mBluetoothManager == null) { Log.w(TAG, "EnableBluetoothConnectivity: Failed to obtain BluetoothManager"); return false; }/*from ww w . ja v a2 s. c om*/ } mBluetoothAdapter = mBluetoothManager.getAdapter(); if (mBluetoothAdapter == null) { Log.w(TAG, "EnableBluetoothConnectivity: Failed to obtain BluetoothAdapter"); return false; } mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner(); if (mBluetoothLeScanner == null) { Log.w(TAG, "EnableBluetoothConnectivity: Failed to obtain BluetoothLeScanner"); return false; } // The first auto-connection after boot might be missed due to starting time of the updater service. List<BluetoothDevice> connectedDevices = mBluetoothManager.getConnectedDevices(BluetoothProfile.GATT); for (BluetoothDevice device : connectedDevices) { if (isTargetKeyboard(device) && !isUpdateServiceInUse()) { Log.d(TAG, "enableBluetoothConnectivity: Found keyboard " + device.getName() + " [" + device.getAddress() + "] connected"); obtainKeyboardInfo(device.getName(), device.getAddress()); break; } } return true; }
From source file:com.netlinks.healthbracelet.service.HeartRateService.java
/** * Start the ConnectedThread to begin managing a Bluetooth connection * @param socket The BluetoothSocket on which the connection was made * @param device The BluetoothDevice that has been connected *///from w w w .j a v a 2 s . c o m public synchronized void connected(BluetoothSocket socket, BluetoothDevice device) { if (D) Log.d(TAG, "connected"); // Cancel the thread that completed the connection 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 manage the connection and perform transmissions mConnectedThread = new ConnectedThread(socket); mConnectedThread.start(); // Send the name of the connected device back to the UI Activity Message msg = mHandler.obtainMessage(MESSAGE_DEVICE_NAME); Bundle bundle = new Bundle(); bundle.putString(DEVICE_NAME, device.getName()); msg.setData(bundle); mHandler.sendMessage(msg); setState(STATE_CONNECTED); }