WriteCharacteristic () returns true, but does not call onCharacteristicWrite ()

I want to read the characteristic value stored on the device, change the value and then write it to the device. For some reason, writeCharacteristic()returns true, but the value of the internal device does not change and is onCharacteristicWrite()not called. In fact, for some reason, it is called only if I try to write something, and then it is called shortly after closing or reopening the application and does not change the value of the device. I studied this for several days and it drove me crazy. It may be worth noting that reading characteristics both manually and through notification work fine.

Why writeCharacteristic()does not pass, and why onCharacteristicWrite()is it called at such an odd time?

I am not sure where this problem comes from. It may be something dumb and simple, how to call it wrong writeCharacteristic()(my implementation is based on the question " Working with BLE Android 4.3 how to write features? "). Is it possible that the request is ignored because it onCharacteristicRead()is somehow not finished?

I believe that these links are most useful for indicating the problem, but I myself could not extract anything from them:

As the passage of my code, an event occurs onItemClick()that triggers a sequence.

int flag = 1;
((MainActivity)getActivity()).BtService.readFlag(flag);

( Service), Bluetooth .

public boolean readFlag(int flag){
    /*... removed code here verifies that the Bluetooth Gatt is available,
    that the Service exists...*/

    BluetoothGattCharacteristic characteristic = Service.getCharacteristic(SEND_FLAGS_CHAR);

    if (characteristic == null) {
        Log.e(TAG, "char not found!");
        return false;
    }

    try {
        // Store intended flag value in SharedPreferences, then read the current one.
        Editor fEditor = sPrefs.edit();
        fEditor.putInt(calibrationSetFlagToSendKey, flag);
        fEditor.commit();

        mConnectedGatt.readCharacteristic(characteristic);

        // Catch response in onCharacteristicRead() callback.
        return true;
    }
    catch (NullPointerException e) {
        Log.w("readCharacteristic", "The characteristic could not be read.");
        return false;
    }
}

onCharacteristicRead(), Handler, MainActivity. trueFlagValue - , Service ( , , .)

case MSG_SEND_FLAG:
    characteristic = (BluetoothGattCharacteristic) msg.obj;
    if (characteristic.getValue() == null) {
        Log.w(TAG, "Error obtaining current flags");
        return;
    }

    int recentReadDeviceFlag = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0);
    int initialFlag = sPrefs.getInt(calibrationSetFlagToSendKey, 0);
    if (initialFlag == 0) Log.e("Write Debug", "Flag to send is apparently 0");

    /*... Arithmetic modifying flag integer value...*/
    //Desired value is the OR'd value of the value read and value desired
    trueFlagValue = (byte) ((byte) initialFlag | (byte) recentReadDeviceFlag);
    Log.d("Read Debug", "OR'd value is " + trueFlagValue +", sending broadcast");
    Intent fIntent = new Intent("com.example.appwithble.SEND_FLAGS");
    sendBroadcast(fIntent);
    break;

BroadcastReceiver MainActivity Service, writeCharacteristic(), readCharacteristic(). trueFlagValue.

    else if("com.example.appwithble.SEND_FLAGS".equals(intent.getAction())) {
        BtService.performWriteFlag();
    }

// ...

public boolean performWriteFlag () {
    /*... check Gatt is available and that Service exists...*/
    BluetoothGattCharacteristic characteristic = Service.getCharacteristic(SEND_FLAGS_CHAR);

    if (characteristic == null) {
        Log.e(TAG, "char not found!");
        return false;
    }

    byte[] byteValue = new byte[1];
    byteValue[0] = trueFlagValue;
    characteristic.setValue(byteValue);

    boolean status = mConnectedGatt.writeCharacteristic(characteristic);
    return status;
}

My onCharacteristicWrite() . . , , .

+4
2

.

, , - . , readCharacteristic(), , , , , , , , .

, ( ), , . , , .

byte[] byteValue = new byte[2];
byteValue[0] = trueFlagValue;
byteValue[1] = (byte)0;
characteristic.setValue(byteValue);

, - , , , , , - , - .

+1

, BLE

?

0

All Articles