package com.afunx.ble.blelitelib.operation; import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGattCharacteristic; import com.afunx.ble.blelitelib.log.BleLiteLog; import com.afunx.ble.blelitelib.utils.HexUtils; import java.util.Arrays; import java.util.concurrent.atomic.AtomicInteger; /* loaded from: classes.dex */ public class BleWriteCharacterisitcNoResponsePacketOperation2 extends BleOperationAbs { private static final int PACKET_SIZE = 20; private static final String TAG = "BleWriteCharacterisitcNoResponsePacketOperation2"; private final BluetoothGatt mBluetoothGatt; private final BluetoothGattCharacteristic mCharacteristic; private final byte[] mMsg; private static int[] PACKET_INTERVALS = {5, 5, 5, 35}; private static final AtomicInteger mAutoId = new AtomicInteger(0); private static volatile long lastTimestamp = 0; private BleWriteCharacterisitcNoResponsePacketOperation2(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic, byte[] bArr) { this.mBluetoothGatt = bluetoothGatt; this.mCharacteristic = bluetoothGattCharacteristic; this.mMsg = bArr; } public static BleWriteCharacterisitcNoResponsePacketOperation2 createInstance(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic, byte[] bArr) { return new BleWriteCharacterisitcNoResponsePacketOperation2(bluetoothGatt, bluetoothGattCharacteristic, bArr); } public static void setPacketIntervals(int[] iArr) { PACKET_INTERVALS = iArr; } private static void sleep() { int currentTimeMillis; synchronized (BleWriteCharacterisitcNoResponsePacketOperation2.class) { int andAdd = mAutoId.getAndAdd(1) % PACKET_INTERVALS.length; if (lastTimestamp == 0) { currentTimeMillis = 0; } else { currentTimeMillis = PACKET_INTERVALS[andAdd] - ((int) (System.currentTimeMillis() - lastTimestamp)); } if (currentTimeMillis > 0) { try { BleWriteCharacterisitcNoResponsePacketOperation2.class.wait(currentTimeMillis); } catch (InterruptedException unused) { Thread.currentThread().interrupt(); } } } } private static synchronized void updateLastTimestamp() { synchronized (BleWriteCharacterisitcNoResponsePacketOperation2.class) { lastTimestamp = System.currentTimeMillis(); } } @Override // com.afunx.ble.blelitelib.operation.BleOperationAbs protected void clearConcurrentOperation() { } @Override // com.afunx.ble.blelitelib.operation.BleOperation public int getOperatcionCode() { return 11; } @Override // com.afunx.ble.blelitelib.operation.BleOperation, java.lang.Runnable public void run() { int length = this.mMsg.length; this.mCharacteristic.setWriteType(1); int i = 0; int i2 = 0; while (i < length) { sleep(); int i3 = i + 20 <= length ? 20 : length - i; byte[] copyOfRange = Arrays.copyOfRange(this.mMsg, i, i + i3); this.mCharacteristic.setValue(copyOfRange); boolean writeCharacteristic = this.mBluetoothGatt.writeCharacteristic(this.mCharacteristic); if (writeCharacteristic) { i2 = 0; } else { i2++; if (i2 > 3) { BleLiteLog.e(TAG, "writeCharacteristic() fail"); return; } i -= i3; try { Thread.sleep(50L); } catch (InterruptedException e) { e.printStackTrace(); Thread.currentThread().interrupt(); } } BleLiteLog.i(TAG, "writeCharacteristic() " + HexUtils.bytes2HexStringWithSpace(copyOfRange, 0, i3) + ", isWriteSuc: " + writeCharacteristic); updateLastTimestamp(); i += i3; } } }