List of usage examples for android.bluetooth BluetoothDevice getName
@RequiresPermission(Manifest.permission.BLUETOOTH)
public String getName()
From source file:com.example.nachiketvatkar.locateus.BluetoothPairing.java
private void showChooseDeviceServiceDialog(final BluetoothDevice device) { // Prepare dialog AlertDialog.Builder builder = new AlertDialog.Builder(this); String deviceName = device.getName(); String title = String.format(getString(R.string.scan_connectto_dialog_title_format), deviceName != null ? deviceName : device.getAddress()); String[] items = new String[kComponentsNameIds.length]; for (int i = 0; i < kComponentsNameIds.length; i++) { items[i] = getString(kComponentsNameIds[i]); }/* w w w . java 2s. c o m*/ builder.setTitle(title).setItems(items, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { switch (kComponentsNameIds[which]) { case R.string.scan_connectservice_info: { // Info // mComponentToStartWhenConnected = InfoActivity.class; break; } case R.string.scan_connectservice_uart: { // Uart // mComponentToStartWhenConnected = UartActivity.class; break; } case R.string.scan_connectservice_pinio: { // PinIO // mComponentToStartWhenConnected = PinIOActivity.class; break; } case R.string.scan_connectservice_controller: { // Controller // mComponentToStartWhenConnected = ControllerActivity.class; break; } case R.string.scan_connectservice_beacon: { // Beacon // mComponentToStartWhenConnected = BeaconActivity.class; break; } } if (mComponentToStartWhenConnected != null) { connect(device);// First connect to the device, and when connected go to selected activity } } }); // Show dialog AlertDialog dialog = builder.create(); dialog.show(); }
From source file:com.cs4644.vt.theonering.MainActivity.java
private void startScan(final UUID[] servicesToScan, final String deviceNameToScanFor) { Log.d(TAG, "startScan"); // Stop current scanning (if needed) stopScanning();//w w w. j a v a 2 s . c o m // Configure scanning BluetoothAdapter bluetoothAdapter = BleUtils.getBluetoothAdapter(getApplicationContext()); if (BleUtils.getBleStatus(this) != BleUtils.STATUS_BLE_ENABLED) { Log.w(TAG, "startScan: BluetoothAdapter not initialized or unspecified address."); } else { mScanner = new BleDevicesScanner(bluetoothAdapter, servicesToScan, new BluetoothAdapter.LeScanCallback() { @Override public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) { final String deviceName = device.getName(); Log.d(TAG, "Discovered device: " + (deviceName != null ? deviceName : "<unknown>")); BluetoothDeviceData previouslyScannedDeviceData = null; if (deviceNameToScanFor == null || (deviceName != null && deviceName.equalsIgnoreCase(deviceNameToScanFor))) { // Workaround for bug in service discovery. Discovery filtered by service uuid is not working on Android 4.3, 4.4 if (mScannedDevices == null) mScannedDevices = new ArrayList<>(); // Safeguard // Check that the device was not previously found for (BluetoothDeviceData deviceData : mScannedDevices) { if (deviceData.device.getAddress().equals(device.getAddress())) { previouslyScannedDeviceData = deviceData; break; } } BluetoothDeviceData deviceData; if (previouslyScannedDeviceData == null) { // Add it to the mScannedDevice list deviceData = new BluetoothDeviceData(); mScannedDevices.add(deviceData); } else { deviceData = previouslyScannedDeviceData; } deviceData.device = device; deviceData.rssi = rssi; deviceData.scanRecord = scanRecord; decodeScanRecords(deviceData); // Update device data long currentMillis = SystemClock.uptimeMillis(); if (previouslyScannedDeviceData == null || currentMillis - mLastUpdateMillis > kMinDelayToUpdateUI) { // Avoid updating when not a new device has been found and the time from the last update is really short to avoid updating UI so fast that it will become unresponsive mLastUpdateMillis = currentMillis; runOnUiThread(new Runnable() { @Override public void run() { updateUI(); } }); } } } }); // Start scanning mScanner.start(); } // Update UI updateUI(); }
From source file:com.cs4644.vt.theonering.MainActivity.java
private void showChooseDeviceServiceDialog(final BluetoothDevice device) { // Prepare dialog AlertDialog.Builder builder = new AlertDialog.Builder(this); String deviceName = device.getName(); String title = String.format(getString(R.string.scan_connectto_dialog_title_format), deviceName != null ? deviceName : device.getAddress()); String[] items = new String[kComponentsNameIds.length]; for (int i = 0; i < kComponentsNameIds.length; i++) items[i] = getString(kComponentsNameIds[i]); builder.setTitle(title).setItems(items, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { switch (kComponentsNameIds[which]) { // case R.string.scan_connectservice_info: { // Info // mComponentToStartWhenConnected = InfoActivity.class; // break; // } case R.string.scan_connectservice_uart: { // Uart // mComponentToStartWhenConnected = UartActivity.class; mComponentToStartWhenConnected = ControllerActivity.class; break; }/*from w ww. j a v a2 s . c om*/ // case R.string.scan_connectservice_pinio: { // PinIO // mComponentToStartWhenConnected = PinIOActivity.class; // break; // } // case R.string.scan_connectservice_controller: { // Controller // mComponentToStartWhenConnected = adafruit.bluefruit.le.connect.app.ControllerActivity.class; // break; // } // case R.string.scan_connectservice_beacon: { // Beacon // mComponentToStartWhenConnected = BeaconActivity.class; // break; // } } if (mComponentToStartWhenConnected != null) { connect(device); // First connect to the device, and when connected go to selected activity } } }); // Show dialog AlertDialog dialog = builder.create(); dialog.show(); }
From source file:com.example.bikey.BluetoothActivity.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 www .ja v a 2 s.co 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; } // Cancel the accept thread because we only want to connect to one device if (mAcceptThread != null) { mAcceptThread.cancel(); mAcceptThread = 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(LockPage.MESSAGE_DEVICE_NAME); Bundle bundle = new Bundle(); bundle.putString(LockPage.DEVICE_NAME, device.getName()); msg.setData(bundle); mHandler.sendMessage(msg); setState(STATE_CONNECTED); }
From source file:com.cypress.cysmart.CommonFragments.ProfileScanningFragment.java
/** * Method to connect to the device selected. The time allotted for having a * connection is 8 seconds. After 8 seconds it will disconnect if not * connected and initiate scan once more * * @param device/*from w w w. j a va2 s .co m*/ */ void connectDevice(BluetoothDevice device) { // Register the broadcast receiver for connection status if (!receiverEnabled) { Logger.e("Registering receiver some how "); getActivity().registerReceiver(mGattUpdateReceiver, Utils.makeGattUpdateIntentFilter()); receiverEnabled = true; } mpdia.setTitle(getResources().getString(R.string.alert_message_connect_title)); mpdia.setMessage(getResources().getString(R.string.alert_message_connect) + "\n" + device.getName() + "\n" + device.getAddress() + "\n" + getResources().getString(R.string.alert_message_wait)); if (!getActivity().isDestroyed() && mpdia != null) { mpdia.show(); } mDeviceAddress = device.getAddress(); mDeviceName = device.getName(); // Get the connection status of the device if (BluetoothLeService.getConnectionState() == BluetoothLeService.STATE_DISCONNECTED) { Logger.i("BluetoothLeService.getConnectionState()--->" + BluetoothLeService.getConnectionState()); // Disconnected,so connect HANDLER_FLAG = true; BluetoothLeService.connect(mDeviceAddress, mDeviceName, getActivity()); } else { Logger.i("BluetoothLeService.getConnectionState()--->" + BluetoothLeService.getConnectionState()); // Connecting to some devices,so disconnect and then connect BluetoothLeService.disconnect(); HANDLER_FLAG = true; BluetoothLeService.connect(mDeviceAddress, mDeviceName, getActivity()); } mConnectHandler.postDelayed(new Runnable() { @Override public void run() { Logger.e("Connect handler called"); if (HANDLER_FLAG) { mpdia.dismiss(); BluetoothLeService.disconnect(); try { Toast.makeText(getActivity(), R.string.profile_control_delay_message, Toast.LENGTH_SHORT) .show(); if (mLeDeviceListAdapter != null) mLeDeviceListAdapter.clear(); if (mLeDeviceListAdapter != null) { try { mLeDeviceListAdapter.notifyDataSetChanged(); } catch (Exception e) { e.printStackTrace(); } } // mswipeLayout.setVisibility(View.INVISIBLE); // mNoDeviceFound.setVisibility(View.VISIBLE); scanLeDevice(true); mScanning = true; } catch (Exception e) { e.printStackTrace(); } } } }, 10000); }
From source file:com.nbplus.iotapp.bluetooth.BluetoothLeService.java
/** * for log.//from ww w . j av a 2s . c o m * @param device * @param adRecords */ private void printScanDevices(BluetoothDevice device, HashMap<Integer, AdRecord> adRecords) { Log.d(TAG, "onLeScan() ============================================="); Log.d(TAG, "onLeScan: uuid:" + (device.getUuids() != null ? device.getUuids().toString() : "null") + ", name = " + device.getName()); Log.d(TAG, "onLeScan: address:" + device.getAddress()); Log.d(TAG, "onLeScan: bluetooth class:" + device.getBluetoothClass()); Log.d(TAG, "onLeScan: type:" + device.getType()); String str = ""; byte[] values; for (Map.Entry<Integer, AdRecord> entry : adRecords.entrySet()) { Integer type = entry.getKey(); AdRecord adRecord = entry.getValue(); if (adRecord != null) { switch (type) { case AdRecord.TYPE_FLAGS: int flags = adRecord.getValue()[0] & 0x0FF; str = ""; if ((flags & 0x01) > 0) { str += "'LE Limited Discoverable Mode' "; } if ((flags & (0x01 << 1)) > 0) { str += "'LE General Discoverable Mode' "; } if ((flags & (0x01 << 2)) > 0) { str += "'BR/EDR Not Supported' "; } if ((flags & (0x01 << 3)) > 0) { str += "'Simultaneous LE and BR/EDR to Same Device Capacble (Controller)' "; } if ((flags & (0x01 << 4)) > 0) { str += "'Simultaneous LE and BR/EDR to Same Device Capacble (Host)' "; } Log.d(TAG, "onLeScan: TYPE_FLAGS = " + str); break; case AdRecord.TYPE_UUID16_INC: case AdRecord.TYPE_UUID16: { ArrayList<String> uuids = DataParser.getUint16StringArray(adRecord.getValue()); int i = 0; for (String uuid : uuids) { Log.d(TAG, "onLeScan: TYPE_UUID16(_INC)[" + (++i) + "] = " + uuid); } break; } case AdRecord.TYPE_UUID32_INC: case AdRecord.TYPE_UUID32: { ArrayList<String> uuids = DataParser.getUint32StringArray(adRecord.getValue()); int i = 0; for (String uuid : uuids) { Log.d(TAG, "onLeScan: TYPE_UUID32(_INC)[" + (++i) + "] = " + uuid); } break; } case AdRecord.TYPE_UUID128_INC: case AdRecord.TYPE_UUID128: { ArrayList<String> uuids = DataParser.getUint128StringArray(adRecord.getValue()); int i = 0; for (String uuid : uuids) { Log.d(TAG, "onLeScan: TYPE_UUID128(_INC)[" + (++i) + "] = " + uuid); } break; } case AdRecord.TYPE_NAME_SHORT: str = DataParser.getString(adRecord.getValue()); Log.d(TAG, "onLeScan: TYPE_NAME_SHORT = " + str); break; case AdRecord.TYPE_NAME: str = DataParser.getString(adRecord.getValue()); Log.d(TAG, "onLeScan: TYPE_NAME = " + str); break; case AdRecord.TYPE_TRANSMITPOWER: Log.d(TAG, "onLeScan: TYPE_TRANSMITPOWER = " + DataParser.getInt8(adRecord.getValue()[0])); break; case AdRecord.TYPE_SERVICEDATA: values = adRecord.getValue(); String uuid = DataParser.getUint16String(Arrays.copyOfRange(values, 0, 2)); Log.d(TAG, "onLeScan: TYPE_SERVICEDATA uuid = " + uuid); str = DataParser.getHexString(Arrays.copyOfRange(values, 2, values.length)); Log.d(TAG, "onLeScan: TYPE_SERVICEDATA hexstringdata = " + str); break; case AdRecord.TYPE_APPEARANCE: str = DataParser.getUint16String(adRecord.getValue()); Log.d(TAG, "onLeScan: TYPE_APPEARANCE = " + str); break; case AdRecord.TYPE_VENDOR_SPECIFIC: values = adRecord.getValue(); // https://www.bluetooth.org/en-us/specification/assigned-numbers/company-identifiers str = DataParser.getUint16String(Arrays.copyOfRange(values, 0, 2)); Log.d(TAG, "onLeScan: TYPE_VENDOR_SPECIFIC company = " + str); if ("004C".equals(str)) { // Apple Inc int offset = 2; int data_type = values[offset++]; int data_length = values[offset++]; if (data_type == 0x02) { // iBeacon // https://www.uncinc.nl/nl/blog/finding-out-the-ibeacons-specifications // http://www.warski.org/blog/2014/01/how-ibeacons-work/ // http://developer.iotdesignshop.com/tutorials/bluetooth-le-and-ibeacon-primer/ // String uuid = parseUUID(this.parseHex(Arrays.copyOfRange(value, offset, offset + 16), true)); // offset += 16; // ad.apple.ibeacon.major = parseHex(Arrays.copyOfRange(value, offset, offset + 2), true); // offset += 2; // ad.apple.ibeacon.minor = parseHex(Arrays.copyOfRange(value, offset, offset + 2), true); // offset += 2; // ad.tx_power = this.parseSignedNumber(value[offset]); } else { // ad.apple.vendor = this.parseHex(Arrays.copyOfRange(value, offset - 2, offset + data_length), true); } } else { // ad.vendor = this.parseHex(Arrays.copyOfRange(value, i, i + len - 1), true); } break; } } } Log.d(TAG, "============================================="); }
From source file:com.example.android.BluetoothSerial.BluetoothSerialService.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 */// w ww . j a va 2 s . com 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; } // Cancel the accept thread because we only want to connect to one device if (mAcceptThread != null) { mAcceptThread.cancel(); mAcceptThread = 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(BluetoothSerial.MESSAGE_DEVICE_NAME); Bundle bundle = new Bundle(); bundle.putString(BluetoothSerial.DEVICE_NAME, device.getName()); msg.setData(bundle); mHandler.sendMessage(msg); setState(STATE_CONNECTED); }
From source file:activeng.pt.activenglab.BluetoothChatService.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 *//* w w w. ja v a 2 s .com*/ public synchronized void connected(BluetoothSocket socket, BluetoothDevice device, final String socketType) { Log.d(TAG, "connected, Socket Type:" + socketType); // 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; } // Cancel the accept thread because we only want to connect to one device if (mSecureAcceptThread != null) { mSecureAcceptThread.cancel(); mSecureAcceptThread = null; } if (mInsecureAcceptThread != null) { mInsecureAcceptThread.cancel(); mInsecureAcceptThread = null; } // Start the thread to manage the connection and perform transmissions mConnectedThread = new ConnectedThread(socket, socketType); mConnectedThread.start(); deviceName = device.getName(); deviceAddress = device.getAddress(); // Send the name of the connected device back to the UI Activity //Message msg = mHandler.obtainMessage(Constants.MESSAGE_DEVICE_NAME); //Bundle bundle = new Bundle(); //bundle.putString(Constants.DEVICE_NAME, device.getName()); //bundle.putString(Constants.DEVICE_ADRESS, device.getAddress()); //msg.setData(bundle); //mHandler.sendMessage(msg); //Intent intent = new Intent(Constants.MESSAGE_BT_NAME).putExtra(Intent.EXTRA_TEXT, device.getName()); //mContext.sendBroadcast(intent); Log.d("ActivEng", "BluetoothChatService BroadcastReceiver versus LocalBroadcastReceiver"); //mContext.getApplicationContext().registerReceiver(this.connectionUpdates, new IntentFilter(Constants.MESSAGE_TO_ARDUINO)); // //LocalBroadcastManager manager = LocalBroadcastManager.getInstance(mContext); //LocalBroadcastManager.getInstance(mContext.getApplicationContext()).registerReceiver(this.connectionUpdates, // new IntentFilter(Constants.MESSAGE_TO_ARDUINO)); LocalBroadcastManager manager = LocalBroadcastManager.getInstance(mContext); IntentFilter filter = new IntentFilter(Constants.MESSAGE_TO_ARDUINO); manager.registerReceiver(this.connectionUpdates, filter); registered = true; setState(Constants.STATE_CONNECTED); }
From source file:com.example.android.BluetoothChat.BluetoothChatService.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 ww w. j av a2 s . co m*/ public synchronized void connected(BluetoothSocket socket, BluetoothDevice device, final String socketType) { if (D) Log.d(TAG, "connected, Socket Type:" + socketType); // 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; } // Cancel the accept thread because we only want to connect to one device if (mSecureAcceptThread != null) { mSecureAcceptThread.cancel(); mSecureAcceptThread = null; } if (mInsecureAcceptThread != null) { mInsecureAcceptThread.cancel(); mInsecureAcceptThread = null; } // Start the thread to manage the connection and perform transmissions mConnectedThread = new ConnectedThread(socket, socketType); mConnectedThread.start(); // Send the name of the connected device back to the UI Activity Message msg = mHandler.obtainMessage(BluetoothChat.MESSAGE_DEVICE_NAME); Bundle bundle = new Bundle(); bundle.putString(BluetoothChat.DEVICE_NAME, device.getName()); msg.setData(bundle); mHandler.sendMessage(msg); setState(STATE_CONNECTED); }
From source file:org.thecongers.mcluster.MainActivity.java
private boolean canBusConnect() { btAdapter = BluetoothAdapter.getDefaultAdapter(); checkBTState();/*from w w w . j a v a2 s .c om*/ if (btAdapter != null) { Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices(); // If there are paired devices if (pairedDevices.size() > 0) { // Loop through paired devices for (BluetoothDevice device : pairedDevices) { if (device.getName().contains("CANBusGateway")) { address = device.getAddress(); Log.d(TAG, "Paired CANBusGateway found: " + device.getName() + " " + device.getAddress()); } } if (address == null) { Toast.makeText(MainActivity.this, getResources().getString(R.string.toast_noPaired), Toast.LENGTH_LONG).show(); return false; } } if (address != null) { // Set up a pointer to the remote node using it's address. BluetoothDevice device = btAdapter.getRemoteDevice(address); canBusConnectThread = new canBusConnectThread(device); canBusConnectThread.start(); } else { Toast.makeText(MainActivity.this, getResources().getString(R.string.toast_noPaired), Toast.LENGTH_LONG).show(); return false; } return true; } Log.d(TAG, "Bluetooth not supported"); return false; }