Fixed bug 4320 - Android remove reflection for HIDDeviceBLESteamController
authorSam Lantinga <slouken@libsdl.org>
Fri, 02 Nov 2018 17:25:00 -0700
changeset 123895817dbd75619
parent 12388 69af2eac84e9
child 12390 60f322c5984f
Fixed bug 4320 - Android remove reflection for HIDDeviceBLESteamController

Sylvain

Uneeded use of reflection to access connectGatt method in HIDDeviceBLESteamController.java

The method is API 23

https://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#connectGatt(android.content.Context,%20boolean,%20android.bluetooth.BluetoothGattCallback,%20int)
android-project/app/src/main/java/org/libsdl/app/HIDDeviceBLESteamController.java
     1.1 --- a/android-project/app/src/main/java/org/libsdl/app/HIDDeviceBLESteamController.java	Fri Nov 02 17:22:15 2018 -0700
     1.2 +++ b/android-project/app/src/main/java/org/libsdl/app/HIDDeviceBLESteamController.java	Fri Nov 02 17:25:00 2018 -0700
     1.3 @@ -12,12 +12,11 @@
     1.4  import android.os.Handler;
     1.5  import android.os.Looper;
     1.6  import android.util.Log;
     1.7 +import android.os.*;
     1.8  
     1.9  //import com.android.internal.util.HexDump;
    1.10  
    1.11  import java.lang.Runnable;
    1.12 -import java.lang.reflect.InvocationTargetException;
    1.13 -import java.lang.reflect.Method;
    1.14  import java.util.Arrays;
    1.15  import java.util.LinkedList;
    1.16  import java.util.UUID;
    1.17 @@ -186,10 +185,13 @@
    1.18      // Because on Chromebooks we show up as a dual-mode device, it will attempt to connect TRANSPORT_AUTO, which will use TRANSPORT_BREDR instead
    1.19      // of TRANSPORT_LE.  Let's force ourselves to connect low energy.
    1.20      private BluetoothGatt connectGatt(boolean managed) {
    1.21 -        try {
    1.22 -            Method m = mDevice.getClass().getDeclaredMethod("connectGatt", Context.class, boolean.class, BluetoothGattCallback.class, int.class);
    1.23 -            return (BluetoothGatt) m.invoke(mDevice, mManager.getContext(), managed, this, TRANSPORT_LE);
    1.24 -        } catch (Exception e) {
    1.25 +        if (Build.VERSION.SDK_INT >= 23) {
    1.26 +            try {
    1.27 +                return mDevice.connectGatt(mManager.getContext(), managed, this, TRANSPORT_LE);
    1.28 +            } catch (Exception e) {
    1.29 +                return mDevice.connectGatt(mManager.getContext(), managed, this);
    1.30 +            }
    1.31 +        } else {
    1.32              return mDevice.connectGatt(mManager.getContext(), managed, this);
    1.33          }
    1.34      }