/** * 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"); } }
/** * Open an RFCOMM socket to the Bluetooth device. * <p> * The device may or may not actually exist, the argument is just a * reference to it. */ private BluetoothSocket setupSocket(BluetoothDevice device) throws BluetoothException { if (device == null) { Log.w(TAG, "Can't setup socket -- device is null"); throw new BluetoothException(); } Log.d(TAG, "Scanning services on " + device); BluetoothSocket socket; try { socket = device.createRfcommSocketToServiceRecord(RFCOMM_UUID); } catch (IOException e) { String error = "Unable to open a socket to device " + device; Log.w(TAG, error); throw new BluetoothException(error, e); } return socket; }
private void connectStreams() throws BluetoothException { mConnectionLock.writeLock().lock(); try { if (!connectingToBLE) { try { mOutStream = new BufferedWriter(new OutputStreamWriter( mSocket.getOutputStream())); mInStream = new BufferedInputStream(mSocket.getInputStream()); Log.i(TAG, "Socket stream to vehicle interface " + "opened successfully"); } catch (IOException e) { Log.e(TAG, "Error opening streams ", e); disconnect(); throw new BluetoothException(); } } else { mBLEInputStream = BLEInputStream.getInstance(); mBLEInputStream.clearBuffer(); mInStream = new BufferedInputStream(mBLEInputStream); Log.i(TAG, "Input stream to vehicle interface " + "opened successfully"); } } finally { mConnectionLock.writeLock().unlock(); } }
public BluetoothSocket connect(BluetoothDevice device) throws BluetoothException { if (device == null) { throw new BluetoothException("Not connecting to null Bluetooth device"); } mSocket = setupSocket(device); connectToSocket(mSocket); storeLastConnectedDevice(device); return mSocket; }
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 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; }