BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (mBluetoothAdapter == null) { // Device does not support Bluetooth } else { if (!mBluetoothAdapter.isEnabled()) { // Bluetooth is not enable :) } }
boolean isCandidateDevice(BluetoothDevice device) { int deviceClass=device.getBluetoothClass().getDeviceClass(); return(((deviceClass & BluetoothClass.Device.Major.COMPUTER)== BluetoothClass.Device.Major.COMPUTER) || ((deviceClass & BluetoothClass.Device.Major.PHONE)== BluetoothClass.Device.Major.PHONE) || ((deviceClass & BluetoothClass.Device.Major.AUDIO_VIDEO)== BluetoothClass.Device.Major.AUDIO_VIDEO)); } }
protected static final UUID CHARACTERISTIC_UPDATE_NOTIFICATION_DESCRIPTOR_UUID = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"); public boolean setCharacteristicNotification(BluetoothDevice device, UUID serviceUuid, UUID characteristicUuid, boolean enable) { if (IS_DEBUG) Log.d(TAG, "setCharacteristicNotification(device=" + device.getName() + device.getAddress() + ", UUID=" + characteristicUuid + ", enable=" + enable + " )"); BluetoothGatt gatt = mGattInstances.get(device.getAddress()); //I just hold the gatt instances I got from connect in this HashMap BluetoothGattCharacteristic characteristic = gatt.getService(serviceUuid).getCharacteristic(characteristicUuid); gatt.setCharacteristicNotification(characteristic, enable); BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CHARACTERISTIC_UPDATE_NOTIFICATION_DESCRIPTOR_UUID); descriptor.setValue(enable ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : new byte[] { 0x00, 0x00 }); return gatt.writeDescriptor(descriptor); //descriptor write operation successfully started? }
public static boolean setBluetooth(boolean enable) { BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); boolean isEnabled = bluetoothAdapter.isEnabled(); if (enable && !isEnabled) { return bluetoothAdapter.enable(); } else if(!enable && isEnabled) { return bluetoothAdapter.disable(); } // No need to change bluetooth state return true; }
@Override public String toString() { return "RxBleDeviceImpl{" + "bluetoothDevice=" + bluetoothDevice.getName() + '(' + bluetoothDevice.getAddress() + ')' + '}'; } }
@Test public void getUuids_setUuidsNotCalled_shouldReturnNull() throws Exception { BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(MOCK_MAC_ADDRESS); assertThat(device.getUuids()).isNull(); }
@Implementation protected BluetoothAdapter getAdapter() { return BluetoothAdapter.getDefaultAdapter(); } }
@Override protected boolean startOperation(BluetoothGatt bluetoothGatt) { bluetoothGattCharacteristic.setValue(data); return bluetoothGatt.writeCharacteristic(bluetoothGattCharacteristic); } }
@Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { ... if(characteristic.getValue() != value) { gatt.abortReliableWrite(); } else { gatt.executeReliableWrite(); } ... }
@Override public BluetoothGattDescriptor apply(BluetoothGattCharacteristic bluetoothGattCharacteristic) { BluetoothGattDescriptor bluetoothGattDescriptor = bluetoothGattCharacteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_UUID); if (bluetoothGattDescriptor == null) { //adding notification descriptor if not present bluetoothGattDescriptor = new BluetoothGattDescriptor(CLIENT_CHARACTERISTIC_CONFIG_UUID, 0); bluetoothGattCharacteristic.addDescriptor(bluetoothGattDescriptor); } return bluetoothGattDescriptor; } });
@Override public byte[] apply(BluetoothGattCharacteristic characteristic) { characteristic.setValue(data); return data; } });
@Override public void run() { bluetoothGatt.disconnect(); } });
private boolean isDisconnected(BluetoothGatt bluetoothGatt) { return bluetoothManager.getConnectionState(bluetoothGatt.getDevice(), BluetoothProfile.GATT) == BluetoothProfile.STATE_DISCONNECTED; }
public void notifyNativeConnectionStateCallback(BluetoothGatt gatt, int status, int newState) { if (nativeCallback != null) { nativeCallback.onConnectionStateChange(gatt, status, newState); } }
void notifyNativeReadCallback(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { if (nativeCallback != null) { nativeCallback.onCharacteristicRead(gatt, characteristic, status); } } }
public void notifyNativeWriteCallback(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { if (nativeCallback != null) { nativeCallback.onCharacteristicWrite(gatt, characteristic, status); } }
@Override public boolean equals(Object o) { if (this == o) { return true; } if (!(o instanceof RxBleDeviceImpl)) { return false; } RxBleDeviceImpl that = (RxBleDeviceImpl) o; return bluetoothDevice.equals(that.bluetoothDevice); }
public void notifyNativeChangedCallback(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { if (nativeCallback != null) { nativeCallback.onCharacteristicChanged(gatt, characteristic); } }
public void notifyNativeReadRssiCallback(BluetoothGatt gatt, int rssi, int status) { if (nativeCallback != null) { nativeCallback.onReadRemoteRssi(gatt, rssi, status); } }