614 lines
		
	
	
		
			30 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
			
		
		
	
	
			614 lines
		
	
	
		
			30 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
package com.afunx.ble.blelitelib.proxy;
 | 
						|
 | 
						|
import android.bluetooth.BluetoothGatt;
 | 
						|
import android.bluetooth.BluetoothGattCallback;
 | 
						|
import android.bluetooth.BluetoothGattCharacteristic;
 | 
						|
import android.bluetooth.BluetoothGattDescriptor;
 | 
						|
import android.bluetooth.BluetoothGattService;
 | 
						|
import android.content.Context;
 | 
						|
import android.os.Build;
 | 
						|
import android.util.Log;
 | 
						|
import android.util.LongSparseArray;
 | 
						|
import com.afunx.ble.blelitelib.connector.BleConnector;
 | 
						|
import com.afunx.ble.blelitelib.log.BleLiteLog;
 | 
						|
import com.afunx.ble.blelitelib.operation.BleCloseOperation;
 | 
						|
import com.afunx.ble.blelitelib.operation.BleConnectOperation;
 | 
						|
import com.afunx.ble.blelitelib.operation.BleDiscoverServiceOperation;
 | 
						|
import com.afunx.ble.blelitelib.operation.BleOperation;
 | 
						|
import com.afunx.ble.blelitelib.operation.BleReadCharacteristicOperation;
 | 
						|
import com.afunx.ble.blelitelib.operation.BleRequestMtuOperation;
 | 
						|
import com.afunx.ble.blelitelib.operation.BleWriteCharacterisitcNoResponsePacketOperation;
 | 
						|
import com.afunx.ble.blelitelib.operation.BleWriteCharacterisitcNoResponsePacketOperation2;
 | 
						|
import com.afunx.ble.blelitelib.operation.BleWriteCharacteristicNoResponse20Operation;
 | 
						|
import com.afunx.ble.blelitelib.operation.BleWriteCharacteristicNoResponseInterruptOperation;
 | 
						|
import com.afunx.ble.blelitelib.operation.BleWriteCharacteristicNoResponseOperation;
 | 
						|
import com.afunx.ble.blelitelib.operation.BleWriteCharacteristicOperation;
 | 
						|
import com.afunx.ble.blelitelib.operation.BleWriteDescriptorOperation;
 | 
						|
import com.afunx.ble.blelitelib.proxy.BleGattClientProxy;
 | 
						|
import com.afunx.ble.blelitelib.proxy.scheme.BleGattReconnectScheme;
 | 
						|
import com.afunx.ble.blelitelib.proxy.scheme.BleGattReconnectSchemeDefaultImpl;
 | 
						|
import com.afunx.ble.blelitelib.threadpool.BleThreadpool;
 | 
						|
import com.afunx.ble.blelitelib.utils.BleGattStateParser;
 | 
						|
import com.afunx.ble.blelitelib.utils.BleGattStatusParser;
 | 
						|
import com.afunx.ble.blelitelib.utils.BleUuidUtils;
 | 
						|
import com.afunx.ble.blelitelib.utils.HexUtils;
 | 
						|
import java.util.List;
 | 
						|
import java.util.UUID;
 | 
						|
 | 
						|
/* loaded from: classes.dex */
 | 
						|
public class BleGattClientProxyImpl implements BleGattClientProxy {
 | 
						|
    private static final String TAG = "BleGattClientProxyImpl";
 | 
						|
    private static final String UUID_CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR = "00002902-0000-1000-8000-00805f9b34fb";
 | 
						|
    private static final String VERSION = "v0.9.5";
 | 
						|
    private final Context mAppContext;
 | 
						|
    private BleConnector mBleConnector;
 | 
						|
    private volatile boolean mIsClosed = false;
 | 
						|
    private final LongSparseArray<BleOperation> mOperations = new LongSparseArray<>();
 | 
						|
    private final LongSparseArray<BleGattClientProxy.OnCharacteristicNotificationListener> mListeners = new LongSparseArray<>();
 | 
						|
    private final BleGattReconnectScheme mReconnectScheme = new BleGattReconnectSchemeDefaultImpl();
 | 
						|
    private final Object mLock4Connect = new Object();
 | 
						|
    private final Object mLock4Close = new Object();
 | 
						|
    private BluetoothGattCallback mBluetoothGattCallback = new BluetoothGattCallback() { // from class: com.afunx.ble.blelitelib.proxy.BleGattClientProxyImpl.1
 | 
						|
        @Override // android.bluetooth.BluetoothGattCallback
 | 
						|
        public void onCharacteristicChanged(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic) {
 | 
						|
            UUID uuid = bluetoothGattCharacteristic.getUuid();
 | 
						|
            byte[] value = bluetoothGattCharacteristic.getValue();
 | 
						|
            BleLiteLog.i(BleGattClientProxyImpl.TAG, "onCharacteristicChanged() characteristic uuid: " + uuid + " msg: " + HexUtils.bytes2HexStringWithSpace(value));
 | 
						|
            BleGattClientProxy.OnCharacteristicNotificationListener listener = BleGattClientProxyImpl.this.getListener(uuid);
 | 
						|
            if (listener != null) {
 | 
						|
                listener.onCharacteristicNotification(value);
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        @Override // android.bluetooth.BluetoothGattCallback
 | 
						|
        public void onCharacteristicRead(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic, int i) {
 | 
						|
            BleLiteLog.i(BleGattClientProxyImpl.TAG, "onCharacteristicRead() characteristic uuid: " + bluetoothGattCharacteristic.getUuid() + ", status: " + BleGattStatusParser.parse(i));
 | 
						|
            BleReadCharacteristicOperation readCharacteristicOperation = BleGattClientProxyImpl.this.getReadCharacteristicOperation();
 | 
						|
            if (i != 0 || readCharacteristicOperation == null) {
 | 
						|
                return;
 | 
						|
            }
 | 
						|
            readCharacteristicOperation.setResult(bluetoothGattCharacteristic.getValue());
 | 
						|
            readCharacteristicOperation.notifyLock();
 | 
						|
        }
 | 
						|
 | 
						|
        @Override // android.bluetooth.BluetoothGattCallback
 | 
						|
        public void onCharacteristicWrite(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic, int i) {
 | 
						|
            BleLiteLog.i(BleGattClientProxyImpl.TAG, "onCharacteristicWrite() characteristic uuid: " + bluetoothGattCharacteristic.getUuid() + ", status: " + BleGattStatusParser.parse(i));
 | 
						|
            BleWriteCharacteristicOperation writeCharacteristicOperation = BleGattClientProxyImpl.this.getWriteCharacteristicOperation();
 | 
						|
            if (i != 0 || writeCharacteristicOperation == null) {
 | 
						|
                return;
 | 
						|
            }
 | 
						|
            writeCharacteristicOperation.notifyLock();
 | 
						|
        }
 | 
						|
 | 
						|
        @Override // android.bluetooth.BluetoothGattCallback
 | 
						|
        public void onConnectionStateChange(BluetoothGatt bluetoothGatt, int i, int i2) {
 | 
						|
            BleLiteLog.i(BleGattClientProxyImpl.TAG, "onConnectionStateChange() status: " + BleGattStatusParser.parse(i) + ", newState: " + BleGattStateParser.parse(i2));
 | 
						|
            BleConnectOperation connectOperation = BleGattClientProxyImpl.this.getConnectOperation();
 | 
						|
            if (i2 != 0) {
 | 
						|
                if (i2 == 1 || i2 != 2) {
 | 
						|
                    return;
 | 
						|
                }
 | 
						|
                BleGattClientProxyImpl.this.mReconnectScheme.clearRetryCount();
 | 
						|
                if (connectOperation != null) {
 | 
						|
                    connectOperation.notifyLock();
 | 
						|
                    return;
 | 
						|
                }
 | 
						|
                return;
 | 
						|
            }
 | 
						|
            synchronized (BleGattClientProxyImpl.this.mLock4Close) {
 | 
						|
                if (!BleGattClientProxyImpl.this.mIsClosed) {
 | 
						|
                    if (!BleGattClientProxyImpl.this.mReconnectScheme.tryAgain()) {
 | 
						|
                        BleLiteLog.w(BleGattClientProxyImpl.TAG, "onConnectionStateChange() stop reconnect");
 | 
						|
                        return;
 | 
						|
                    }
 | 
						|
                    if (i == 8) {
 | 
						|
                        BleGattClientProxyImpl.this.mReconnectScheme.callDisconnectCallbackInstantly();
 | 
						|
                        BleGattClientProxyImpl.this.__close();
 | 
						|
                    } else {
 | 
						|
                        final long sleepTimestamp = BleGattClientProxyImpl.this.mReconnectScheme.getSleepTimestamp(BleGattClientProxyImpl.this.mReconnectScheme.addAndGetRetryCount());
 | 
						|
                        BleThreadpool.getInstance().submit(new Runnable() { // from class: com.afunx.ble.blelitelib.proxy.BleGattClientProxyImpl.1.1
 | 
						|
                            @Override // java.lang.Runnable
 | 
						|
                            public void run() {
 | 
						|
                                try {
 | 
						|
                                    Thread.sleep(sleepTimestamp);
 | 
						|
                                } catch (InterruptedException unused) {
 | 
						|
                                    Thread.currentThread().interrupt();
 | 
						|
                                }
 | 
						|
                                BleConnectOperation connectOperation2 = BleGattClientProxyImpl.this.getConnectOperation();
 | 
						|
                                if (connectOperation2 == null) {
 | 
						|
                                    BleLiteLog.i(BleGattClientProxyImpl.TAG, "onConnectionStateChange() connectOperation is null");
 | 
						|
                                } else {
 | 
						|
                                    BleLiteLog.i(BleGattClientProxyImpl.TAG, "onConnectionStateChange() reconnect...");
 | 
						|
                                    connectOperation2.doRunnableSelfAsync(true);
 | 
						|
                                }
 | 
						|
                            }
 | 
						|
                        });
 | 
						|
                    }
 | 
						|
                }
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        @Override // android.bluetooth.BluetoothGattCallback
 | 
						|
        public void onDescriptorWrite(BluetoothGatt bluetoothGatt, BluetoothGattDescriptor bluetoothGattDescriptor, int i) {
 | 
						|
            BleLiteLog.i(BleGattClientProxyImpl.TAG, "onDescriptorWrite() characteristic uuid: " + bluetoothGattDescriptor.getUuid());
 | 
						|
            BleWriteDescriptorOperation writeDescriptorOperation = BleGattClientProxyImpl.this.getWriteDescriptorOperation();
 | 
						|
            if (i != 0 || writeDescriptorOperation == null) {
 | 
						|
                return;
 | 
						|
            }
 | 
						|
            writeDescriptorOperation.notifyLock();
 | 
						|
        }
 | 
						|
 | 
						|
        @Override // android.bluetooth.BluetoothGattCallback
 | 
						|
        public void onMtuChanged(BluetoothGatt bluetoothGatt, int i, int i2) {
 | 
						|
            BleLiteLog.i(BleGattClientProxyImpl.TAG, "onMtuChanged() mtu: " + i + ", status: " + BleGattStatusParser.parse(i2));
 | 
						|
            BleRequestMtuOperation requestMtuOperation = BleGattClientProxyImpl.this.getRequestMtuOperation();
 | 
						|
            if (i2 != 0 || requestMtuOperation == null) {
 | 
						|
                return;
 | 
						|
            }
 | 
						|
            requestMtuOperation.notifyLock();
 | 
						|
        }
 | 
						|
 | 
						|
        @Override // android.bluetooth.BluetoothGattCallback
 | 
						|
        public void onServicesDiscovered(BluetoothGatt bluetoothGatt, int i) {
 | 
						|
            BleLiteLog.i(BleGattClientProxyImpl.TAG, "onServicesDiscovered() status: " + BleGattStatusParser.parse(i));
 | 
						|
            BleDiscoverServiceOperation discoverServiceOperation = BleGattClientProxyImpl.this.getDiscoverServiceOperation();
 | 
						|
            if (i != 0 || discoverServiceOperation == null) {
 | 
						|
                return;
 | 
						|
            }
 | 
						|
            discoverServiceOperation.notifyLock();
 | 
						|
        }
 | 
						|
    };
 | 
						|
 | 
						|
    public BleGattClientProxyImpl(Context context) {
 | 
						|
        Log.i(TAG, "blelitelib version: v0.9.5");
 | 
						|
        this.mAppContext = context.getApplicationContext();
 | 
						|
    }
 | 
						|
 | 
						|
    /* JADX INFO: Access modifiers changed from: private */
 | 
						|
    public void __close() {
 | 
						|
        if (this.mIsClosed) {
 | 
						|
            BleLiteLog.i(TAG, "__close() it is closed already");
 | 
						|
            return;
 | 
						|
        }
 | 
						|
        this.mIsClosed = true;
 | 
						|
        BleLiteLog.i(TAG, "__close() closing");
 | 
						|
        unregister(1L);
 | 
						|
        unregister(3L);
 | 
						|
        unregister(5L);
 | 
						|
        unregister(4L);
 | 
						|
        unregister(6L);
 | 
						|
        BluetoothGatt bluetoothGatt = getBluetoothGatt();
 | 
						|
        if (bluetoothGatt == null) {
 | 
						|
            BleLiteLog.i(TAG, "__close() bluetoothGatt is null");
 | 
						|
        }
 | 
						|
        BleCloseOperation.createInstance(bluetoothGatt).run();
 | 
						|
        BleLiteLog.i(TAG, "__close() closed");
 | 
						|
    }
 | 
						|
 | 
						|
    private boolean __connect(String str, long j) {
 | 
						|
        synchronized (this.mLock4Close) {
 | 
						|
            this.mIsClosed = false;
 | 
						|
        }
 | 
						|
        this.mReconnectScheme.clearRetryCount();
 | 
						|
        Context context = this.mAppContext;
 | 
						|
        BleConnectOperation connectOperation = getConnectOperation();
 | 
						|
        if (connectOperation == null) {
 | 
						|
            connectOperation = BleConnectOperation.createInstance(context, str, this.mBluetoothGattCallback);
 | 
						|
            register(connectOperation);
 | 
						|
        } else {
 | 
						|
            BleLiteLog.w(TAG, "__connect() close old connection, it should't appear frequently.");
 | 
						|
            __close();
 | 
						|
        }
 | 
						|
        long currentTimeMillis = System.currentTimeMillis();
 | 
						|
        connectOperation.doRunnableSelfAsync(true);
 | 
						|
        connectOperation.waitLock(j);
 | 
						|
        long currentTimeMillis2 = System.currentTimeMillis() - currentTimeMillis;
 | 
						|
        setBleConnector(connectOperation.getConnector());
 | 
						|
        boolean isNotified = connectOperation.isNotified();
 | 
						|
        BleLiteLog.i(TAG, "__connect() suc: " + isNotified + ", consume: " + currentTimeMillis2 + " ms");
 | 
						|
        if (!isNotified) {
 | 
						|
            close();
 | 
						|
        }
 | 
						|
        return isNotified;
 | 
						|
    }
 | 
						|
 | 
						|
    private BluetoothGattCharacteristic __discoverCharacteristic(BluetoothGattService bluetoothGattService, UUID uuid) {
 | 
						|
        BluetoothGattCharacteristic characteristic = bluetoothGattService.getCharacteristic(uuid);
 | 
						|
        StringBuilder sb = new StringBuilder();
 | 
						|
        sb.append("__discoverCharacteristic() gattService uuid: ");
 | 
						|
        sb.append(bluetoothGattService.getUuid());
 | 
						|
        sb.append(", characteristic uuid: ");
 | 
						|
        sb.append(uuid);
 | 
						|
        sb.append(", gattCharacteristic is ");
 | 
						|
        sb.append(characteristic != null ? "found" : "missed");
 | 
						|
        BleLiteLog.i(TAG, sb.toString());
 | 
						|
        return characteristic;
 | 
						|
    }
 | 
						|
 | 
						|
    private boolean __discoverService(BluetoothGatt bluetoothGatt, long j) {
 | 
						|
        if (bluetoothGatt == null) {
 | 
						|
            BleLiteLog.w(TAG, "__discoverService() fail for bluetoothGatt is null");
 | 
						|
            return false;
 | 
						|
        }
 | 
						|
        BleDiscoverServiceOperation createInstance = BleDiscoverServiceOperation.createInstance(bluetoothGatt);
 | 
						|
        register(createInstance);
 | 
						|
        long currentTimeMillis = System.currentTimeMillis();
 | 
						|
        createInstance.doRunnableSelfAsync(true);
 | 
						|
        createInstance.waitLock(j);
 | 
						|
        long currentTimeMillis2 = System.currentTimeMillis() - currentTimeMillis;
 | 
						|
        boolean isNotified = createInstance.isNotified();
 | 
						|
        BleLiteLog.i(TAG, "__discoverService() discover services suc: " + isNotified + ", consume: " + currentTimeMillis2 + " ms");
 | 
						|
        return isNotified;
 | 
						|
    }
 | 
						|
 | 
						|
    private List<BluetoothGattService> __discoverServices(long j) {
 | 
						|
        BluetoothGatt bluetoothGatt = getBluetoothGatt();
 | 
						|
        if (bluetoothGatt == null) {
 | 
						|
            BleLiteLog.w(TAG, "__discoverServices() fail for bluetoothGatt is null");
 | 
						|
            return null;
 | 
						|
        }
 | 
						|
        if (__discoverService(bluetoothGatt, j)) {
 | 
						|
            return bluetoothGatt.getServices();
 | 
						|
        }
 | 
						|
        return null;
 | 
						|
    }
 | 
						|
 | 
						|
    private byte[] __readCharacteristic(BluetoothGattCharacteristic bluetoothGattCharacteristic, long j) {
 | 
						|
        BluetoothGatt bluetoothGatt = getBluetoothGatt();
 | 
						|
        if (bluetoothGatt == null) {
 | 
						|
            BleLiteLog.w(TAG, "__readCharacteristic() fail for bluetoothGatt is null");
 | 
						|
            return null;
 | 
						|
        }
 | 
						|
        BleReadCharacteristicOperation createInstance = BleReadCharacteristicOperation.createInstance(bluetoothGatt, bluetoothGattCharacteristic);
 | 
						|
        register(createInstance);
 | 
						|
        long currentTimeMillis = System.currentTimeMillis();
 | 
						|
        createInstance.doRunnableSelfAsync(false);
 | 
						|
        createInstance.waitLock(j);
 | 
						|
        long currentTimeMillis2 = System.currentTimeMillis() - currentTimeMillis;
 | 
						|
        byte[] result = createInstance.isNotified() ? createInstance.getResult() : null;
 | 
						|
        BleLiteLog.i(TAG, "__readCharacteristic() gattCharacteristic's uuid: " + bluetoothGattCharacteristic.getUuid() + ", consume: " + currentTimeMillis2 + " ms, msg: " + HexUtils.bytes2HexStringWithSpace(result));
 | 
						|
        return result;
 | 
						|
    }
 | 
						|
 | 
						|
    private boolean __registerCharacteristicNotification(BluetoothGattCharacteristic bluetoothGattCharacteristic, BleGattClientProxy.OnCharacteristicNotificationListener onCharacteristicNotificationListener) {
 | 
						|
        BluetoothGatt bluetoothGatt = getBluetoothGatt();
 | 
						|
        if (bluetoothGatt == null) {
 | 
						|
            BleLiteLog.w(TAG, "__registerCharacteristicNotification() fail for bluetoothGatt is null");
 | 
						|
            return false;
 | 
						|
        }
 | 
						|
        if (bluetoothGattCharacteristic == null) {
 | 
						|
            BleLiteLog.e(TAG, "__registerCharacteristicNotification() fail for characteristic is null");
 | 
						|
            return false;
 | 
						|
        }
 | 
						|
        boolean characteristicNotification = bluetoothGatt.setCharacteristicNotification(bluetoothGattCharacteristic, true);
 | 
						|
        if (characteristicNotification) {
 | 
						|
            registerListener(bluetoothGattCharacteristic.getUuid(), onCharacteristicNotificationListener);
 | 
						|
        }
 | 
						|
        BleLiteLog.i(TAG, "__registerCharacteristicNotification() characteristic's uuid: " + bluetoothGattCharacteristic.getUuid() + ", register suc: " + characteristicNotification);
 | 
						|
        BluetoothGattDescriptor descriptor = bluetoothGattCharacteristic.getDescriptor(BleUuidUtils.str2uuid(UUID_CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR));
 | 
						|
        return descriptor != null ? __writeDescriptor(descriptor, BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE, 2000L) : characteristicNotification;
 | 
						|
    }
 | 
						|
 | 
						|
    private boolean __requestMtu(int i, long j) {
 | 
						|
        if (Build.VERSION.SDK_INT < 21) {
 | 
						|
            BleLiteLog.w(TAG, "__requestMtu() fail for android version is to low(lower than 5.0 LOLLIPOP)");
 | 
						|
            return false;
 | 
						|
        }
 | 
						|
        BluetoothGatt bluetoothGatt = getBluetoothGatt();
 | 
						|
        if (bluetoothGatt == null) {
 | 
						|
            BleLiteLog.w(TAG, "__requestMtu() fail for bluetoothGatt is null");
 | 
						|
            return false;
 | 
						|
        }
 | 
						|
        BleRequestMtuOperation createInstance = BleRequestMtuOperation.createInstance(bluetoothGatt, i);
 | 
						|
        register(createInstance);
 | 
						|
        long currentTimeMillis = System.currentTimeMillis();
 | 
						|
        createInstance.doRunnableSelfAsync(false);
 | 
						|
        createInstance.waitLock(j);
 | 
						|
        long currentTimeMillis2 = System.currentTimeMillis() - currentTimeMillis;
 | 
						|
        boolean isNotified = createInstance.isNotified();
 | 
						|
        BleLiteLog.i(TAG, "__requestMtu() mtu: " + i + " suc: " + isNotified + ", consume: " + currentTimeMillis2 + " ms");
 | 
						|
        return isNotified;
 | 
						|
    }
 | 
						|
 | 
						|
    private void __unregisterCharacteristicNotification(UUID uuid) {
 | 
						|
        unregisterListener(uuid);
 | 
						|
    }
 | 
						|
 | 
						|
    private boolean __writeCharacterisitcNoResponsePacket2(BluetoothGattCharacteristic bluetoothGattCharacteristic, byte[] bArr) {
 | 
						|
        BluetoothGatt bluetoothGatt = getBluetoothGatt();
 | 
						|
        if (bluetoothGatt == null) {
 | 
						|
            BleLiteLog.w(TAG, "__writeCharacterisitcNoResponsePacket2() fail for bluetoothGatt is null");
 | 
						|
            return false;
 | 
						|
        }
 | 
						|
        BleWriteCharacterisitcNoResponsePacketOperation2.createInstance(bluetoothGatt, bluetoothGattCharacteristic, bArr).doRunnableSelfAsync(false);
 | 
						|
        return true;
 | 
						|
    }
 | 
						|
 | 
						|
    private boolean __writeCharacteristic(BluetoothGattCharacteristic bluetoothGattCharacteristic, byte[] bArr, long j) {
 | 
						|
        BluetoothGatt bluetoothGatt = getBluetoothGatt();
 | 
						|
        if (bluetoothGatt == null) {
 | 
						|
            BleLiteLog.w(TAG, "__writeCharacteristic() fail for bluetoothGatt is null");
 | 
						|
            return false;
 | 
						|
        }
 | 
						|
        BleWriteCharacteristicOperation createInstance = BleWriteCharacteristicOperation.createInstance(bluetoothGatt, bluetoothGattCharacteristic, bArr);
 | 
						|
        register(createInstance);
 | 
						|
        long currentTimeMillis = System.currentTimeMillis();
 | 
						|
        createInstance.doRunnableSelfAsync(false);
 | 
						|
        createInstance.waitLock(j);
 | 
						|
        long currentTimeMillis2 = System.currentTimeMillis() - currentTimeMillis;
 | 
						|
        boolean isNotified = createInstance.isNotified();
 | 
						|
        BleLiteLog.i(TAG, "__writeCharacteristic() msg: " + HexUtils.bytes2HexStringWithSpace(bArr) + " suc: " + isNotified + ", consume: " + currentTimeMillis2 + " ms");
 | 
						|
        return isNotified;
 | 
						|
    }
 | 
						|
 | 
						|
    private boolean __writeCharacteristicNoResponse(BluetoothGattCharacteristic bluetoothGattCharacteristic, byte[] bArr, long j) {
 | 
						|
        BluetoothGatt bluetoothGatt = getBluetoothGatt();
 | 
						|
        if (bluetoothGatt == null) {
 | 
						|
            BleLiteLog.w(TAG, "__writeCharacteristicNoResponse() fail for bluetoothGatt is null");
 | 
						|
            return false;
 | 
						|
        }
 | 
						|
        BleWriteCharacteristicNoResponseOperation.createInstance(bluetoothGatt, bluetoothGattCharacteristic, bArr).doRunnableSelfAsync(false);
 | 
						|
        if (j > 0) {
 | 
						|
            try {
 | 
						|
                Thread.sleep(j);
 | 
						|
            } catch (InterruptedException unused) {
 | 
						|
                Thread.currentThread().interrupt();
 | 
						|
                return false;
 | 
						|
            }
 | 
						|
        }
 | 
						|
        BleLiteLog.i(TAG, "__writeCharacteristicNoResponse() msg: " + HexUtils.bytes2HexStringWithSpace(bArr) + " suc: true, interval: " + j + " ms");
 | 
						|
        return true;
 | 
						|
    }
 | 
						|
 | 
						|
    private boolean __writeCharacteristicNoResponse20(BluetoothGattCharacteristic bluetoothGattCharacteristic, byte[] bArr) {
 | 
						|
        BluetoothGatt bluetoothGatt = getBluetoothGatt();
 | 
						|
        if (bluetoothGatt == null) {
 | 
						|
            BleLiteLog.w(TAG, "__writeCharacteristicNoResponse20() fail for bluetoothGatt is null");
 | 
						|
            return false;
 | 
						|
        }
 | 
						|
        BleWriteCharacteristicNoResponse20Operation.createInstance(bluetoothGatt, bluetoothGattCharacteristic, bArr).doRunnableSelfAsync(false);
 | 
						|
        return true;
 | 
						|
    }
 | 
						|
 | 
						|
    private boolean __writeCharacteristicNoResponsePacket(BluetoothGattCharacteristic bluetoothGattCharacteristic, byte[] bArr, int i, int i2) {
 | 
						|
        BluetoothGatt bluetoothGatt = getBluetoothGatt();
 | 
						|
        if (bluetoothGatt == null) {
 | 
						|
            BleLiteLog.w(TAG, "__writeCharacteristicNoResponsePacket() fail for bluetoothGatt is null");
 | 
						|
            return false;
 | 
						|
        }
 | 
						|
        BleWriteCharacterisitcNoResponsePacketOperation.createInstance(bluetoothGatt, bluetoothGattCharacteristic, bArr, i, i2).doRunnableSelfAsync(false);
 | 
						|
        return true;
 | 
						|
    }
 | 
						|
 | 
						|
    private boolean __writeCharacteristicNoResponsePreemptible(BluetoothGattCharacteristic bluetoothGattCharacteristic, byte[] bArr) {
 | 
						|
        BluetoothGatt bluetoothGatt = getBluetoothGatt();
 | 
						|
        if (bluetoothGatt == null) {
 | 
						|
            BleLiteLog.w(TAG, "__writeCharacteristicNoResponsePreemptible() fail for bluetoothGatt is null");
 | 
						|
            return false;
 | 
						|
        }
 | 
						|
        BleWriteCharacteristicNoResponseInterruptOperation createInstance = BleWriteCharacteristicNoResponseInterruptOperation.createInstance(bluetoothGatt, bluetoothGattCharacteristic, bArr);
 | 
						|
        createInstance.doRunnableSelfAsyncInterruptable(createInstance.getOperatcionCode());
 | 
						|
        return true;
 | 
						|
    }
 | 
						|
 | 
						|
    private boolean __writeDescriptor(BluetoothGattDescriptor bluetoothGattDescriptor, byte[] bArr, long j) {
 | 
						|
        BluetoothGatt bluetoothGatt = getBluetoothGatt();
 | 
						|
        if (bluetoothGatt == null) {
 | 
						|
            BleLiteLog.w(TAG, "__writeDescriptor() fail for bluetoothGatt is null");
 | 
						|
            return false;
 | 
						|
        }
 | 
						|
        BleWriteDescriptorOperation createInstance = BleWriteDescriptorOperation.createInstance(bluetoothGatt, bluetoothGattDescriptor, bArr);
 | 
						|
        register(createInstance);
 | 
						|
        long currentTimeMillis = System.currentTimeMillis();
 | 
						|
        createInstance.doRunnableSelfAsync(false);
 | 
						|
        createInstance.waitLock(j);
 | 
						|
        long currentTimeMillis2 = System.currentTimeMillis() - currentTimeMillis;
 | 
						|
        boolean isNotified = createInstance.isNotified();
 | 
						|
        BleLiteLog.i(TAG, "__writeDescriptor() msg: " + HexUtils.bytes2HexStringWithSpace(bArr) + " suc: " + isNotified + ", consume: " + currentTimeMillis2 + " ms");
 | 
						|
        return isNotified;
 | 
						|
    }
 | 
						|
 | 
						|
    private BluetoothGatt getBluetoothGatt() {
 | 
						|
        BleConnector bleConnector = this.mBleConnector;
 | 
						|
        if (bleConnector != null) {
 | 
						|
            return bleConnector.getBluetoothGatt();
 | 
						|
        }
 | 
						|
        return null;
 | 
						|
    }
 | 
						|
 | 
						|
    /* JADX INFO: Access modifiers changed from: private */
 | 
						|
    public BleConnectOperation getConnectOperation() {
 | 
						|
        BleOperation bleOperation = this.mOperations.get(1L);
 | 
						|
        if (bleOperation != null) {
 | 
						|
            return (BleConnectOperation) bleOperation;
 | 
						|
        }
 | 
						|
        return null;
 | 
						|
    }
 | 
						|
 | 
						|
    /* JADX INFO: Access modifiers changed from: private */
 | 
						|
    public BleDiscoverServiceOperation getDiscoverServiceOperation() {
 | 
						|
        BleOperation bleOperation = this.mOperations.get(3L);
 | 
						|
        if (bleOperation != null) {
 | 
						|
            return (BleDiscoverServiceOperation) bleOperation;
 | 
						|
        }
 | 
						|
        return null;
 | 
						|
    }
 | 
						|
 | 
						|
    /* JADX INFO: Access modifiers changed from: private */
 | 
						|
    public BleGattClientProxy.OnCharacteristicNotificationListener getListener(UUID uuid) {
 | 
						|
        return this.mListeners.get(BleUuidUtils.uuid2int(uuid));
 | 
						|
    }
 | 
						|
 | 
						|
    /* JADX INFO: Access modifiers changed from: private */
 | 
						|
    public BleReadCharacteristicOperation getReadCharacteristicOperation() {
 | 
						|
        BleOperation bleOperation = this.mOperations.get(4L);
 | 
						|
        if (bleOperation != null) {
 | 
						|
            return (BleReadCharacteristicOperation) bleOperation;
 | 
						|
        }
 | 
						|
        return null;
 | 
						|
    }
 | 
						|
 | 
						|
    /* JADX INFO: Access modifiers changed from: private */
 | 
						|
    public BleRequestMtuOperation getRequestMtuOperation() {
 | 
						|
        BleOperation bleOperation = this.mOperations.get(5L);
 | 
						|
        if (bleOperation != null) {
 | 
						|
            return (BleRequestMtuOperation) bleOperation;
 | 
						|
        }
 | 
						|
        return null;
 | 
						|
    }
 | 
						|
 | 
						|
    /* JADX INFO: Access modifiers changed from: private */
 | 
						|
    public BleWriteCharacteristicOperation getWriteCharacteristicOperation() {
 | 
						|
        BleOperation bleOperation = this.mOperations.get(6L);
 | 
						|
        if (bleOperation != null) {
 | 
						|
            return (BleWriteCharacteristicOperation) bleOperation;
 | 
						|
        }
 | 
						|
        return null;
 | 
						|
    }
 | 
						|
 | 
						|
    /* JADX INFO: Access modifiers changed from: private */
 | 
						|
    public BleWriteDescriptorOperation getWriteDescriptorOperation() {
 | 
						|
        BleOperation bleOperation = this.mOperations.get(7L);
 | 
						|
        if (bleOperation != null) {
 | 
						|
            return (BleWriteDescriptorOperation) bleOperation;
 | 
						|
        }
 | 
						|
        return null;
 | 
						|
    }
 | 
						|
 | 
						|
    private void register(BleOperation bleOperation) {
 | 
						|
        BleLiteLog.d(TAG, "register() operation: " + bleOperation);
 | 
						|
        this.mOperations.put((long) bleOperation.getOperatcionCode(), bleOperation);
 | 
						|
    }
 | 
						|
 | 
						|
    private void registerListener(UUID uuid, BleGattClientProxy.OnCharacteristicNotificationListener onCharacteristicNotificationListener) {
 | 
						|
        long uuid2int = BleUuidUtils.uuid2int(uuid);
 | 
						|
        BleLiteLog.d(TAG, "registerListener() uuid: " + uuid + ", key: " + uuid2int);
 | 
						|
        this.mListeners.put(uuid2int, onCharacteristicNotificationListener);
 | 
						|
    }
 | 
						|
 | 
						|
    private void setBleConnector(BleConnector bleConnector) {
 | 
						|
        this.mBleConnector = bleConnector;
 | 
						|
    }
 | 
						|
 | 
						|
    private void unregister(long j) {
 | 
						|
        BleLiteLog.d(TAG, "unregister() operationCode: " + j);
 | 
						|
        this.mOperations.remove(j);
 | 
						|
    }
 | 
						|
 | 
						|
    private void unregisterListener(UUID uuid) {
 | 
						|
        long uuid2int = BleUuidUtils.uuid2int(uuid);
 | 
						|
        BleLiteLog.d(TAG, "unregisterListener() uuid: " + uuid + ", key: " + uuid2int);
 | 
						|
        this.mListeners.remove(uuid2int);
 | 
						|
    }
 | 
						|
 | 
						|
    @Override // com.afunx.ble.blelitelib.proxy.BleGattClientProxy
 | 
						|
    public void close() {
 | 
						|
        synchronized (this.mLock4Close) {
 | 
						|
            __close();
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    @Override // com.afunx.ble.blelitelib.proxy.BleGattClientProxy
 | 
						|
    public boolean connect(String str, long j) {
 | 
						|
        boolean __connect;
 | 
						|
        synchronized (this.mLock4Connect) {
 | 
						|
            __connect = __connect(str, j);
 | 
						|
        }
 | 
						|
        return __connect;
 | 
						|
    }
 | 
						|
 | 
						|
    @Override // com.afunx.ble.blelitelib.proxy.BleGattClientProxy
 | 
						|
    public BluetoothGattCharacteristic discoverCharacteristic(BluetoothGattService bluetoothGattService, UUID uuid) {
 | 
						|
        return __discoverCharacteristic(bluetoothGattService, uuid);
 | 
						|
    }
 | 
						|
 | 
						|
    @Override // com.afunx.ble.blelitelib.proxy.BleGattClientProxy
 | 
						|
    public BluetoothGattService discoverService(UUID uuid, long j) {
 | 
						|
        return __discoverService(uuid, j);
 | 
						|
    }
 | 
						|
 | 
						|
    @Override // com.afunx.ble.blelitelib.proxy.BleGattClientProxy
 | 
						|
    public List<BluetoothGattService> discoverServices(long j) {
 | 
						|
        return __discoverServices(j);
 | 
						|
    }
 | 
						|
 | 
						|
    @Override // com.afunx.ble.blelitelib.proxy.BleGattClientProxy
 | 
						|
    public byte[] readCharacteristic(BluetoothGattCharacteristic bluetoothGattCharacteristic, long j) {
 | 
						|
        return __readCharacteristic(bluetoothGattCharacteristic, j);
 | 
						|
    }
 | 
						|
 | 
						|
    @Override // com.afunx.ble.blelitelib.proxy.BleGattClientProxy
 | 
						|
    public boolean registerCharacteristicNotification(BluetoothGattCharacteristic bluetoothGattCharacteristic, BleGattClientProxy.OnCharacteristicNotificationListener onCharacteristicNotificationListener) {
 | 
						|
        return __registerCharacteristicNotification(bluetoothGattCharacteristic, onCharacteristicNotificationListener);
 | 
						|
    }
 | 
						|
 | 
						|
    @Override // com.afunx.ble.blelitelib.proxy.BleGattClientProxy
 | 
						|
    public boolean requestMtu(int i, long j) {
 | 
						|
        return __requestMtu(i, j);
 | 
						|
    }
 | 
						|
 | 
						|
    @Override // com.afunx.ble.blelitelib.proxy.BleGattClientProxy
 | 
						|
    public void setDisconnectCallback(Runnable runnable, long j) {
 | 
						|
        this.mReconnectScheme.setDisconnectCallback(runnable, j);
 | 
						|
    }
 | 
						|
 | 
						|
    @Override // com.afunx.ble.blelitelib.proxy.BleGattClientProxy
 | 
						|
    public void unregisterCharacteristicNotification(UUID uuid) {
 | 
						|
        __unregisterCharacteristicNotification(uuid);
 | 
						|
    }
 | 
						|
 | 
						|
    @Override // com.afunx.ble.blelitelib.proxy.BleGattClientProxy
 | 
						|
    public boolean writeCharacterisitcNoResponse2(BluetoothGattCharacteristic bluetoothGattCharacteristic, byte[] bArr) {
 | 
						|
        return __writeCharacterisitcNoResponsePacket2(bluetoothGattCharacteristic, bArr);
 | 
						|
    }
 | 
						|
 | 
						|
    @Override // com.afunx.ble.blelitelib.proxy.BleGattClientProxy
 | 
						|
    public boolean writeCharacteristic(BluetoothGattCharacteristic bluetoothGattCharacteristic, byte[] bArr, long j) {
 | 
						|
        return __writeCharacteristic(bluetoothGattCharacteristic, bArr, j);
 | 
						|
    }
 | 
						|
 | 
						|
    @Override // com.afunx.ble.blelitelib.proxy.BleGattClientProxy
 | 
						|
    public boolean writeCharacteristicNoResponse(BluetoothGattCharacteristic bluetoothGattCharacteristic, byte[] bArr, long j) {
 | 
						|
        return __writeCharacteristicNoResponse(bluetoothGattCharacteristic, bArr, j);
 | 
						|
    }
 | 
						|
 | 
						|
    @Override // com.afunx.ble.blelitelib.proxy.BleGattClientProxy
 | 
						|
    public boolean writeCharacteristicNoResponse20(BluetoothGattCharacteristic bluetoothGattCharacteristic, byte[] bArr) {
 | 
						|
        return __writeCharacteristicNoResponse20(bluetoothGattCharacteristic, bArr);
 | 
						|
    }
 | 
						|
 | 
						|
    @Override // com.afunx.ble.blelitelib.proxy.BleGattClientProxy
 | 
						|
    public boolean writeCharacteristicNoResponsePacket(BluetoothGattCharacteristic bluetoothGattCharacteristic, byte[] bArr, int i, int i2) {
 | 
						|
        return __writeCharacteristicNoResponsePacket(bluetoothGattCharacteristic, bArr, i, i2);
 | 
						|
    }
 | 
						|
 | 
						|
    @Override // com.afunx.ble.blelitelib.proxy.BleGattClientProxy
 | 
						|
    public boolean writeCharacteristicNoResponsePreemptible(BluetoothGattCharacteristic bluetoothGattCharacteristic, byte[] bArr) {
 | 
						|
        return __writeCharacteristicNoResponsePreemptible(bluetoothGattCharacteristic, bArr);
 | 
						|
    }
 | 
						|
 | 
						|
    private BluetoothGattService __discoverService(UUID uuid, long j) {
 | 
						|
        BluetoothGatt bluetoothGatt = getBluetoothGatt();
 | 
						|
        if (bluetoothGatt == null) {
 | 
						|
            BleLiteLog.w(TAG, "__discoverService() fail for bluetoothGatt is null");
 | 
						|
            return null;
 | 
						|
        }
 | 
						|
        BluetoothGattService service = bluetoothGatt.getService(uuid);
 | 
						|
        if (service != null) {
 | 
						|
            BleLiteLog.i(TAG, "__discoverService() uuid: " + uuid + ", gattService is exist already");
 | 
						|
            return service;
 | 
						|
        }
 | 
						|
        if (__discoverService(bluetoothGatt, j)) {
 | 
						|
            service = bluetoothGatt.getService(uuid);
 | 
						|
            StringBuilder sb = new StringBuilder();
 | 
						|
            sb.append("__discoverService() uuid: ");
 | 
						|
            sb.append(uuid);
 | 
						|
            sb.append(", gattService is ");
 | 
						|
            sb.append(service != null ? "found" : "missed");
 | 
						|
            BleLiteLog.i(TAG, sb.toString());
 | 
						|
        }
 | 
						|
        return service;
 | 
						|
    }
 | 
						|
}
 |