jimu-decompiled/sources/com/afunx/ble/blelitelib/connector/BleConnectCompat.java
2025-05-13 19:24:51 +02:00

118 lines
6.3 KiB
Java

package com.afunx.ble.blelitelib.connector;
import android.annotation.TargetApi;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.content.Context;
import android.os.Build;
import com.afunx.ble.blelitelib.log.BleLiteLog;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/* loaded from: classes.dex */
public class BleConnectCompat {
private static final String TAG = "BleConnectCompat";
private final Context context;
public BleConnectCompat(Context context) {
this.context = context;
}
private BluetoothGatt connectGattCompat(BluetoothGattCallback bluetoothGattCallback, BluetoothDevice bluetoothDevice, boolean z) {
BleLiteLog.v(TAG, "Connecting without reflection");
return Build.VERSION.SDK_INT >= 23 ? bluetoothDevice.connectGatt(this.context, z, bluetoothGattCallback, 2) : bluetoothDevice.connectGatt(this.context, z, bluetoothGattCallback);
}
private boolean connectUsingReflection(BluetoothGatt bluetoothGatt, BluetoothGattCallback bluetoothGattCallback, boolean z) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, NoSuchFieldException {
BleLiteLog.v(TAG, "Connecting using reflection");
setAutoConnectValue(bluetoothGatt, z);
Method declaredMethod = bluetoothGatt.getClass().getDeclaredMethod("connect", Boolean.class, BluetoothGattCallback.class);
declaredMethod.setAccessible(true);
return ((Boolean) declaredMethod.invoke(bluetoothGatt, true, bluetoothGattCallback)).booleanValue();
}
private BluetoothGatt connectUsingReflectionx(BluetoothDevice bluetoothDevice, BluetoothGattCallback bluetoothGattCallback, boolean z) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, NoSuchFieldException {
BleLiteLog.v(TAG, "Connecting using reflection");
Method declaredMethod = bluetoothDevice.getClass().getDeclaredMethod("connectGatt", Context.class, Boolean.TYPE, BluetoothGattCallback.class, Integer.TYPE);
declaredMethod.setAccessible(true);
return (BluetoothGatt) declaredMethod.invoke(bluetoothDevice, this.context, true, bluetoothGattCallback, 2);
}
@TargetApi(23)
private BluetoothGatt createBluetoothGatt(Object obj, BluetoothDevice bluetoothDevice) throws IllegalAccessException, InvocationTargetException, InstantiationException {
Constructor<?> constructor = BluetoothGatt.class.getDeclaredConstructors()[0];
constructor.setAccessible(true);
BleLiteLog.v(TAG, "Found constructor with args count = \" + bluetoothGattConstructor.getParameterTypes().length");
return constructor.getParameterTypes().length == 4 ? (BluetoothGatt) constructor.newInstance(this.context, obj, bluetoothDevice, 2) : (BluetoothGatt) constructor.newInstance(this.context, obj, bluetoothDevice);
}
private Object getIBluetoothGatt(Object obj) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
if (obj == null) {
return null;
}
return getMethodFromClass(obj.getClass(), "getBluetoothGatt").invoke(obj, new Object[0]);
}
private Object getIBluetoothManager() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();
if (defaultAdapter == null) {
return null;
}
return getMethodFromClass(defaultAdapter.getClass(), "getBluetoothManager").invoke(defaultAdapter, new Object[0]);
}
private Method getMethodFromClass(Class<?> cls, String str) throws NoSuchMethodException {
Method declaredMethod = cls.getDeclaredMethod(str, new Class[0]);
declaredMethod.setAccessible(true);
return declaredMethod;
}
private void setAutoConnectValue(BluetoothGatt bluetoothGatt, boolean z) throws NoSuchFieldException, IllegalAccessException {
Field declaredField = bluetoothGatt.getClass().getDeclaredField("mAutoConnect");
declaredField.setAccessible(true);
declaredField.setBoolean(bluetoothGatt, z);
}
public BluetoothGatt connectGatt(BluetoothDevice bluetoothDevice, boolean z, BluetoothGattCallback bluetoothGattCallback) {
int i;
if (bluetoothDevice == null) {
return null;
}
if (Build.VERSION.SDK_INT >= 24 || !z) {
return connectGattCompat(bluetoothGattCallback, bluetoothDevice, false);
}
String str = Build.PRODUCT;
if (str != null && str.toUpperCase().contains("LenovoTB3".toUpperCase()) && (i = Build.VERSION.SDK_INT) >= 21 && i < 23) {
return connectGattCompat(bluetoothGattCallback, bluetoothDevice, false);
}
try {
BleLiteLog.v(TAG, "Trying to connectGatt using reflection.");
if (Build.VERSION.SDK_INT >= 21 && Build.VERSION.SDK_INT < 23) {
return connectUsingReflectionx(bluetoothDevice, bluetoothGattCallback, z);
}
Object iBluetoothGatt = getIBluetoothGatt(getIBluetoothManager());
if (iBluetoothGatt == null) {
BleLiteLog.v(TAG, "Couldn't get iBluetoothGatt object");
return connectGattCompat(bluetoothGattCallback, bluetoothDevice, true);
}
BluetoothGatt createBluetoothGatt = createBluetoothGatt(iBluetoothGatt, bluetoothDevice);
if (createBluetoothGatt == null) {
BleLiteLog.v(TAG, "Couldn't create BluetoothGatt object");
return connectGattCompat(bluetoothGattCallback, bluetoothDevice, true);
}
if (!connectUsingReflection(createBluetoothGatt, bluetoothGattCallback, true)) {
BleLiteLog.v(TAG, "Connection using reflection failed, closing gatt");
createBluetoothGatt.close();
}
return createBluetoothGatt;
} catch (IllegalAccessException | IllegalArgumentException | InstantiationException | NoSuchFieldException | NoSuchMethodException | InvocationTargetException unused) {
BleLiteLog.v(TAG, "Error during reflection");
return connectGattCompat(bluetoothGattCallback, bluetoothDevice, true);
}
}
}