aboutsummaryrefslogtreecommitdiff
path: root/shadows/framework/src/main/java/org/robolectric/shadows/ShadowBluetoothAdapter.java
blob: 84bf1ca8c6f260d463cdca814a11567e6bad4524 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
package org.robolectric.shadows;

import static android.bluetooth.BluetoothAdapter.STATE_ON;
import static android.os.Build.VERSION_CODES.M;
import static android.os.Build.VERSION_CODES.O;
import static android.os.Build.VERSION_CODES.Q;
import static android.os.Build.VERSION_CODES.R;
import static android.os.Build.VERSION_CODES.S_V2;
import static android.os.Build.VERSION_CODES.TIRAMISU;
import static android.os.Build.VERSION_CODES.UPSIDE_DOWN_CAKE;
import static org.robolectric.Shadows.shadowOf;
import static org.robolectric.util.reflector.Reflector.reflector;

import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothAdapter.LeScanCallback;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.bluetooth.BluetoothStatusCodes;
import android.bluetooth.IBluetoothManager;
import android.bluetooth.IBluetoothProfileServiceConnection;
import android.bluetooth.le.BluetoothLeAdvertiser;
import android.bluetooth.le.BluetoothLeScanner;
import android.content.AttributionSource;
import android.content.Context;
import android.os.Build;
import android.os.Build.VERSION_CODES;
import android.os.ParcelUuid;
import android.os.RemoteException;
import android.provider.Settings;
import com.google.common.collect.ImmutableList;
import java.io.IOException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import javax.annotation.Nullable;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.RealObject;
import org.robolectric.annotation.Resetter;
import org.robolectric.util.ReflectionHelpers;
import org.robolectric.util.ReflectionHelpers.ClassParameter;
import org.robolectric.util.reflector.Accessor;
import org.robolectric.util.reflector.Direct;
import org.robolectric.util.reflector.ForType;
import org.robolectric.util.reflector.Static;

@SuppressWarnings({"UnusedDeclaration"})
@Implements(value = BluetoothAdapter.class, looseSignatures = true)
public class ShadowBluetoothAdapter {
  @RealObject private BluetoothAdapter realAdapter;

  private static final int ADDRESS_LENGTH = 17;
  private static final int LE_MAXIMUM_ADVERTISING_DATA_LENGTH = 31;
  private static final int LE_MAXIMUM_ADVERTISING_DATA_LENGTH_EXTENDED = 1650;

  /**
   * Equivalent value to internal SystemApi {@link
   * BluetoothStatusCodes#RFCOMM_LISTENER_START_FAILED_UUID_IN_USE}.
   */
  public static final int RFCOMM_LISTENER_START_FAILED_UUID_IN_USE = 2000;

  /**
   * Equivalent value to internal SystemApi {@link
   * BluetoothStatusCodes#RFCOMM_LISTENER_OPERATION_FAILED_NO_MATCHING_SERVICE_RECORD}.
   */
  public static final int RFCOMM_LISTENER_OPERATION_FAILED_NO_MATCHING_SERVICE_RECORD = 2001;

  /**
   * Equivalent value to internal SystemApi {@link
   * BluetoothStatusCodes#RFCOMM_LISTENER_FAILED_TO_CLOSE_SERVER_SOCKET}.
   */
  public static final int RFCOMM_LISTENER_FAILED_TO_CLOSE_SERVER_SOCKET = 2004;

  private static boolean isBluetoothSupported = true;

  private static final Map<String, BluetoothDevice> deviceCache = new HashMap<>();
  private Set<BluetoothDevice> bondedDevices = new HashSet<BluetoothDevice>();
  private List<BluetoothDevice> mostRecentlyConnectedDevices = new ArrayList<>();
  private Set<LeScanCallback> leScanCallbacks = new HashSet<LeScanCallback>();
  private boolean isDiscovering;
  private String address;
  private int state;
  private String name = "DefaultBluetoothDeviceName";
  private int scanMode = BluetoothAdapter.SCAN_MODE_NONE;
  private Duration discoverableTimeout;
  private boolean isBleScanAlwaysAvailable = true;
  private boolean isMultipleAdvertisementSupported = true;
  private int isLeAudioSupported = BluetoothStatusCodes.FEATURE_NOT_SUPPORTED;
  private int isDistanceMeasurementSupported = BluetoothStatusCodes.FEATURE_NOT_SUPPORTED;
  private boolean isLeExtendedAdvertisingSupported = true;
  private boolean isLeCodedPhySupported = true;
  private boolean isLe2MPhySupported = true;
  private boolean isOverridingProxyBehavior;
  private final Map<Integer, Integer> profileConnectionStateData = new HashMap<>();
  private final Map<Integer, BluetoothProfile> profileProxies = new HashMap<>();
  private final ConcurrentMap<UUID, BackgroundRfcommServerEntry> backgroundRfcommServers =
      new ConcurrentHashMap<>();
  private final Map<Integer, List<BluetoothProfile.ServiceListener>>
      bluetoothProfileServiceListeners = new HashMap<>();

  @Resetter
  public static void reset() {
    setIsBluetoothSupported(true);
    BluetoothAdapterReflector bluetoothReflector = reflector(BluetoothAdapterReflector.class);
    int apiLevel = RuntimeEnvironment.getApiLevel();
    if (apiLevel <= VERSION_CODES.R) {
      bluetoothReflector.setSBluetoothLeAdvertiser(null);
      bluetoothReflector.setSBluetoothLeScanner(null);
    }
    bluetoothReflector.setAdapter(null);
    deviceCache.clear();
  }

  @Implementation
  protected static BluetoothAdapter getDefaultAdapter() {
    if (!isBluetoothSupported) {
      return null;
    }
    return reflector(BluetoothAdapterReflector.class).getDefaultAdapter();
  }

  /** Requires LooseSignatures because of {@link AttributionSource} parameter */
  @Implementation(minSdk = VERSION_CODES.TIRAMISU)
  protected static Object createAdapter(Object attributionSource) {
    IBluetoothManager service =
        ReflectionHelpers.createDelegatingProxy(
            IBluetoothManager.class, new BluetoothManagerDelegate());
    return ReflectionHelpers.callConstructor(
        BluetoothAdapter.class,
        ClassParameter.from(IBluetoothManager.class, service),
        ClassParameter.from(AttributionSource.class, attributionSource));
  }

  /** Sets whether the Le Audio is supported or not. Minimum sdk version required is TIRAMISU. */
  public void setLeAudioSupported(int supported) {
    isLeAudioSupported = supported;
  }

  @Implementation(minSdk = VERSION_CODES.TIRAMISU)
  protected int isLeAudioSupported() {
    return isLeAudioSupported;
  }

  /** Determines if getDefaultAdapter() returns the default local adapter (true) or null (false). */
  public static void setIsBluetoothSupported(boolean supported) {
    isBluetoothSupported = supported;
  }

  /**
   * Sets whether the distance measurement is supported or not. Minimum sdk version required is
   * UPSIDE_DOWN_CAKE.
   */
  public void setDistanceMeasurementSupported(int supported) {
    isDistanceMeasurementSupported = supported;
  }

  @Implementation(minSdk = VERSION_CODES.UPSIDE_DOWN_CAKE)
  protected int isDistanceMeasurementSupported() {
    return isDistanceMeasurementSupported;
  }

  /**
   * @deprecated use real BluetoothLeAdvertiser instead
   */
  @Deprecated
  public void setBluetoothLeAdvertiser(BluetoothLeAdvertiser advertiser) {
    if (RuntimeEnvironment.getApiLevel() <= VERSION_CODES.LOLLIPOP_MR1) {
      reflector(BluetoothAdapterReflector.class, realAdapter).setSBluetoothLeAdvertiser(advertiser);
    } else {
      reflector(BluetoothAdapterReflector.class, realAdapter).setBluetoothLeAdvertiser(advertiser);
    }
  }

  @Implementation
  protected synchronized BluetoothDevice getRemoteDevice(String address) {
    if (!deviceCache.containsKey(address)) {
      deviceCache.put(
          address,
          reflector(BluetoothAdapterReflector.class, realAdapter).getRemoteDevice(address));
    }
    return deviceCache.get(address);
  }

  public void setMostRecentlyConnectedDevices(List<BluetoothDevice> devices) {
    mostRecentlyConnectedDevices = devices;
  }

  @Implementation(minSdk = TIRAMISU)
  protected List<BluetoothDevice> getMostRecentlyConnectedDevices() {
    return mostRecentlyConnectedDevices;
  }

  @Implementation
  @Nullable
  protected Set<BluetoothDevice> getBondedDevices() {
    // real android will return null in error conditions
    if (bondedDevices == null) {
      return null;
    }
    return Collections.unmodifiableSet(bondedDevices);
  }

  public void setBondedDevices(@Nullable Set<BluetoothDevice> bluetoothDevices) {
    bondedDevices = bluetoothDevices;
  }

  @Implementation
  protected BluetoothServerSocket listenUsingInsecureRfcommWithServiceRecord(
      String serviceName, UUID uuid) {
    return ShadowBluetoothServerSocket.newInstance(
        BluetoothSocket.TYPE_RFCOMM, /* auth= */ false, /* encrypt= */ false, new ParcelUuid(uuid));
  }

  @Implementation
  protected BluetoothServerSocket listenUsingRfcommWithServiceRecord(String serviceName, UUID uuid)
      throws IOException {
    return ShadowBluetoothServerSocket.newInstance(
        BluetoothSocket.TYPE_RFCOMM, /* auth= */ false, /* encrypt= */ true, new ParcelUuid(uuid));
  }

  @Implementation(minSdk = Q)
  protected BluetoothServerSocket listenUsingInsecureL2capChannel() throws IOException {
    return ShadowBluetoothServerSocket.newInstance(
        BluetoothSocket.TYPE_L2CAP, /* auth= */ false, /* encrypt= */ true, /* uuid= */ null);
  }

  @Implementation(minSdk = Q)
  protected BluetoothServerSocket listenUsingL2capChannel() throws IOException {
    return ShadowBluetoothServerSocket.newInstance(
        BluetoothSocket.TYPE_L2CAP, /* auth= */ false, /* encrypt= */ true, /* uuid= */ null);
  }

  @Implementation
  protected boolean startDiscovery() {
    isDiscovering = true;
    return true;
  }

  @Implementation
  protected boolean cancelDiscovery() {
    isDiscovering = false;
    return true;
  }

  /** When true, overrides the value of {@link #getLeState}. By default, this is false. */
  @Implementation(minSdk = M)
  protected boolean isBleScanAlwaysAvailable() {
    return isBleScanAlwaysAvailable;
  }

  /**
   * Decides the correct LE state. When off, BLE calls will fail or return null.
   *
   * <p>LE is enabled if either Bluetooth or BLE scans are enabled. LE is always off if Airplane
   * Mode is enabled.
   */
  @Implementation(minSdk = M)
  public int getLeState() {
    if (isAirplaneMode()) {
      return BluetoothAdapter.STATE_OFF;
    }

    if (isEnabled()) {
      return BluetoothAdapter.STATE_ON;
    }

    if (isBleScanAlwaysAvailable()) {
      return BluetoothAdapter.STATE_BLE_ON;
    }

    return BluetoothAdapter.STATE_OFF;
  }

  /**
   * True if either Bluetooth is enabled or BLE scanning is available. Always false if Airplane Mode
   * is enabled. When false, BLE scans will fail. @Implementation(minSdk = M) protected boolean
   * isLeEnabled() { if (isAirplaneMode()) { return false; } return isEnabled() ||
   * isBleScanAlwaysAvailable(); }
   */
  private static boolean isAirplaneMode() {
    Context context = RuntimeEnvironment.getApplication();
    return Settings.Global.getInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0)
        != 0;
  }

  @Implementation
  protected boolean startLeScan(LeScanCallback callback) {
    return startLeScan(null, callback);
  }

  @Implementation
  protected boolean startLeScan(UUID[] serviceUuids, LeScanCallback callback) {
    if (Build.VERSION.SDK_INT >= M && !realAdapter.isLeEnabled()) {
      return false;
    }

    // Ignoring the serviceUuids param for now.
    leScanCallbacks.add(callback);
    return true;
  }

  @Implementation
  protected void stopLeScan(LeScanCallback callback) {
    leScanCallbacks.remove(callback);
  }

  public Set<LeScanCallback> getLeScanCallbacks() {
    return Collections.unmodifiableSet(leScanCallbacks);
  }

  public LeScanCallback getSingleLeScanCallback() {
    if (leScanCallbacks.size() != 1) {
      throw new IllegalStateException("There are " + leScanCallbacks.size() + " callbacks");
    }
    return leScanCallbacks.iterator().next();
  }

  @Implementation
  protected boolean isDiscovering() {
    return isDiscovering;
  }

  @Implementation
  protected boolean isEnabled() {
    return state == BluetoothAdapter.STATE_ON;
  }

  @Implementation
  protected boolean enable() {
    setState(BluetoothAdapter.STATE_ON);
    return true;
  }

  @Implementation
  protected boolean disable() {
    setState(BluetoothAdapter.STATE_OFF);
    return true;
  }

  @Implementation
  protected boolean disable(boolean persist) {
    return disable();
  }

  @Implementation
  protected String getAddress() {
    return this.address;
  }

  @Implementation
  protected int getState() {
    return state;
  }

  @Implementation
  protected String getName() {
    return name;
  }

  @Implementation
  protected boolean setName(String name) {
    this.name = name;
    return true;
  }

  /**
   * Needs looseSignatures because in Android T the return value of this method was changed from
   * bool to int.
   */
  @Implementation
  protected Object setScanMode(int scanMode) {
    boolean result = true;
    if (scanMode != BluetoothAdapter.SCAN_MODE_CONNECTABLE
        && scanMode != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE
        && scanMode != BluetoothAdapter.SCAN_MODE_NONE) {
      result = false;
    }

    this.scanMode = scanMode;
    if (RuntimeEnvironment.getApiLevel() >= VERSION_CODES.TIRAMISU) {
      return result ? BluetoothStatusCodes.SUCCESS : BluetoothStatusCodes.ERROR_UNKNOWN;
    } else {
      return result;
    }
  }

  @Implementation(maxSdk = Q)
  protected boolean setScanMode(int scanMode, int discoverableTimeout) {
    setDiscoverableTimeout(discoverableTimeout);
    return (boolean) setScanMode(scanMode);
  }

  @Implementation(minSdk = R, maxSdk = S_V2)
  protected boolean setScanMode(int scanMode, long durationMillis) {
    int durationSeconds = Math.toIntExact(durationMillis / 1000);
    setDiscoverableTimeout(durationSeconds);
    return (boolean) setScanMode(scanMode);
  }

  @Implementation
  protected int getScanMode() {
    return scanMode;
  }

  /**
   * Needs looseSignatures because the return value changed from {@code int} to {@link Duration}
   * starting in T.
   */
  @Implementation
  protected Object getDiscoverableTimeout() {
    if (RuntimeEnvironment.getApiLevel() <= S_V2) {
      return (int) discoverableTimeout.toSeconds();
    } else {
      return discoverableTimeout;
    }
  }

  @Implementation(maxSdk = S_V2)
  protected void setDiscoverableTimeout(int timeout) {
    discoverableTimeout = Duration.ofSeconds(timeout);
  }

  @Implementation(minSdk = 33)
  protected int setDiscoverableTimeout(Duration timeout) {
    if (getState() != STATE_ON) {
      return BluetoothStatusCodes.ERROR_BLUETOOTH_NOT_ENABLED;
    }
    if (timeout.toSeconds() > Integer.MAX_VALUE) {
      throw new IllegalArgumentException(
          "Timeout in seconds must be less or equal to " + Integer.MAX_VALUE);
    }
    this.discoverableTimeout = timeout;
    return BluetoothStatusCodes.SUCCESS;
  }

  @Implementation
  protected boolean isMultipleAdvertisementSupported() {
    return isMultipleAdvertisementSupported;
  }

  /**
   * Validate a Bluetooth address, such as "00:43:A8:23:10:F0" Alphabetic characters must be
   * uppercase to be valid.
   *
   * @param address Bluetooth address as string
   * @return true if the address is valid, false otherwise
   */
  @Implementation
  protected static boolean checkBluetoothAddress(String address) {
    if (address == null || address.length() != ADDRESS_LENGTH) {
      return false;
    }
    for (int i = 0; i < ADDRESS_LENGTH; i++) {
      char c = address.charAt(i);
      switch (i % 3) {
        case 0:
        case 1:
          if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')) {
            // hex character, OK
            break;
          }
          return false;
        case 2:
          if (c == ':') {
            break; // OK
          }
          return false;
      }
    }
    return true;
  }

  /**
   * Returns the connection state for the given Bluetooth {@code profile}, defaulting to {@link
   * BluetoothProfile.STATE_DISCONNECTED} if the profile's connection state was never set.
   *
   * <p>Set a Bluetooth profile's connection state via {@link #setProfileConnectionState(int, int)}.
   */
  @Implementation
  protected int getProfileConnectionState(int profile) {
    Integer state = profileConnectionStateData.get(profile);
    if (state == null) {
      return BluetoothProfile.STATE_DISCONNECTED;
    }
    return state;
  }

  public void setAddress(String address) {
    this.address = address;
  }

  public void setState(int state) {
    this.state = state;
  }

  /**
   * @deprecated Use {@link BluetoothAdapter#enable()} or {@link BluetoothAdapter#disable()}.
   */
  @Deprecated
  public void setEnabled(boolean enabled) {
    if (enabled) {
      enable();
    } else {
      disable();
    }
  }

  /**
   * Sets the value for {@link isBleScanAlwaysAvailable}. If true, {@link getLeState} will always
   * return true.
   */
  public void setBleScanAlwaysAvailable(boolean alwaysAvailable) {
    isBleScanAlwaysAvailable = alwaysAvailable;
  }

  /** Sets the value for {@link isMultipleAdvertisementSupported}. */
  public void setIsMultipleAdvertisementSupported(boolean supported) {
    isMultipleAdvertisementSupported = supported;
  }

  /** Sets the connection state {@code state} for the given BluetoothProfile {@code profile} */
  public void setProfileConnectionState(int profile, int state) {
    profileConnectionStateData.put(profile, state);
  }

  /**
   * Sets the active BluetoothProfile {@code proxy} for the given {@code profile}. Will always
   * affect behavior of {@link BluetoothAdapter#getProfileProxy} and {@link
   * BluetoothAdapter#closeProfileProxy}. Call to {@link BluetoothAdapter#closeProfileProxy} can
   * remove the set active proxy.
   *
   * @param proxy can be 'null' to simulate the situation where {@link
   *     BluetoothAdapter#getProfileProxy} would return 'false'. This can happen on older Android
   *     versions for Bluetooth profiles introduced in later Android versions.
   */
  public void setProfileProxy(int profile, @Nullable BluetoothProfile proxy) {
    isOverridingProxyBehavior = true;
    if (proxy != null) {
      profileProxies.put(profile, proxy);
    }
  }

  /**
   * @return 'true' if active (non-null) proxy has been set by {@link
   *     ShadowBluetoothAdapter#setProfileProxy} for the given {@code profile} AND it has not been
   *     "deactivated" by a call to {@link BluetoothAdapter#closeProfileProxy}. Only meaningful if
   *     {@link ShadowBluetoothAdapter#setProfileProxy} has been previously called.
   */
  public boolean hasActiveProfileProxy(int profile) {
    return profileProxies.get(profile) != null;
  }

  /**
   * Overrides behavior of {@link getProfileProxy} if {@link ShadowBluetoothAdapter#setProfileProxy}
   * has been previously called.
   *
   * <p>If active (non-null) proxy has been set by {@link setProfileProxy} for the given {@code
   * profile}, {@link getProfileProxy} will immediately call {@code onServiceConnected} of the given
   * BluetoothProfile.ServiceListener {@code listener}.
   *
   * @return 'true' if a proxy object has been set by {@link setProfileProxy} for the given
   *     BluetoothProfile {@code profile}
   */
  @Implementation
  protected boolean getProfileProxy(
      Context context, BluetoothProfile.ServiceListener listener, int profile) {
    if (!isOverridingProxyBehavior) {
      return reflector(BluetoothAdapterReflector.class, realAdapter)
          .getProfileProxy(context, listener, profile);
    }

    BluetoothProfile proxy = profileProxies.get(profile);
    if (proxy == null) {
      return false;
    } else {
      listener.onServiceConnected(profile, proxy);
      List<BluetoothProfile.ServiceListener> profileListeners =
          bluetoothProfileServiceListeners.get(profile);
      if (profileListeners != null) {
        profileListeners.add(listener);
      } else {
        bluetoothProfileServiceListeners.put(profile, new ArrayList<>(ImmutableList.of(listener)));
      }
      return true;
    }
  }

  /**
   * Overrides behavior of {@link closeProfileProxy} if {@link
   * ShadowBluetoothAdapter#setProfileProxy} has been previously called.
   *
   * <p>If the given non-null BluetoothProfile {@code proxy} was previously set for the given {@code
   * profile} by {@link ShadowBluetoothAdapter#setProfileProxy}, this proxy will be "deactivated".
   */
  @Implementation
  protected void closeProfileProxy(int profile, BluetoothProfile proxy) {
    if (!isOverridingProxyBehavior) {
      reflector(BluetoothAdapterReflector.class, realAdapter).closeProfileProxy(profile, proxy);
      return;
    }

    if (proxy != null && proxy.equals(profileProxies.get(profile))) {
      profileProxies.remove(profile);
      List<BluetoothProfile.ServiceListener> profileListeners =
          bluetoothProfileServiceListeners.remove(profile);
      if (profileListeners != null) {
        for (BluetoothProfile.ServiceListener listener : profileListeners) {
          listener.onServiceDisconnected(profile);
        }
      }
    }
  }

  /** Returns the last value of {@link #setIsLeExtendedAdvertisingSupported}, defaulting to true. */
  @Implementation(minSdk = O)
  protected boolean isLeExtendedAdvertisingSupported() {
    return isLeExtendedAdvertisingSupported;
  }

  /**
   * Sets the isLeExtendedAdvertisingSupported to enable/disable LE extended advertisements feature
   */
  public void setIsLeExtendedAdvertisingSupported(boolean supported) {
    isLeExtendedAdvertisingSupported = supported;
  }

  /** Returns the last value of {@link #setIsLeCodedPhySupported}, defaulting to true. */
  @Implementation(minSdk = UPSIDE_DOWN_CAKE)
  protected boolean isLeCodedPhySupported() {
    return isLeCodedPhySupported;
  }

  /** Sets the {@link #isLeCodedPhySupported} to enable/disable LE coded phy supported featured. */
  public void setIsLeCodedPhySupported(boolean supported) {
    isLeCodedPhySupported = supported;
  }

  /** Returns the last value of {@link #setIsLe2MPhySupported}, defaulting to true. */
  @Implementation(minSdk = UPSIDE_DOWN_CAKE)
  protected boolean isLe2MPhySupported() {
    return isLe2MPhySupported;
  }

  /** Sets the {@link #isLe2MPhySupported} to enable/disable LE 2M phy supported featured. */
  public void setIsLe2MPhySupported(boolean supported) {
    isLe2MPhySupported = supported;
  }

  @Implementation(minSdk = O)
  protected int getLeMaximumAdvertisingDataLength() {
    return isLeExtendedAdvertisingSupported
        ? LE_MAXIMUM_ADVERTISING_DATA_LENGTH_EXTENDED
        : LE_MAXIMUM_ADVERTISING_DATA_LENGTH;
  }

  @Implementation(minSdk = TIRAMISU)
  protected int startRfcommServer(String name, UUID uuid, PendingIntent pendingIntent) {
    // PendingIntent#isImmutable throws an NPE if the component does not exist, so verify directly
    // against the flags for now.
    if ((shadowOf(pendingIntent).getFlags() & PendingIntent.FLAG_IMMUTABLE) == 0) {
      throw new IllegalArgumentException("RFCOMM servers PendingIntent must be marked immutable");
    }

    boolean[] isNewServerSocket = {false};
    backgroundRfcommServers.computeIfAbsent(
        uuid,
        unused -> {
          isNewServerSocket[0] = true;
          return new BackgroundRfcommServerEntry(uuid, pendingIntent);
        });
    return isNewServerSocket[0]
        ? BluetoothStatusCodes.SUCCESS
        : RFCOMM_LISTENER_START_FAILED_UUID_IN_USE;
  }

  @Implementation(minSdk = TIRAMISU)
  protected int stopRfcommServer(UUID uuid) {
    BackgroundRfcommServerEntry entry = backgroundRfcommServers.remove(uuid);

    if (entry == null) {
      return RFCOMM_LISTENER_OPERATION_FAILED_NO_MATCHING_SERVICE_RECORD;
    }

    try {
      entry.serverSocket.close();
      return BluetoothStatusCodes.SUCCESS;
    } catch (IOException e) {
      return RFCOMM_LISTENER_FAILED_TO_CLOSE_SERVER_SOCKET;
    }
  }

  @Implementation(minSdk = TIRAMISU)
  @Nullable
  protected BluetoothSocket retrieveConnectedRfcommSocket(UUID uuid) {
    BackgroundRfcommServerEntry serverEntry = backgroundRfcommServers.get(uuid);

    try {
      return serverEntry == null ? null : serverEntry.serverSocket.accept(/* timeout= */ 0);
    } catch (IOException e) {
      // This means there is no pending socket, so the contract indicates we should return null.
      return null;
    }
  }

  /**
   * Creates an incoming socket connection from the given {@link BluetoothDevice} to a background
   * Bluetooth server created with {@link BluetoothAdapter#startRfcommServer(String, UUID,
   * PendingIntent)} on the given uuid.
   *
   * <p>Creating this socket connection will invoke the {@link PendingIntent} provided in {@link
   * BluetoothAdapter#startRfcommServer(String, UUID, PendingIntent)} when the server socket was
   * created for the given UUID. The component provided in the intent can then call {@link
   * BluetoothAdapter#retrieveConnectedRfcommSocket(UUID)} to obtain the server side socket.
   *
   * <p>A {@link ShadowBluetoothSocket} obtained from the returned {@link BluetoothSocket} can be
   * used to send data to and receive data from the server side socket. This returned {@link
   * BluetoothSocket} is the same socket as returned by {@link
   * BluetoothAdapter#retrieveConnectedRfcommSocket(UUID)} and should generally not be used directly
   * outside of obtaining the shadow, as this socket is normally not exposed outside of the
   * component started by the pending intent. {@link ShadowBluetoothSocket#getInputStreamFeeder()}
   * and {@link ShadowBluetoothSocket#getOutputStreamSink()} can be used to send data to and from
   * the socket as if it was a remote connection.
   *
   * <p><b>Warning:</b> The socket returned by this method and the corresponding server side socket
   * retrieved from {@link BluetoothAdapter#retrieveConnectedRfcommSocket(UUID)} do not support
   * reads and writes from different threads. Once reading or writing is started for a given socket
   * on a given thread, that type of operation on that socket must only be done on that thread.
   *
   * @return a server side BluetoothSocket or {@code null} if the {@link UUID} is not registered.
   *     This value should generally not be used directly, and is mainly used to obtain a shadow
   *     with which a RFCOMM client can be simulated.
   * @throws IllegalArgumentException if a server is not started for the given {@link UUID}.
   * @throws CanceledException if the pending intent for the server socket was cancelled.
   */
  public BluetoothSocket addIncomingRfcommConnection(BluetoothDevice remoteDevice, UUID uuid)
      throws CanceledException {
    BackgroundRfcommServerEntry entry = backgroundRfcommServers.get(uuid);
    if (entry == null) {
      throw new IllegalArgumentException("No RFCOMM server open for UUID: " + uuid);
    }

    BluetoothSocket socket = shadowOf(entry.serverSocket).deviceConnected(remoteDevice);
    entry.pendingIntent.send();

    return socket;
  }

  /**
   * Returns an immutable set of {@link UUID}s representing the currently registered RFCOMM servers.
   */
  @SuppressWarnings("JdkImmutableCollections")
  public Set<UUID> getRegisteredRfcommServerUuids() {
    return Set.of(backgroundRfcommServers.keySet().toArray(new UUID[0]));
  }

  private static final class BackgroundRfcommServerEntry {
    final BluetoothServerSocket serverSocket;
    final PendingIntent pendingIntent;

    BackgroundRfcommServerEntry(UUID uuid, PendingIntent pendingIntent) {
      this.serverSocket =
          ShadowBluetoothServerSocket.newInstance(
              /* type= */ BluetoothSocket.TYPE_RFCOMM,
              /* auth= */ true,
              /* encrypt= */ true,
              new ParcelUuid(uuid));
      this.pendingIntent = pendingIntent;
    }
  }

  @ForType(BluetoothAdapter.class)
  interface BluetoothAdapterReflector {

    @Static
    @Direct
    BluetoothAdapter getDefaultAdapter();

    @Direct
    boolean getProfileProxy(
        Context context, BluetoothProfile.ServiceListener listener, int profile);

    @Direct
    void closeProfileProxy(int profile, BluetoothProfile proxy);

    @Direct
    BluetoothDevice getRemoteDevice(String address);

    @Accessor("sAdapter")
    @Static
    void setAdapter(BluetoothAdapter adapter);

    @Accessor("mBluetoothLeAdvertiser")
    @Deprecated
    void setBluetoothLeAdvertiser(BluetoothLeAdvertiser advertiser);

    @Accessor("sBluetoothLeAdvertiser")
    @Static
    void setSBluetoothLeAdvertiser(BluetoothLeAdvertiser advertiser);

    @Accessor("sBluetoothLeScanner")
    @Static
    void setSBluetoothLeScanner(BluetoothLeScanner scanner);
  }

  // Any BluetoothAdapter calls which need to invoke BluetoothManager methods can delegate those
  // calls to this class. The default behavior for any methods not defined in this class is a no-op.
  @SuppressWarnings("unused")
  private static class BluetoothManagerDelegate {
    /**
     * Allows the internal BluetoothProfileConnector associated with a {@link BluetoothProfile} to
     * automatically invoke the service connected callback.
     */
    public boolean bindBluetoothProfileService(
        int bluetoothProfile, String serviceName, IBluetoothProfileServiceConnection proxy) {
      if (!BluetoothAdapter.getDefaultAdapter().isEnabled()) {
        return false;
      }
      try {
        proxy.onServiceConnected(null, null);
      } catch (RemoteException e) {
        return false;
      }
      return true;
    }

    /**
     * Allows the internal BluetoothProfileConnector associated with a {@link BluetoothProfile} to
     * automatically invoke the service disconnected callback.
     */
    public void unbindBluetoothProfileService(
        int bluetoothProfile, IBluetoothProfileServiceConnection proxy) {
      try {
        proxy.onServiceDisconnected(null);
      } catch (RemoteException e) {
        // nothing to do
      }
    }
  }
}