aboutsummaryrefslogtreecommitdiff
path: root/shadows/framework/src/main/java/org/robolectric/shadows/ShadowBluetoothServerSocket.java
blob: 70166ddad241e43c99cc46e50f775f5225d26ac2 (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
package org.robolectric.shadows;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.os.ParcelUuid;
import java.io.IOException;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.util.ReflectionHelpers;

@Implements(value = BluetoothServerSocket.class)
public class ShadowBluetoothServerSocket {
  private final BlockingQueue<BluetoothSocket> sockets = new LinkedBlockingQueue<>();
  private boolean closed;

  @SuppressLint("PrivateApi")
  public static BluetoothServerSocket newInstance(
      int type, boolean auth, boolean encrypt, ParcelUuid uuid) {
    return Shadow.newInstance(
        BluetoothServerSocket.class,
        new Class<?>[] {Integer.TYPE, Boolean.TYPE, Boolean.TYPE, ParcelUuid.class},
        new Object[] {type, auth, encrypt, uuid});
  }

  // Port ranges are valid from 1 to MAX_RFCOMM_CHANNEL.
  private static int getPort(ParcelUuid uuid) {
    return Math.abs(uuid.hashCode() % BluetoothSocket.MAX_RFCOMM_CHANNEL) + 1;
  }

  /**
   * May block the current thread and wait until {@link BluetoothDevice} is offered via
   * {@link #deviceConnected(BluetoothDevice)} method or timeout occurred.
   *
   * @return socket of the connected bluetooth device
   * @throws IOException if socket has been closed, thread interrupted while waiting or timeout has
   *         occurred.
   */
  @Implementation
  protected BluetoothSocket accept(int timeout) throws IOException {
    if (closed) {
      throw new IOException("Socket closed");
    }

    BluetoothSocket socket;
    try {
      socket = timeout == -1
              ? sockets.take() : sockets.poll(timeout, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
      throw new IOException(e);
    }

    if (socket == null) {
      throw new IOException("Timeout occurred");
    }
    socket.connect();
    return socket;
  }

  @Implementation
  protected void close() throws IOException {
    closed = true;
  }

  /** Creates {@link BluetoothSocket} for the given device and makes this socket available
   * immediately in the {@link #accept(int)} method. */
  public BluetoothSocket deviceConnected(BluetoothDevice device) {
    BluetoothSocket socket = Shadow.newInstanceOf(BluetoothSocket.class);
    ReflectionHelpers.setField(socket, "mDevice", device);
    sockets.offer(socket);
    return socket;
  }
}