Can't write any more BLE device characterization after reconnecting

I am writing print data to a BluetoothGattCharacteristic

Zebra ZD410 printer. I do this by splitting the data into 20 byte blocks and writing the chunk at a time with the following code:

mCharacteristic.setValue(bytes);
boolean status = mGatt.writeCharacteristic(mCharacteristic);

      

and then until I get BluetoothGattCallback.onCharacteristicWrite()

until I start writing the next snippet. This works great.

If I am disconnect()

and close()

BluetoothGatt

and then reconnect to the same device with BluetoothDevice.connectGatt()

, and then try to write in Characteristic

after I was called onServicesDiscovered()

and I am my Characteristic

again, the recording will fail. I mean, when I write now Characteristic

, onCharacteristicWrite()

c will be called Characteristic

, which getValue()

returns the value of the last entry in the old Gatt.

After trying to solve this for two days and reading tons of SO posts, I haven't found a solution.

How can I fix this?

EDIT Here is the code forBluetoothGattCallback

private final BluetoothGattCallback gattCallback = new BluetoothGattCallback()
{
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState)
    {
        FALog.i(TAG, "onConnectionStateChange Status: " + status);
        switch (newState)
        {
            case BluetoothProfile.STATE_CONNECTED:
                FALog.i(TAG, "gattCallback STATE_CONNECTED");
                gatt.discoverServices();
                break;
            case BluetoothProfile.STATE_DISCONNECTED:
                disconnectAndCloseGatt();
                mCharacteristic = null;
                connectionFailed();
                FALog.e(TAG, "gattCallback STATE_DISCONNECTED");
                break;
            default:
                FALog.e(TAG, "gattCallback STATE_OTHER");
        }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status)
    {
        BluetoothGattService service = gatt.getService(PRINTER_SERVICE_UUID);
        if (service != null)
        {
            BluetoothGattCharacteristic characteristic = service.getCharacteristic
                    (PRINTER_SERVICE_CHARACTERISTIC_UUID);
            if (characteristic != null)
            {
                mCharacteristic = characteristic;
                mInternalState = STATE_CONNECTED;
                mState = State.CONNECTED;
                notifyStateChanged();
                print("~JA");
                FALog.d(TAG, "Printer connected");
                mBluetoothActivity.runOnUiThread(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        mListener.onPrinterConnected();
                    }
                });
            }
        }
    }

    @Override
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)
    {
        FALog.d(TAG, "received onCharacteristicWrite " + new String(characteristic.getValue()) + "; success: " +
                (status == BluetoothGatt.GATT_SUCCESS));
        if (status == BluetoothGatt.GATT_SUCCESS)
        {
            handler.removeCallbacks(writeRunnable);
            popQueueAndReleaseLock();
        }
    }
};

      

+3


source to share





All Articles