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 synchronized void setBluetoothPollingStatus(boolean enabled) { if(mVehicleInterface != null && mVehicleInterface instanceof BluetoothVehicleInterface) { ((BluetoothVehicleInterface)mVehicleInterface).setPollingStatus( enabled); } }
/*** * This is used to get the connection status using gatt callback * @return boolean value indicating */ public boolean isBLEConnected() { if (mGattCallback != null) { return mGattCallback.isConnected(); } else { return false; } }
@Override public synchronized void stop() { if (isRunning()) { try { getContext().unregisterReceiver(mBroadcastReceiver); } catch (IllegalArgumentException e) { Log.w(TAG, "Broadcast receiver not registered but we expected it to be"); } mDeviceManager.stop(); closeSocket(); super.stop(); } }
@Override protected int read(byte[] bytes) throws IOException { mConnectionLock.readLock().lock(); int bytesRead = -1; if (connectingToBLE) { bytesRead = 0; } try { if (isConnected() && !connectingToBLE) { bytesRead = mInStream.read(bytes, 0, bytes.length); } else if (isConnected()) { if (mBLEInputStream != null && mBLEInputStream.doesBufferHasRemaining()) { bytesRead = mInStream.read(bytes, 0, bytes.length); } else if (mDeviceManager.isBLEDisconnected()) { //Disconnect BLE if GattCallBack state changed to disconnected mConnectedAddress = null; bytesRead = -1; } } } finally { mConnectionLock.readLock().unlock(); } return bytesRead; }
public void readChangedCharacteristic(BluetoothGattCharacteristic characteristic) { byte[] data; data = characteristic.getValue(); // *** this is going to get overwritten by next call, so make a queue if (data != null && data.length > 0 && (isConnected())) { messageBuffer.append(new String(data, 0, data.length)); if(JsonStreamer.containsJson(messageBuffer.toString())) { int delimiterIndex = messageBuffer.indexOf(DELIMITER); if (delimiterIndex != -1) { byte message[] = messageBuffer.substring(0, delimiterIndex + 1).getBytes(); BLEInputStream.getInstance().putDataInBuffer(message); messageBuffer.delete(0, delimiterIndex + 1); } } else { BLEInputStream.getInstance().putDataInBuffer(data); } } }
private void fillBluetoothDeviceList() { for(BluetoothDevice device : mBluetoothDeviceManager.getPairedDevices()) { mDiscoveredDevices.put(device.getAddress(), device.getName() + " (" + device.getAddress() + ")"); } persistCandidateDiscoveredDevices(); mBluetoothDeviceManager.startDiscovery(); }
/** * 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 BluetoothGatt connectBLE(String address) throws BluetoothException { return connectBLE(getDefaultAdapter().getRemoteDevice(address)); }
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; }
@Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { readChangedCharacteristic(characteristic); }
public static BLEInputStream getInstance() { if (bleInputStream == null) { bleInputStream = new BLEInputStream(); } return bleInputStream; }
public boolean isBLEDisconnected() { if (mGattCallback != null) { return mGattCallback.isDisconnected(); } else { return false; } }
private void stopWhileBluetoothDisabled() { mSocketAccepterRunning = false; stopConnectionAttempts(); }
public boolean writeCharacteristicToBLE(byte[] bytes) { try { for (int i = 0; i < bytes.length; i++) { writeArray[queueEnd++] = bytes[i]; } } catch (BufferOverflowException e) { Log.d(TAG, "Buffer overflowing!!!!"); return false; } BLESendData(); return true; }
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 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; }
public boolean isBLEDevice(String address) { return getDefaultAdapter(). getRemoteDevice(address). getType() == BluetoothDevice.DEVICE_TYPE_LE; }