Java Code Examples for com.polidea.rxandroidble2.internal.RxBleLog#w()

The following examples show how to use com.polidea.rxandroidble2.internal.RxBleLog#w() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ValueInterpreter.java    From RxAndroidBle with Apache License 2.0 6 votes vote down vote up
/**
 * Return the float value interpreted from the passed byte array.
 *
 * @param value The byte array from which to interpret value.
 * @param formatType The format type used to interpret the value.
 * @param offset Offset at which the float value can be found.
 * @return The value at a given offset or null if the requested offset exceeds the value size.
 */
public static Float getFloatValue(@NonNull byte[] value, @FloatFormatType int formatType, @IntRange(from = 0) int offset) {
    if ((offset + getTypeLen(formatType)) > value.length) {
        RxBleLog.w(
                "Float formatType (0x%x) is longer than remaining bytes (%d) - returning null", formatType, value.length - offset
        );
        return null;
    }

    switch (formatType) {
        case FORMAT_SFLOAT:
            return bytesToFloat(value[offset], value[offset + 1]);

        case FORMAT_FLOAT:
            return bytesToFloat(value[offset],   value[offset + 1],
                    value[offset + 2], value[offset + 3]);
        default:
            RxBleLog.w("Passed an invalid float formatType (0x%x) - returning null", formatType);
            return null;
    }
}
 
Example 2
Source File: ScanSettingsEmulator.java    From RxAndroidBle with Apache License 2.0 6 votes vote down vote up
ObservableTransformer<RxBleInternalScanResult, RxBleInternalScanResult> emulateScanMode(@ScanSettings.ScanMode int scanMode) {
    switch (scanMode) {

        case ScanSettings.SCAN_MODE_BALANCED:
            return scanModeBalancedTransformer();
        case ScanSettings.SCAN_MODE_OPPORTUNISTIC:
            RxBleLog.w("Cannot emulate opportunistic scan mode since it is OS dependent - fallthrough to low power");
            // fallthrough
        case ScanSettings.SCAN_MODE_LOW_POWER:
            return scanModeLowPowerTransformer();
        case ScanSettings.SCAN_MODE_LOW_LATENCY:
            // return the original observable - fallthrough
        default: // checkstyle always needs default
            return identityTransformer();
    }
}
 
Example 3
Source File: BackgroundScannerImpl.java    From RxAndroidBle with Apache License 2.0 6 votes vote down vote up
@RequiresApi(26 /* Build.VERSION_CODES.O */)
@Override
public void scanBleDeviceInBackground(@NonNull PendingIntent callbackIntent, ScanSettings scanSettings, ScanFilter... scanFilters) {
    if (Build.VERSION.SDK_INT < 26 /* Build.VERSION_CODES.O */) {
        RxBleLog.w("PendingIntent based scanning is available for Android O and higher only.");
        return;
    }
    if (!rxBleAdapterWrapper.isBluetoothEnabled()) {
        RxBleLog.w("PendingIntent based scanning is available only when Bluetooth is ON.");
        throw new BleScanException(BleScanException.BLUETOOTH_DISABLED);
    }

    RxBleLog.i("Requesting pending intent based scan.");
    final List<android.bluetooth.le.ScanFilter> nativeScanFilters = scanObjectsConverter.toNativeFilters(scanFilters);
    final android.bluetooth.le.ScanSettings nativeScanSettings = scanObjectsConverter.toNativeSettings(scanSettings);
    final int scanStartResult = rxBleAdapterWrapper.startLeScan(nativeScanFilters, nativeScanSettings, callbackIntent);

    if (scanStartResult != NO_ERROR) {
        final BleScanException bleScanException = new BleScanException(scanStartResult);
        RxBleLog.w(bleScanException, "Failed to start scan"); // TODO?
        throw bleScanException;
    }
}
 
Example 4
Source File: ScanOperationApi21.java    From RxAndroidBle with Apache License 2.0 6 votes vote down vote up
@BleScanException.Reason
static int errorCodeToBleErrorCode(int errorCode) {
    switch (errorCode) {
        case ScanCallback.SCAN_FAILED_ALREADY_STARTED:
            return BleScanException.SCAN_FAILED_ALREADY_STARTED;
        case ScanCallback.SCAN_FAILED_APPLICATION_REGISTRATION_FAILED:
            return BleScanException.SCAN_FAILED_APPLICATION_REGISTRATION_FAILED;
        case ScanCallback.SCAN_FAILED_FEATURE_UNSUPPORTED:
            return BleScanException.SCAN_FAILED_FEATURE_UNSUPPORTED;
        case ScanCallback.SCAN_FAILED_INTERNAL_ERROR:
            return BleScanException.SCAN_FAILED_INTERNAL_ERROR;
        case 5: // ScanCallback.SCAN_FAILED_OUT_OF_HARDWARE_RESOURCES
            return BleScanException.SCAN_FAILED_OUT_OF_HARDWARE_RESOURCES;
        default:
            RxBleLog.w("Encountered unknown scanning error code: %d -> check android.bluetooth.le.ScanCallback");
            return BleScanException.UNKNOWN_ERROR_CODE;
    }
}
 
Example 5
Source File: ScanOperation.java    From RxAndroidBle with Apache License 2.0 6 votes vote down vote up
@Override
final protected void protectedRun(final ObservableEmitter<SCAN_RESULT_TYPE> emitter, QueueReleaseInterface queueReleaseInterface) {

    final SCAN_CALLBACK_TYPE scanCallback = createScanCallback(emitter);

    try {
        emitter.setCancellable(new Cancellable() {
            @Override
            public void cancel() {
                RxBleLog.i("Scan operation is requested to stop.");
                stopScan(rxBleAdapterWrapper, scanCallback);
            }
        });
        RxBleLog.i("Scan operation is requested to start.");
        boolean startLeScanStatus = startScan(rxBleAdapterWrapper, scanCallback);

        if (!startLeScanStatus) {
            emitter.tryOnError(new BleScanException(BleScanException.BLUETOOTH_CANNOT_START));
        }
    } catch (Throwable throwable) {
        RxBleLog.w(throwable, "Error while calling the start scan function");
        emitter.tryOnError(new BleScanException(BleScanException.BLUETOOTH_CANNOT_START, throwable));
    } finally {
        queueReleaseInterface.release();
    }
}
 
Example 6
Source File: RxBleAdapterWrapper.java    From RxAndroidBle with Apache License 2.0 6 votes vote down vote up
@TargetApi(21 /* Build.VERSION_CODES.LOLLIPOP */)
public void stopLeScan(ScanCallback scanCallback) {
    if (!bluetoothAdapter.isEnabled()) {
        // this situation seems to be a problem since API 29
        RxBleLog.v(
                "BluetoothAdapter is disabled, calling BluetoothLeScanner.stopScan(ScanCallback) may cause IllegalStateException"
        );
        // if stopping the scan is not possible due to BluetoothAdapter turned off then it is probably stopped anyway
        return;
    }
    final BluetoothLeScanner bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
    if (bluetoothLeScanner == null) {
        RxBleLog.w(
                "Cannot call BluetoothLeScanner.stopScan(ScanCallback) on 'null' reference; BluetoothAdapter.isEnabled() == %b",
                bluetoothAdapter.isEnabled()
        );
        // if stopping the scan is not possible due to BluetoothLeScanner not accessible then it is probably stopped anyway
        // this should not happen since the check for BluetoothAdapter.isEnabled() has been added above. This situation was only
        // observed when the adapter was disabled
        return;
    }
    bluetoothLeScanner.stopScan(scanCallback);
}
 
Example 7
Source File: ValueInterpreter.java    From RxAndroidBle with Apache License 2.0 5 votes vote down vote up
/**
 * Return the integer value interpreted from the passed byte array.
 *
 * <p>The formatType parameter determines how the value
 * is to be interpreted. For example, setting formatType to
 * {@link #FORMAT_UINT16} specifies that the first two bytes of the
 * characteristic value at the given offset are interpreted to generate the
 * return value.
 *
 * @param value The byte array from which to interpret value.
 * @param formatType The format type used to interpret the value.
 * @param offset Offset at which the integer value can be found.
 * @return The value at a given offset or null if offset exceeds value size.
 */
public static Integer getIntValue(@NonNull byte[] value, @IntFormatType int formatType, @IntRange(from = 0) int offset) {
    if ((offset + getTypeLen(formatType)) > value.length) {
        RxBleLog.w(
                "Int formatType (0x%x) is longer than remaining bytes (%d) - returning null", formatType, value.length - offset
        );
        return null;
    }

    switch (formatType) {
        case FORMAT_UINT8:
            return unsignedByteToInt(value[offset]);

        case FORMAT_UINT16:
            return unsignedBytesToInt(value[offset], value[offset + 1]);

        case FORMAT_UINT32:
            return unsignedBytesToInt(value[offset],   value[offset + 1],
                    value[offset + 2], value[offset + 3]);
        case FORMAT_SINT8:
            return unsignedToSigned(unsignedByteToInt(value[offset]), 8);

        case FORMAT_SINT16:
            return unsignedToSigned(unsignedBytesToInt(value[offset],
                    value[offset + 1]), 16);

        case FORMAT_SINT32:
            return unsignedToSigned(unsignedBytesToInt(value[offset],
                    value[offset + 1], value[offset + 2], value[offset + 3]), 32);
        default:
            RxBleLog.w("Passed an invalid integer formatType (0x%x) - returning null", formatType);
            return null;
    }
}
 
Example 8
Source File: ValueInterpreter.java    From RxAndroidBle with Apache License 2.0 5 votes vote down vote up
/**
 * Return the string value interpreted from the passed byte array.
 *
 * @param offset Offset at which the string value can be found.
 * @return The value at a given offset
 */
public static String getStringValue(@NonNull byte[] value, @IntRange(from = 0) int offset) {
    if (offset > value.length) {
        RxBleLog.w("Passed offset that exceeds the length of the byte array - returning null");
        return null;
    }
    byte[] strBytes = new byte[value.length - offset];
    for (int i = 0; i != (value.length - offset); ++i) {
        strBytes[i] = value[offset + i];
    }
    return new String(strBytes);
}
 
Example 9
Source File: QueueSemaphore.java    From RxAndroidBle with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized void awaitRelease() throws InterruptedException {
    while (!isReleased.get()) {
        try {
            wait();
        } catch (InterruptedException e) {
            if (!isReleased.get()) {
                RxBleLog.w(e, "Queue's awaitRelease() has been interrupted abruptly "
                        + "while it wasn't released by the release() method.");
            }
        }
    }
}
 
Example 10
Source File: BackgroundScannerImpl.java    From RxAndroidBle with Apache License 2.0 5 votes vote down vote up
@RequiresApi(26 /* Build.VERSION_CODES.O */)
@Override
public void stopBackgroundBleScan(@NonNull PendingIntent callbackIntent) {
    if (Build.VERSION.SDK_INT < 26 /* Build.VERSION_CODES.O */) {
        RxBleLog.w("PendingIntent based scanning is available for Android O and higher only.");
        return;
    }
    if (!rxBleAdapterWrapper.isBluetoothEnabled()) {
        RxBleLog.w("PendingIntent based scanning is available only when Bluetooth is ON.");
        return;
    }

    RxBleLog.i("Stopping pending intent based scan.");
    rxBleAdapterWrapper.stopLeScan(callbackIntent);
}
 
Example 11
Source File: InternalScanResultCreator.java    From RxAndroidBle with Apache License 2.0 5 votes vote down vote up
@RequiresApi(21 /* Build.VERSION_CODES.LOLLIPOP */)
private static ScanCallbackType toScanCallbackType(int callbackType) {
    switch (callbackType) {
        case ScanSettings.CALLBACK_TYPE_ALL_MATCHES:
            return CALLBACK_TYPE_ALL_MATCHES;
        case ScanSettings.CALLBACK_TYPE_FIRST_MATCH:
            return CALLBACK_TYPE_FIRST_MATCH;
        case ScanSettings.CALLBACK_TYPE_MATCH_LOST:
            return CALLBACK_TYPE_MATCH_LOST;
        default:
            RxBleLog.w("Unknown callback type %d -> check android.bluetooth.le.ScanSettings", callbackType);
            return CALLBACK_TYPE_UNKNOWN;
    }
}
 
Example 12
Source File: CheckerLocationProvider.java    From RxAndroidBle with Apache License 2.0 5 votes vote down vote up
public boolean isLocationProviderEnabled() {
    if (Build.VERSION.SDK_INT >= 19 /* Build.VERSION_CODES.KITKAT */) {
        try {
            return Settings.Secure.getInt(contentResolver, Settings.Secure.LOCATION_MODE) != Settings.Secure.LOCATION_MODE_OFF;
        } catch (Settings.SettingNotFoundException e) {
            RxBleLog.w(e, "Could not use LOCATION_MODE check. Falling back to legacy method.");
        }
    }
    return locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
            || locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
 
Example 13
Source File: LoggingIllegalOperationHandler.java    From RxAndroidBle with Apache License 2.0 4 votes vote down vote up
/**
 * This method logs a warning.
 * @param characteristic the characteristic upon which the operation was requested
 * @param neededProperties bitmask of properties needed by the operation
 */
@Override
public BleIllegalOperationException handleMismatchData(BluetoothGattCharacteristic characteristic, int neededProperties) {
    RxBleLog.w(messageCreator.createMismatchMessage(characteristic, neededProperties));
    return null;
}
 
Example 14
Source File: BleConnectionCompat.java    From RxAndroidBle with Apache License 2.0 4 votes vote down vote up
public BluetoothGatt connectGatt(BluetoothDevice remoteDevice, boolean autoConnect, BluetoothGattCallback bluetoothGattCallback) {

        if (remoteDevice == null) {
            return null;
        }

        /**
         * Issue that caused a race condition mentioned below was fixed in 7.0.0_r1
         * https://android.googlesource.com/platform/frameworks/base/+/android-7.0.0_r1/core/java/android/bluetooth/BluetoothGatt.java#649
         * compared to
         * https://android.googlesource.com/platform/frameworks/base/+/android-6.0.1_r72/core/java/android/bluetooth/BluetoothGatt.java#739
         * issue: https://android.googlesource.com/platform/frameworks/base/+/d35167adcaa40cb54df8e392379dfdfe98bcdba2%5E%21/#F0
          */
        if (Build.VERSION.SDK_INT >= 24 /* Build.VERSION_CODES.N */ || !autoConnect) {
            return connectGattCompat(bluetoothGattCallback, remoteDevice, autoConnect);
        }

        /**
         * Some implementations of Bluetooth Stack have a race condition where autoConnect flag
         * is not properly set before calling connectGatt. That's the reason for using reflection
         * to set the flag manually.
         */

        try {
            RxBleLog.v("Trying to connectGatt using reflection.");
            Object iBluetoothGatt = getIBluetoothGatt(getIBluetoothManager());

            if (iBluetoothGatt == null) {
                RxBleLog.w("Couldn't get iBluetoothGatt object");
                return connectGattCompat(bluetoothGattCallback, remoteDevice, true);
            }

            BluetoothGatt bluetoothGatt = createBluetoothGatt(iBluetoothGatt, remoteDevice);

            if (bluetoothGatt == null) {
                RxBleLog.w("Couldn't create BluetoothGatt object");
                return connectGattCompat(bluetoothGattCallback, remoteDevice, true);
            }

            boolean connectedSuccessfully = connectUsingReflection(bluetoothGatt, bluetoothGattCallback, true);

            if (!connectedSuccessfully) {
                RxBleLog.w("Connection using reflection failed, closing gatt");
                bluetoothGatt.close();
            }

            return bluetoothGatt;
        } catch (NoSuchMethodException
                | IllegalAccessException
                | IllegalArgumentException
                | InvocationTargetException
                | InstantiationException
                | NoSuchFieldException exception) {
            RxBleLog.w(exception, "Error while trying to connect via reflection");
            return connectGattCompat(bluetoothGattCallback, remoteDevice, true);
        }
    }