• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.shadows;
2 
3 import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
4 import static android.os.Build.VERSION_CODES.LOLLIPOP;
5 import static android.os.Build.VERSION_CODES.O;
6 import static android.os.Build.VERSION_CODES.O_MR1;
7 
8 import android.annotation.SuppressLint;
9 import android.bluetooth.BluetoothDevice;
10 import android.bluetooth.BluetoothGatt;
11 import android.content.Context;
12 import android.os.Build;
13 import org.robolectric.RuntimeEnvironment;
14 import org.robolectric.annotation.Implements;
15 import org.robolectric.shadow.api.Shadow;
16 
17 @Implements(value = BluetoothGatt.class, minSdk = JELLY_BEAN_MR2)
18 public class ShadowBluetoothGatt {
19 
20   @SuppressLint("PrivateApi")
21   @SuppressWarnings("unchecked")
newInstance(BluetoothDevice device)22   public static BluetoothGatt newInstance(BluetoothDevice device) {
23     try {
24       Class<?> iBluetoothGattClass =
25           Shadow.class.getClassLoader().loadClass("android.bluetooth.IBluetoothGatt");
26 
27       BluetoothGatt bluetoothGatt;
28       if (Build.VERSION.SDK_INT >= O_MR1) {
29         bluetoothGatt =
30             Shadow.newInstance(
31                 BluetoothGatt.class,
32                 new Class<?>[] {
33                   iBluetoothGattClass,
34                   BluetoothDevice.class,
35                   Integer.TYPE,
36                   Boolean.TYPE,
37                   Integer.TYPE
38                 },
39                 new Object[] {null, device, 0, false, 0});
40       } else if (Build.VERSION.SDK_INT >= O) {
41         bluetoothGatt =
42             Shadow.newInstance(
43                 BluetoothGatt.class,
44                 new Class<?>[] {
45                   iBluetoothGattClass, BluetoothDevice.class, Integer.TYPE, Integer.TYPE
46                 },
47                 new Object[] {null, device, 0, 0});
48       } else if (Build.VERSION.SDK_INT >= LOLLIPOP) {
49         bluetoothGatt =
50             Shadow.newInstance(
51                 BluetoothGatt.class,
52                 new Class<?>[] {
53                   Context.class, iBluetoothGattClass, BluetoothDevice.class, Integer.TYPE
54                 },
55                 new Object[] {RuntimeEnvironment.application, null, device, 0});
56       } else {
57         bluetoothGatt =
58             Shadow.newInstance(
59                 BluetoothGatt.class,
60                 new Class<?>[] {Context.class, iBluetoothGattClass, BluetoothDevice.class},
61                 new Object[] {RuntimeEnvironment.application, null, device});
62       }
63       return bluetoothGatt;
64     } catch (ClassNotFoundException e) {
65       throw new RuntimeException(e);
66     }
67   }
68 }
69