public Set<BluetoothDevice> getPairedDevices() { Set<BluetoothDevice> devices = new HashSet<>(); if (getDefaultAdapter() != null && getDefaultAdapter().isEnabled()) { devices = getDefaultAdapter().getBondedDevices(); } return devices; }
public void startDiscovery() { if (getDefaultAdapter() != null) { if (getDefaultAdapter().isDiscovering()) { getDefaultAdapter().cancelDiscovery(); } Log.i(TAG, "Starting Bluetooth discovery"); getDefaultAdapter().startDiscovery(); } }
public BluetoothServerSocket listen() { BluetoothServerSocket tmp = null; try { // TODO use an OpenXC-specific UUID tmp = getDefaultAdapter().listenUsingRfcommWithServiceRecord( "TODO", DeviceManager.RFCOMM_UUID); } catch (IOException e) { } return tmp; }
public BluetoothDevice getLastConnectedDevice() { SharedPreferences preferences = mContext.getSharedPreferences(KNOWN_BLUETOOTH_DEVICE_PREFERENCES, Context.MODE_MULTI_PROCESS); String lastConnectedDeviceAddress = preferences.getString( LAST_CONNECTED_BLUETOOTH_DEVICE_PREF_KEY, null); BluetoothDevice lastConnectedDevice = null; if (lastConnectedDeviceAddress != null) { lastConnectedDevice = getDefaultAdapter().getRemoteDevice(lastConnectedDeviceAddress); } return lastConnectedDevice; }
/** * The DeviceManager requires an Android Context in order to send the intent * to enable Bluetooth if it isn't already on. */ public DeviceManager(Context context) throws BluetoothException { mContext = context; if (getDefaultAdapter() == null) { String message = "This device most likely does not have " + "a Bluetooth adapter"; Log.w(TAG, message); throw new BluetoothException(message); } else { Log.d(TAG, "Initializing Bluetooth device manager"); } }
/** * Connect to the target device and open a socket. This method will block * while waiting for the device. * <p> * Returns a socket connected to the device. */ public BluetoothSocket connect(String targetAddress) throws BluetoothException { return connect(getDefaultAdapter().getRemoteDevice(targetAddress)); }
public boolean isBLEDevice(String address) { return getDefaultAdapter(). getRemoteDevice(address). getType() == BluetoothDevice.DEVICE_TYPE_LE; }
public BluetoothGatt connectBLE(String address) throws BluetoothException { return connectBLE(getDefaultAdapter().getRemoteDevice(address)); }
/** * Immediately cancel any pending Bluetooth operations. * <p> * The BluetoothSocket.connect() function blocks while waiting for a * connection, but it's thread safe and we can cancel that by calling * close() on it at any time. * <p> * Importantly we don't want to close the socket any other time, because we * want to leave that up to the user of the socket - if you call close() * twice, or close Input/Output streams associated with the socket * simultaneously, it can cause a segfault due to a bug in some Android * Bluetooth stacks. Awesome! */ public void stop() { if (mSocketConnecting.get() && mSocket != null) { try { mSocket.close(); } catch (IOException e) { } } if (mSocketConnecting.get() && mBluetoothGatt != null) { mBluetoothGatt.close(); } if (getDefaultAdapter() != null) { getDefaultAdapter().cancelDiscovery(); } }
private void connectToSocket(BluetoothSocket socket) throws BluetoothException { mSocketConnecting.set(true); try { socket.connect(); if (getDefaultAdapter().isDiscovering()) { getDefaultAdapter().cancelDiscovery(); } } catch (IOException e) { String error = "Could not connect to SPP service on " + socket; Log.e(TAG, error); try { socket.close(); } catch (IOException e2) { } throw new BluetoothException(error, e); } finally { mSocketConnecting.set(false); } }
public Set<BluetoothDevice> getCandidateDevices() { Set<BluetoothDevice> candidates = new HashSet<>(); for (BluetoothDevice device : getPairedDevices()) { if (device.getName().startsWith( BluetoothVehicleInterface.DEVICE_NAME_PREFIX)) { candidates.add(device); } } SharedPreferences preferences = mContext.getSharedPreferences(KNOWN_BLUETOOTH_DEVICE_PREFERENCES, Context.MODE_MULTI_PROCESS); Set<String> detectedDevices = SupportSettingsUtils.getStringSet( preferences, KNOWN_BLUETOOTH_DEVICE_PREF_KEY, new HashSet<String>()); for (String address : detectedDevices) { if (BluetoothAdapter.checkBluetoothAddress(address)) { candidates.add(getDefaultAdapter().getRemoteDevice(address)); } } for (BluetoothDevice candidate : candidates) { Log.d(TAG, "Found previously discovered or paired OpenXC BT VI " + candidate.getAddress()); } return candidates; }
public BluetoothGatt connectBLE(BluetoothDevice device) throws BluetoothException { if (device == null) { throw new BluetoothException("Not connecting to null Bluetooth device"); } mSocketConnecting.set(true); try { mGattCallback = new GattCallback(); mBluetoothGatt = device.connectGatt(mContext, true, mGattCallback); mBluetoothGatt.connect(); mGattCallback.setBluetoothGatt(mBluetoothGatt); Log.d(TAG, "Connected to gatt " + mBluetoothGatt); if (getDefaultAdapter().isDiscovering()) { getDefaultAdapter().cancelDiscovery(); } } catch (IllegalArgumentException e) { String error = "Unable to connect to GATT Server on BLE device " + device; Log.w(TAG, error); mBluetoothGatt.disconnect(); throw new BluetoothException(error, e); } finally { mSocketConnecting.set(false); } //storeLastConnectedDevice(device); return mBluetoothGatt; }