| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 1 | /* |
| Zhihai Xu | a9cc57d | 2012-10-23 17:31:56 -0700 | [diff] [blame] | 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | package android.bluetooth; |
| 18 | |
| Nick Pelly | 07b84cb | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 19 | import android.os.ParcelUuid; |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 20 | import android.os.ParcelFileDescriptor; |
| Nick Pelly | 07b84cb | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 21 | import android.os.RemoteException; |
| 22 | import android.util.Log; |
| 23 | |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 24 | import java.io.BufferedInputStream; |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 25 | import java.io.Closeable; |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 26 | import java.io.FileDescriptor; |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 27 | import java.io.IOException; |
| 28 | import java.io.InputStream; |
| 29 | import java.io.OutputStream; |
| Andreas Gampe | 7b1e740 | 2015-12-11 18:00:38 -0800 | [diff] [blame] | 30 | import java.util.Arrays; |
| Jeff Sharkey | c27359e | 2013-06-11 14:13:09 -0700 | [diff] [blame] | 31 | import java.util.Locale; |
| zzy | 3049570 | 2012-10-11 14:52:43 -0700 | [diff] [blame] | 32 | import java.util.UUID; |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 33 | import android.net.LocalSocket; |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 34 | |
| 35 | import java.nio.Buffer; |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 36 | import java.nio.ByteOrder; |
| 37 | import java.nio.ByteBuffer; |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 38 | /** |
| Nick Pelly | 753da53 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 39 | * A connected or connecting Bluetooth socket. |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 40 | * |
| Nick Pelly | 753da53 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 41 | * <p>The interface for Bluetooth Sockets is similar to that of TCP sockets: |
| 42 | * {@link java.net.Socket} and {@link java.net.ServerSocket}. On the server |
| 43 | * side, use a {@link BluetoothServerSocket} to create a listening server |
| Scott Main | beef809 | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 44 | * socket. When a connection is accepted by the {@link BluetoothServerSocket}, |
| 45 | * it will return a new {@link BluetoothSocket} to manage the connection. |
| Jake Hamby | 04c7138 | 2010-09-21 13:39:53 -0700 | [diff] [blame] | 46 | * On the client side, use a single {@link BluetoothSocket} to both initiate |
| Scott Main | beef809 | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 47 | * an outgoing connection and to manage the connection. |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 48 | * |
| Scott Main | beef809 | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 49 | * <p>The most common type of Bluetooth socket is RFCOMM, which is the type |
| 50 | * supported by the Android APIs. RFCOMM is a connection-oriented, streaming |
| 51 | * transport over Bluetooth. It is also known as the Serial Port Profile (SPP). |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 52 | * |
| Scott Main | beef809 | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 53 | * <p>To create a {@link BluetoothSocket} for connecting to a known device, use |
| 54 | * {@link BluetoothDevice#createRfcommSocketToServiceRecord |
| 55 | * BluetoothDevice.createRfcommSocketToServiceRecord()}. |
| 56 | * Then call {@link #connect()} to attempt a connection to the remote device. |
| 57 | * This call will block until a connection is established or the connection |
| 58 | * fails. |
| Nick Pelly | 753da53 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 59 | * |
| Scott Main | beef809 | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 60 | * <p>To create a {@link BluetoothSocket} as a server (or "host"), see the |
| 61 | * {@link BluetoothServerSocket} documentation. |
| Nick Pelly | 753da53 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 62 | * |
| Scott Main | beef809 | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 63 | * <p>Once the socket is connected, whether initiated as a client or accepted |
| 64 | * as a server, open the IO streams by calling {@link #getInputStream} and |
| 65 | * {@link #getOutputStream} in order to retrieve {@link java.io.InputStream} |
| 66 | * and {@link java.io.OutputStream} objects, respectively, which are |
| 67 | * automatically connected to the socket. |
| 68 | * |
| 69 | * <p>{@link BluetoothSocket} is thread |
| Nick Pelly | 753da53 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 70 | * safe. In particular, {@link #close} will always immediately abort ongoing |
| 71 | * operations and close the socket. |
| Nick Pelly | ed6f2ce | 2009-09-08 10:12:06 -0700 | [diff] [blame] | 72 | * |
| Scott Main | beef809 | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 73 | * <p class="note"><strong>Note:</strong> |
| 74 | * Requires the {@link android.Manifest.permission#BLUETOOTH} permission. |
| 75 | * |
| Joe Fernandez | da4e2ab | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 76 | * <div class="special reference"> |
| 77 | * <h3>Developer Guides</h3> |
| 78 | * <p>For more information about using Bluetooth, read the |
| Marie Janssen | f242df2 | 2016-06-20 10:26:31 -0700 | [diff] [blame] | 79 | * <a href="{@docRoot}guide/topics/connectivity/bluetooth.html">Bluetooth</a> developer guide.</p> |
| Joe Fernandez | da4e2ab | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 80 | * </div> |
| 81 | * |
| Scott Main | beef809 | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 82 | * {@see BluetoothServerSocket} |
| 83 | * {@see java.io.InputStream} |
| 84 | * {@see java.io.OutputStream} |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 85 | */ |
| 86 | public final class BluetoothSocket implements Closeable { |
| Nick Pelly | 07b84cb | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 87 | private static final String TAG = "BluetoothSocket"; |
| Joe LaPenna | a49d75d | 2014-05-13 18:17:46 -0700 | [diff] [blame] | 88 | private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG); |
| 89 | private static final boolean VDBG = Log.isLoggable(TAG, Log.VERBOSE); |
| Nick Pelly | 07b84cb | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 90 | |
| Nick Pelly | ee1402d | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 91 | /** @hide */ |
| 92 | public static final int MAX_RFCOMM_CHANNEL = 30; |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 93 | /*package*/ static final int MAX_L2CAP_PACKAGE_SIZE = 0xFFFF; |
| Nick Pelly | ee1402d | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 94 | |
| Andre Eisenbach | 660bff7 | 2015-05-04 13:48:50 -0700 | [diff] [blame] | 95 | /** RFCOMM socket */ |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 96 | public static final int TYPE_RFCOMM = 1; |
| Andre Eisenbach | 660bff7 | 2015-05-04 13:48:50 -0700 | [diff] [blame] | 97 | |
| 98 | /** SCO socket */ |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 99 | public static final int TYPE_SCO = 2; |
| Andre Eisenbach | 660bff7 | 2015-05-04 13:48:50 -0700 | [diff] [blame] | 100 | |
| 101 | /** L2CAP socket */ |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 102 | public static final int TYPE_L2CAP = 3; |
| Nick Pelly | cb32d7c | 2009-06-02 15:57:18 -0700 | [diff] [blame] | 103 | |
| Nick Pelly | ee1402d | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 104 | /*package*/ static final int EBADFD = 77; |
| 105 | /*package*/ static final int EADDRINUSE = 98; |
| 106 | |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 107 | /*package*/ static final int SEC_FLAG_ENCRYPT = 1; |
| 108 | /*package*/ static final int SEC_FLAG_AUTH = 1 << 1; |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 109 | /*package*/ static final int BTSOCK_FLAG_NO_SDP = 1 << 2; |
| Casper Bonde | 60d77c2 | 2015-04-21 13:12:05 +0200 | [diff] [blame] | 110 | /*package*/ static final int SEC_FLAG_AUTH_MITM = 1 << 3; |
| Casper Bonde | 9127601 | 2015-05-08 14:32:24 +0200 | [diff] [blame] | 111 | /*package*/ static final int SEC_FLAG_AUTH_16_DIGIT = 1 << 4; |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 112 | |
| Nick Pelly | cb32d7c | 2009-06-02 15:57:18 -0700 | [diff] [blame] | 113 | private final int mType; /* one of TYPE_RFCOMM etc */ |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 114 | private BluetoothDevice mDevice; /* remote device */ |
| 115 | private String mAddress; /* remote address */ |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 116 | private final boolean mAuth; |
| 117 | private final boolean mEncrypt; |
| 118 | private final BluetoothInputStream mInputStream; |
| 119 | private final BluetoothOutputStream mOutputStream; |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 120 | private final ParcelUuid mUuid; |
| Casper Bonde | 60d77c2 | 2015-04-21 13:12:05 +0200 | [diff] [blame] | 121 | private boolean mExcludeSdp = false; /* when true no SPP SDP record will be created */ |
| 122 | private boolean mAuthMitm = false; /* when true Man-in-the-middle protection will be enabled*/ |
| Casper Bonde | 9127601 | 2015-05-08 14:32:24 +0200 | [diff] [blame] | 123 | private boolean mMin16DigitPin = false; /* Minimum 16 digit pin for sec mode 2 connections */ |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 124 | private ParcelFileDescriptor mPfd; |
| 125 | private LocalSocket mSocket; |
| 126 | private InputStream mSocketIS; |
| 127 | private OutputStream mSocketOS; |
| Mike Lockwood | 9cffff7 | 2013-10-02 07:56:46 -0700 | [diff] [blame] | 128 | private int mPort; /* RFCOMM channel or L2CAP psm */ |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 129 | private int mFd; |
| 130 | private String mServiceName; |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 131 | private static int PROXY_CONNECTION_TIMEOUT = 5000; |
| 132 | |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 133 | private static int SOCK_SIGNAL_SIZE = 20; |
| 134 | |
| 135 | private ByteBuffer mL2capBuffer = null; |
| 136 | private int mMaxTxPacketSize = 0; // The l2cap maximum packet size supported by the peer. |
| 137 | private int mMaxRxPacketSize = 0; // The l2cap maximum packet size that can be received. |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 138 | |
| Matthew Xie | 638350f | 2011-05-03 19:50:20 -0700 | [diff] [blame] | 139 | private enum SocketState { |
| 140 | INIT, |
| 141 | CONNECTED, |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 142 | LISTENING, |
| 143 | CLOSED, |
| Matthew Xie | 638350f | 2011-05-03 19:50:20 -0700 | [diff] [blame] | 144 | } |
| Nick Pelly | 4cd2cd9 | 2009-09-02 11:51:35 -0700 | [diff] [blame] | 145 | |
| Matthew Xie | 638350f | 2011-05-03 19:50:20 -0700 | [diff] [blame] | 146 | /** prevents all native calls after destroyNative() */ |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 147 | private volatile SocketState mSocketState; |
| Matthew Xie | 638350f | 2011-05-03 19:50:20 -0700 | [diff] [blame] | 148 | |
| 149 | /** protects mSocketState */ |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 150 | //private final ReentrantReadWriteLock mLock; |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 151 | |
| 152 | /** |
| Nick Pelly | 2d66488 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 153 | * Construct a BluetoothSocket. |
| Nick Pelly | cb32d7c | 2009-06-02 15:57:18 -0700 | [diff] [blame] | 154 | * @param type type of socket |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 155 | * @param fd fd to use for connected socket, or -1 for a new socket |
| 156 | * @param auth require the remote device to be authenticated |
| 157 | * @param encrypt require the connection to be encrypted |
| Nick Pelly | 2d66488 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 158 | * @param device remote device that this socket can connect to |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 159 | * @param port remote port |
| Nick Pelly | 07b84cb | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 160 | * @param uuid SDP uuid |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 161 | * @throws IOException On error, for example Bluetooth not available, or |
| Jake Hamby | 04c7138 | 2010-09-21 13:39:53 -0700 | [diff] [blame] | 162 | * insufficient privileges |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 163 | */ |
| Nick Pelly | 2d66488 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 164 | /*package*/ BluetoothSocket(int type, int fd, boolean auth, boolean encrypt, |
| Nick Pelly | 07b84cb | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 165 | BluetoothDevice device, int port, ParcelUuid uuid) throws IOException { |
| Casper Bonde | 9127601 | 2015-05-08 14:32:24 +0200 | [diff] [blame] | 166 | this(type, fd, auth, encrypt, device, port, uuid, false, false); |
| Casper Bonde | 60d77c2 | 2015-04-21 13:12:05 +0200 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Construct a BluetoothSocket. |
| 171 | * @param type type of socket |
| 172 | * @param fd fd to use for connected socket, or -1 for a new socket |
| 173 | * @param auth require the remote device to be authenticated |
| 174 | * @param encrypt require the connection to be encrypted |
| 175 | * @param device remote device that this socket can connect to |
| 176 | * @param port remote port |
| 177 | * @param uuid SDP uuid |
| 178 | * @param mitm enforce man-in-the-middle protection. |
| Casper Bonde | 9127601 | 2015-05-08 14:32:24 +0200 | [diff] [blame] | 179 | * @param min16DigitPin enforce a minimum length of 16 digits for a sec mode 2 connection |
| Casper Bonde | 60d77c2 | 2015-04-21 13:12:05 +0200 | [diff] [blame] | 180 | * @throws IOException On error, for example Bluetooth not available, or |
| 181 | * insufficient privileges |
| 182 | */ |
| 183 | /*package*/ BluetoothSocket(int type, int fd, boolean auth, boolean encrypt, |
| Casper Bonde | 9127601 | 2015-05-08 14:32:24 +0200 | [diff] [blame] | 184 | BluetoothDevice device, int port, ParcelUuid uuid, boolean mitm, boolean min16DigitPin) |
| 185 | throws IOException { |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 186 | if (VDBG) Log.d(TAG, "Creating new BluetoothSocket of type: " + type); |
| 187 | if (type == BluetoothSocket.TYPE_RFCOMM && uuid == null && fd == -1 |
| 188 | && port != BluetoothAdapter.SOCKET_CHANNEL_AUTO_STATIC_NO_SDP) { |
| Nick Pelly | ee1402d | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 189 | if (port < 1 || port > MAX_RFCOMM_CHANNEL) { |
| 190 | throw new IOException("Invalid RFCOMM channel: " + port); |
| 191 | } |
| 192 | } |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 193 | if (uuid != null) |
| zzy | 3049570 | 2012-10-11 14:52:43 -0700 | [diff] [blame] | 194 | mUuid = uuid; |
| 195 | else mUuid = new ParcelUuid(new UUID(0, 0)); |
| Nick Pelly | cb32d7c | 2009-06-02 15:57:18 -0700 | [diff] [blame] | 196 | mType = type; |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 197 | mAuth = auth; |
| Casper Bonde | 60d77c2 | 2015-04-21 13:12:05 +0200 | [diff] [blame] | 198 | mAuthMitm = mitm; |
| Casper Bonde | 9127601 | 2015-05-08 14:32:24 +0200 | [diff] [blame] | 199 | mMin16DigitPin = min16DigitPin; |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 200 | mEncrypt = encrypt; |
| Nick Pelly | 2d66488 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 201 | mDevice = device; |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 202 | mPort = port; |
| 203 | mFd = fd; |
| 204 | |
| 205 | mSocketState = SocketState.INIT; |
| 206 | |
| Nick Pelly | 2d66488 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 207 | if (device == null) { |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 208 | // Server socket |
| 209 | mAddress = BluetoothAdapter.getDefaultAdapter().getAddress(); |
| Nick Pelly | 2d66488 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 210 | } else { |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 211 | // Remote socket |
| Nick Pelly | 2d66488 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 212 | mAddress = device.getAddress(); |
| 213 | } |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 214 | mInputStream = new BluetoothInputStream(this); |
| 215 | mOutputStream = new BluetoothOutputStream(this); |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 216 | } |
| 217 | private BluetoothSocket(BluetoothSocket s) { |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 218 | if (VDBG) Log.d(TAG, "Creating new Private BluetoothSocket of type: " + s.mType); |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 219 | mUuid = s.mUuid; |
| 220 | mType = s.mType; |
| 221 | mAuth = s.mAuth; |
| 222 | mEncrypt = s.mEncrypt; |
| 223 | mPort = s.mPort; |
| 224 | mInputStream = new BluetoothInputStream(this); |
| 225 | mOutputStream = new BluetoothOutputStream(this); |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 226 | mMaxRxPacketSize = s.mMaxRxPacketSize; |
| 227 | mMaxTxPacketSize = s.mMaxTxPacketSize; |
| 228 | |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 229 | mServiceName = s.mServiceName; |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 230 | mExcludeSdp = s.mExcludeSdp; |
| Casper Bonde | 60d77c2 | 2015-04-21 13:12:05 +0200 | [diff] [blame] | 231 | mAuthMitm = s.mAuthMitm; |
| Casper Bonde | 9127601 | 2015-05-08 14:32:24 +0200 | [diff] [blame] | 232 | mMin16DigitPin = s.mMin16DigitPin; |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 233 | } |
| 234 | private BluetoothSocket acceptSocket(String RemoteAddr) throws IOException { |
| 235 | BluetoothSocket as = new BluetoothSocket(this); |
| 236 | as.mSocketState = SocketState.CONNECTED; |
| 237 | FileDescriptor[] fds = mSocket.getAncillaryFileDescriptors(); |
| Andreas Gampe | 7b1e740 | 2015-12-11 18:00:38 -0800 | [diff] [blame] | 238 | if (DBG) Log.d(TAG, "socket fd passed by stack fds: " + Arrays.toString(fds)); |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 239 | if(fds == null || fds.length != 1) { |
| Andreas Gampe | 7b1e740 | 2015-12-11 18:00:38 -0800 | [diff] [blame] | 240 | Log.e(TAG, "socket fd passed from stack failed, fds: " + Arrays.toString(fds)); |
| zzy | f6fcd9b | 2013-04-16 17:17:37 -0700 | [diff] [blame] | 241 | as.close(); |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 242 | throw new IOException("bt socket acept failed"); |
| 243 | } |
| Zach Johnson | c624245 | 2015-07-13 18:00:35 -0700 | [diff] [blame] | 244 | |
| 245 | as.mPfd = new ParcelFileDescriptor(fds[0]); |
| Neil Fuller | 3dfe08b | 2017-01-06 11:29:15 +0000 | [diff] [blame] | 246 | as.mSocket = LocalSocket.createConnectedLocalSocket(fds[0]); |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 247 | as.mSocketIS = as.mSocket.getInputStream(); |
| 248 | as.mSocketOS = as.mSocket.getOutputStream(); |
| 249 | as.mAddress = RemoteAddr; |
| 250 | as.mDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(RemoteAddr); |
| 251 | return as; |
| 252 | } |
| Nick Pelly | 2d66488 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 253 | /** |
| Nick Pelly | 07b84cb | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 254 | * Construct a BluetoothSocket from address. Used by native code. |
| Nick Pelly | 2d66488 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 255 | * @param type type of socket |
| 256 | * @param fd fd to use for connected socket, or -1 for a new socket |
| 257 | * @param auth require the remote device to be authenticated |
| 258 | * @param encrypt require the connection to be encrypted |
| 259 | * @param address remote device that this socket can connect to |
| 260 | * @param port remote port |
| 261 | * @throws IOException On error, for example Bluetooth not available, or |
| Jake Hamby | 04c7138 | 2010-09-21 13:39:53 -0700 | [diff] [blame] | 262 | * insufficient privileges |
| Nick Pelly | 2d66488 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 263 | */ |
| 264 | private BluetoothSocket(int type, int fd, boolean auth, boolean encrypt, String address, |
| 265 | int port) throws IOException { |
| Casper Bonde | 9127601 | 2015-05-08 14:32:24 +0200 | [diff] [blame] | 266 | this(type, fd, auth, encrypt, new BluetoothDevice(address), port, null, false, false); |
| Nick Pelly | 2d66488 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| Nick Pelly | 753da53 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 269 | /** @hide */ |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 270 | @Override |
| 271 | protected void finalize() throws Throwable { |
| 272 | try { |
| 273 | close(); |
| 274 | } finally { |
| 275 | super.finalize(); |
| 276 | } |
| 277 | } |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 278 | private int getSecurityFlags() { |
| 279 | int flags = 0; |
| 280 | if(mAuth) |
| 281 | flags |= SEC_FLAG_AUTH; |
| 282 | if(mEncrypt) |
| 283 | flags |= SEC_FLAG_ENCRYPT; |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 284 | if(mExcludeSdp) |
| 285 | flags |= BTSOCK_FLAG_NO_SDP; |
| Casper Bonde | 60d77c2 | 2015-04-21 13:12:05 +0200 | [diff] [blame] | 286 | if(mAuthMitm) |
| 287 | flags |= SEC_FLAG_AUTH_MITM; |
| Casper Bonde | 9127601 | 2015-05-08 14:32:24 +0200 | [diff] [blame] | 288 | if(mMin16DigitPin) |
| 289 | flags |= SEC_FLAG_AUTH_16_DIGIT; |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 290 | return flags; |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | /** |
| Nick Pelly | 753da53 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 294 | * Get the remote device this socket is connecting, or connected, to. |
| 295 | * @return remote device |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 296 | */ |
| Nick Pelly | 2d66488 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 297 | public BluetoothDevice getRemoteDevice() { |
| 298 | return mDevice; |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Get the input stream associated with this socket. |
| Nick Pelly | 753da53 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 303 | * <p>The input stream will be returned even if the socket is not yet |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 304 | * connected, but operations on that stream will throw IOException until |
| 305 | * the associated socket is connected. |
| 306 | * @return InputStream |
| 307 | */ |
| 308 | public InputStream getInputStream() throws IOException { |
| 309 | return mInputStream; |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Get the output stream associated with this socket. |
| Nick Pelly | 753da53 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 314 | * <p>The output stream will be returned even if the socket is not yet |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 315 | * connected, but operations on that stream will throw IOException until |
| 316 | * the associated socket is connected. |
| 317 | * @return OutputStream |
| 318 | */ |
| 319 | public OutputStream getOutputStream() throws IOException { |
| 320 | return mOutputStream; |
| 321 | } |
| 322 | |
| Nick Pelly | ee1402d | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 323 | /** |
| Matthew Xie | 638350f | 2011-05-03 19:50:20 -0700 | [diff] [blame] | 324 | * Get the connection status of this socket, ie, whether there is an active connection with |
| 325 | * remote device. |
| 326 | * @return true if connected |
| 327 | * false if not connected |
| 328 | */ |
| 329 | public boolean isConnected() { |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 330 | return mSocketState == SocketState.CONNECTED; |
| 331 | } |
| 332 | |
| 333 | /*package*/ void setServiceName(String name) { |
| 334 | mServiceName = name; |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Attempt to connect to a remote device. |
| 339 | * <p>This method will block until a connection is made or the connection |
| 340 | * fails. If this method returns without an exception then this socket |
| 341 | * is now connected. |
| 342 | * <p>Creating new connections to |
| 343 | * remote Bluetooth devices should not be attempted while device discovery |
| 344 | * is in progress. Device discovery is a heavyweight procedure on the |
| 345 | * Bluetooth adapter and will significantly slow a device connection. |
| 346 | * Use {@link BluetoothAdapter#cancelDiscovery()} to cancel an ongoing |
| 347 | * discovery. Discovery is not managed by the Activity, |
| 348 | * but is run as a system service, so an application should always call |
| 349 | * {@link BluetoothAdapter#cancelDiscovery()} even if it |
| 350 | * did not directly request a discovery, just to be sure. |
| 351 | * <p>{@link #close} can be used to abort this call from another thread. |
| 352 | * @throws IOException on error, for example connection failure |
| 353 | */ |
| 354 | public void connect() throws IOException { |
| 355 | if (mDevice == null) throw new IOException("Connect is called on null device"); |
| 356 | |
| 357 | try { |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 358 | if (mSocketState == SocketState.CLOSED) throw new IOException("socket closed"); |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 359 | IBluetooth bluetoothProxy = |
| 360 | BluetoothAdapter.getDefaultAdapter().getBluetoothService(null); |
| fredc | 3c71964 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 361 | if (bluetoothProxy == null) throw new IOException("Bluetooth is off"); |
| 362 | mPfd = bluetoothProxy.connectSocket(mDevice, mType, |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 363 | mUuid, mPort, getSecurityFlags()); |
| 364 | synchronized(this) |
| 365 | { |
| Matthew Xie | f8035a7 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 366 | if (DBG) Log.d(TAG, "connect(), SocketState: " + mSocketState + ", mPfd: " + mPfd); |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 367 | if (mSocketState == SocketState.CLOSED) throw new IOException("socket closed"); |
| 368 | if (mPfd == null) throw new IOException("bt socket connect failed"); |
| 369 | FileDescriptor fd = mPfd.getFileDescriptor(); |
| Neil Fuller | 3dfe08b | 2017-01-06 11:29:15 +0000 | [diff] [blame] | 370 | mSocket = LocalSocket.createConnectedLocalSocket(fd); |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 371 | mSocketIS = mSocket.getInputStream(); |
| 372 | mSocketOS = mSocket.getOutputStream(); |
| 373 | } |
| 374 | int channel = readInt(mSocketIS); |
| 375 | if (channel <= 0) |
| 376 | throw new IOException("bt socket connect failed"); |
| 377 | mPort = channel; |
| 378 | waitSocketSignal(mSocketIS); |
| 379 | synchronized(this) |
| 380 | { |
| 381 | if (mSocketState == SocketState.CLOSED) |
| 382 | throw new IOException("bt socket closed"); |
| 383 | mSocketState = SocketState.CONNECTED; |
| 384 | } |
| 385 | } catch (RemoteException e) { |
| 386 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| Sharvil Nanavati | 8b3a52f | 2014-04-08 14:51:15 -0700 | [diff] [blame] | 387 | throw new IOException("unable to send RPC: " + e.getMessage()); |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 388 | } |
| Matthew Xie | 638350f | 2011-05-03 19:50:20 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /** |
| Nick Pelly | ee1402d | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 392 | * Currently returns unix errno instead of throwing IOException, |
| 393 | * so that BluetoothAdapter can check the error code for EADDRINUSE |
| 394 | */ |
| 395 | /*package*/ int bindListen() { |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 396 | int ret; |
| 397 | if (mSocketState == SocketState.CLOSED) return EBADFD; |
| fredc | f1f5dde | 2012-04-24 03:59:57 -0700 | [diff] [blame] | 398 | IBluetooth bluetoothProxy = BluetoothAdapter.getDefaultAdapter().getBluetoothService(null); |
| fredc | 3c71964 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 399 | if (bluetoothProxy == null) { |
| 400 | Log.e(TAG, "bindListen fail, reason: bluetooth is off"); |
| 401 | return -1; |
| 402 | } |
| Nick Pelly | 4cd2cd9 | 2009-09-02 11:51:35 -0700 | [diff] [blame] | 403 | try { |
| fredc | 3c71964 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 404 | mPfd = bluetoothProxy.createSocketChannel(mType, mServiceName, |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 405 | mUuid, mPort, getSecurityFlags()); |
| 406 | } catch (RemoteException e) { |
| 407 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 408 | return -1; |
| 409 | } |
| 410 | |
| 411 | // read out port number |
| 412 | try { |
| 413 | synchronized(this) { |
| Joe LaPenna | a49d75d | 2014-05-13 18:17:46 -0700 | [diff] [blame] | 414 | if (DBG) Log.d(TAG, "bindListen(), SocketState: " + mSocketState + ", mPfd: " + |
| Matthew Xie | f8035a7 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 415 | mPfd); |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 416 | if(mSocketState != SocketState.INIT) return EBADFD; |
| 417 | if(mPfd == null) return -1; |
| 418 | FileDescriptor fd = mPfd.getFileDescriptor(); |
| Ajay Panicker | 178b028 | 2017-03-28 14:28:27 -0700 | [diff] [blame] | 419 | if (fd == null) { |
| 420 | Log.e(TAG, "bindListen(), null file descriptor"); |
| 421 | return -1; |
| 422 | } |
| 423 | |
| Neil Fuller | 3dfe08b | 2017-01-06 11:29:15 +0000 | [diff] [blame] | 424 | if (DBG) Log.d(TAG, "bindListen(), Create LocalSocket"); |
| 425 | mSocket = LocalSocket.createConnectedLocalSocket(fd); |
| 426 | if (DBG) Log.d(TAG, "bindListen(), new LocalSocket.getInputStream()"); |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 427 | mSocketIS = mSocket.getInputStream(); |
| 428 | mSocketOS = mSocket.getOutputStream(); |
| 429 | } |
| Joe LaPenna | a49d75d | 2014-05-13 18:17:46 -0700 | [diff] [blame] | 430 | if (DBG) Log.d(TAG, "bindListen(), readInt mSocketIS: " + mSocketIS); |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 431 | int channel = readInt(mSocketIS); |
| 432 | synchronized(this) { |
| 433 | if(mSocketState == SocketState.INIT) |
| 434 | mSocketState = SocketState.LISTENING; |
| 435 | } |
| Joe LaPenna | a49d75d | 2014-05-13 18:17:46 -0700 | [diff] [blame] | 436 | if (DBG) Log.d(TAG, "channel: " + channel); |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 437 | if (mPort <= -1) { |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 438 | mPort = channel; |
| 439 | } // else ASSERT(mPort == channel) |
| 440 | ret = 0; |
| 441 | } catch (IOException e) { |
| Matthew Xie | 6398bb3 | 2014-09-03 10:04:00 -0700 | [diff] [blame] | 442 | if (mPfd != null) { |
| 443 | try { |
| 444 | mPfd.close(); |
| 445 | } catch (IOException e1) { |
| 446 | Log.e(TAG, "bindListen, close mPfd: " + e1); |
| 447 | } |
| 448 | mPfd = null; |
| 449 | } |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 450 | Log.e(TAG, "bindListen, fail to get port number, exception: " + e); |
| 451 | return -1; |
| 452 | } |
| 453 | return ret; |
| Nick Pelly | 4cd2cd9 | 2009-09-02 11:51:35 -0700 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | /*package*/ BluetoothSocket accept(int timeout) throws IOException { |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 457 | BluetoothSocket acceptedSocket; |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 458 | if (mSocketState != SocketState.LISTENING) |
| 459 | throw new IOException("bt socket is not in listen state"); |
| zzy | 2e650b3 | 2012-11-22 11:52:44 -0800 | [diff] [blame] | 460 | if(timeout > 0) { |
| 461 | Log.d(TAG, "accept() set timeout (ms):" + timeout); |
| 462 | mSocket.setSoTimeout(timeout); |
| 463 | } |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 464 | String RemoteAddr = waitSocketSignal(mSocketIS); |
| zzy | 2e650b3 | 2012-11-22 11:52:44 -0800 | [diff] [blame] | 465 | if(timeout > 0) |
| 466 | mSocket.setSoTimeout(0); |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 467 | synchronized(this) |
| 468 | { |
| 469 | if (mSocketState != SocketState.LISTENING) |
| 470 | throw new IOException("bt socket is not in listen state"); |
| 471 | acceptedSocket = acceptSocket(RemoteAddr); |
| 472 | //quick drop the reference of the file handle |
| Nick Pelly | 4cd2cd9 | 2009-09-02 11:51:35 -0700 | [diff] [blame] | 473 | } |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 474 | return acceptedSocket; |
| Nick Pelly | 4cd2cd9 | 2009-09-02 11:51:35 -0700 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | /*package*/ int available() throws IOException { |
| Matthew Xie | f8035a7 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 478 | if (VDBG) Log.d(TAG, "available: " + mSocketIS); |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 479 | return mSocketIS.available(); |
| Nick Pelly | 4cd2cd9 | 2009-09-02 11:51:35 -0700 | [diff] [blame] | 480 | } |
| zzy | f6fcd9b | 2013-04-16 17:17:37 -0700 | [diff] [blame] | 481 | /** |
| 482 | * Wait until the data in sending queue is emptied. A polling version |
| 483 | * for flush implementation. Used to ensure the writing data afterwards will |
| 484 | * be packed in new RFCOMM frame. |
| 485 | * @throws IOException |
| 486 | * if an i/o error occurs. |
| 487 | */ |
| 488 | /*package*/ void flush() throws IOException { |
| Zhihai Xu | f4f4b3b | 2013-12-17 11:39:20 -0800 | [diff] [blame] | 489 | if (mSocketOS == null) throw new IOException("flush is called on null OutputStream"); |
| zzy | f6fcd9b | 2013-04-16 17:17:37 -0700 | [diff] [blame] | 490 | if (VDBG) Log.d(TAG, "flush: " + mSocketOS); |
| 491 | mSocketOS.flush(); |
| 492 | } |
| Nick Pelly | 4cd2cd9 | 2009-09-02 11:51:35 -0700 | [diff] [blame] | 493 | |
| 494 | /*package*/ int read(byte[] b, int offset, int length) throws IOException { |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 495 | int ret = 0; |
| Zhihai Xu | f4f4b3b | 2013-12-17 11:39:20 -0800 | [diff] [blame] | 496 | if (VDBG) Log.d(TAG, "read in: " + mSocketIS + " len: " + length); |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 497 | if(mType == TYPE_L2CAP) |
| 498 | { |
| 499 | int bytesToRead = length; |
| 500 | if (VDBG) Log.v(TAG, "l2cap: read(): offset: " + offset + " length:" + length |
| 501 | + "mL2capBuffer= " + mL2capBuffer); |
| 502 | if (mL2capBuffer == null) { |
| 503 | createL2capRxBuffer(); |
| 504 | } |
| 505 | if (mL2capBuffer.remaining() == 0) { |
| 506 | if (VDBG) Log.v(TAG, "l2cap buffer empty, refilling..."); |
| 507 | if (fillL2capRxBuffer() == -1) { |
| 508 | return -1; |
| 509 | } |
| 510 | } |
| 511 | if (bytesToRead > mL2capBuffer.remaining()) { |
| 512 | bytesToRead = mL2capBuffer.remaining(); |
| 513 | } |
| 514 | if(VDBG) Log.v(TAG, "get(): offset: " + offset |
| 515 | + " bytesToRead: " + bytesToRead); |
| 516 | mL2capBuffer.get(b, offset, bytesToRead); |
| 517 | ret = bytesToRead; |
| 518 | }else { |
| 519 | if (VDBG) Log.v(TAG, "default: read(): offset: " + offset + " length:" + length); |
| 520 | ret = mSocketIS.read(b, offset, length); |
| 521 | } |
| 522 | if (ret < 0) |
| Zhihai Xu | f4f4b3b | 2013-12-17 11:39:20 -0800 | [diff] [blame] | 523 | throw new IOException("bt socket closed, read return: " + ret); |
| 524 | if (VDBG) Log.d(TAG, "read out: " + mSocketIS + " ret: " + ret); |
| 525 | return ret; |
| Nick Pelly | 4cd2cd9 | 2009-09-02 11:51:35 -0700 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | /*package*/ int write(byte[] b, int offset, int length) throws IOException { |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 529 | |
| 530 | //TODO: Since bindings can exist between the SDU size and the |
| 531 | // protocol, we might need to throw an exception instead of just |
| 532 | // splitting the write into multiple smaller writes. |
| 533 | // Rfcomm uses dynamic allocation, and should not have any bindings |
| 534 | // to the actual message length. |
| 535 | if (VDBG) Log.d(TAG, "write: " + mSocketOS + " length: " + length); |
| 536 | if (mType == TYPE_L2CAP) { |
| 537 | if(length <= mMaxTxPacketSize) { |
| 538 | mSocketOS.write(b, offset, length); |
| 539 | } else { |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 540 | if(DBG) Log.w(TAG, "WARNING: Write buffer larger than L2CAP packet size!\n" |
| 541 | + "Packet will be divided into SDU packets of size " |
| 542 | + mMaxTxPacketSize); |
| Christine Hallstrom | 5822d7e | 2016-06-17 16:00:25 -0700 | [diff] [blame] | 543 | int tmpOffset = offset; |
| 544 | int bytesToWrite = length; |
| 545 | while (bytesToWrite > 0) { |
| 546 | int tmpLength = (bytesToWrite > mMaxTxPacketSize) |
| 547 | ? mMaxTxPacketSize |
| 548 | : bytesToWrite; |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 549 | mSocketOS.write(b, tmpOffset, tmpLength); |
| Christine Hallstrom | 5822d7e | 2016-06-17 16:00:25 -0700 | [diff] [blame] | 550 | tmpOffset += tmpLength; |
| 551 | bytesToWrite -= tmpLength; |
| 552 | } |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 553 | } |
| 554 | } else { |
| 555 | mSocketOS.write(b, offset, length); |
| 556 | } |
| 557 | // There is no good way to confirm since the entire process is asynchronous anyway |
| 558 | if (VDBG) Log.d(TAG, "write out: " + mSocketOS + " length: " + length); |
| 559 | return length; |
| Nick Pelly | 4cd2cd9 | 2009-09-02 11:51:35 -0700 | [diff] [blame] | 560 | } |
| 561 | |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 562 | @Override |
| 563 | public void close() throws IOException { |
| Ajay Panicker | 178b028 | 2017-03-28 14:28:27 -0700 | [diff] [blame] | 564 | Log.d(TAG, "close() this: " + this + ", channel: " + mPort + |
| 565 | ", mSocketIS: " + mSocketIS + ", mSocketOS: " + mSocketOS + |
| 566 | "mSocket: " + mSocket + ", mSocketState: " + mSocketState); |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 567 | if(mSocketState == SocketState.CLOSED) |
| 568 | return; |
| 569 | else |
| 570 | { |
| 571 | synchronized(this) |
| 572 | { |
| 573 | if(mSocketState == SocketState.CLOSED) |
| 574 | return; |
| 575 | mSocketState = SocketState.CLOSED; |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 576 | if(mSocket != null) { |
| Joe LaPenna | a49d75d | 2014-05-13 18:17:46 -0700 | [diff] [blame] | 577 | if (DBG) Log.d(TAG, "Closing mSocket: " + mSocket); |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 578 | mSocket.shutdownInput(); |
| 579 | mSocket.shutdownOutput(); |
| 580 | mSocket.close(); |
| 581 | mSocket = null; |
| 582 | } |
| Zhihai Xu | 0ad5916 | 2014-01-20 12:04:23 -0800 | [diff] [blame] | 583 | if (mPfd != null) { |
| 584 | mPfd.close(); |
| 585 | mPfd = null; |
| 586 | } |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 587 | } |
| Nick Pelly | 07b84cb | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 588 | } |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 589 | } |
| Nick Pelly | 07b84cb | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 590 | |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 591 | /*package */ void removeChannel() { |
| 592 | } |
| Nick Pelly | 07b84cb | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 593 | |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 594 | /*package */ int getPort() { |
| 595 | return mPort; |
| 596 | } |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 597 | |
| 598 | /** |
| 599 | * Get the maximum supported Transmit packet size for the underlying transport. |
| 600 | * Use this to optimize the writes done to the output socket, to avoid sending |
| 601 | * half full packets. |
| 602 | * @return the maximum supported Transmit packet size for the underlying transport. |
| 603 | */ |
| 604 | public int getMaxTransmitPacketSize(){ |
| 605 | return mMaxTxPacketSize; |
| 606 | } |
| 607 | |
| 608 | /** |
| 609 | * Get the maximum supported Receive packet size for the underlying transport. |
| 610 | * Use this to optimize the reads done on the input stream, as any call to read |
| 611 | * will return a maximum of this amount of bytes - or for some transports a |
| 612 | * multiple of this value. |
| 613 | * @return the maximum supported Receive packet size for the underlying transport. |
| 614 | */ |
| 615 | public int getMaxReceivePacketSize(){ |
| 616 | return mMaxRxPacketSize; |
| 617 | } |
| 618 | |
| 619 | /** |
| Andre Eisenbach | 660bff7 | 2015-05-04 13:48:50 -0700 | [diff] [blame] | 620 | * Get the type of the underlying connection. |
| 621 | * @return one of {@link #TYPE_RFCOMM}, {@link #TYPE_SCO} or {@link #TYPE_L2CAP} |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 622 | */ |
| 623 | public int getConnectionType() { |
| 624 | return mType; |
| 625 | } |
| 626 | |
| 627 | /** |
| 628 | * Change if a SDP entry should be automatically created. |
| 629 | * Must be called before calling .bind, for the call to have any effect. |
| 630 | * @param mExcludeSdp <li>TRUE - do not auto generate SDP record. |
| 631 | * <li>FALSE - default - auto generate SPP SDP record. |
| 632 | * @hide |
| 633 | */ |
| 634 | public void setExcludeSdp(boolean excludeSdp) { |
| 635 | this.mExcludeSdp = excludeSdp; |
| 636 | } |
| 637 | |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 638 | private String convertAddr(final byte[] addr) { |
| Jeff Sharkey | c27359e | 2013-06-11 14:13:09 -0700 | [diff] [blame] | 639 | return String.format(Locale.US, "%02X:%02X:%02X:%02X:%02X:%02X", |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 640 | addr[0] , addr[1], addr[2], addr[3] , addr[4], addr[5]); |
| 641 | } |
| 642 | private String waitSocketSignal(InputStream is) throws IOException { |
| 643 | byte [] sig = new byte[SOCK_SIGNAL_SIZE]; |
| 644 | int ret = readAll(is, sig); |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 645 | if (VDBG) Log.d(TAG, "waitSocketSignal read " + SOCK_SIGNAL_SIZE + |
| 646 | " bytes signal ret: " + ret); |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 647 | ByteBuffer bb = ByteBuffer.wrap(sig); |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 648 | /* the struct in native is decorated with __attribute__((packed)), hence this is possible */ |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 649 | bb.order(ByteOrder.nativeOrder()); |
| 650 | int size = bb.getShort(); |
| zzy | 2e650b3 | 2012-11-22 11:52:44 -0800 | [diff] [blame] | 651 | if(size != SOCK_SIGNAL_SIZE) |
| 652 | throw new IOException("Connection failure, wrong signal size: " + size); |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 653 | byte [] addr = new byte[6]; |
| 654 | bb.get(addr); |
| 655 | int channel = bb.getInt(); |
| 656 | int status = bb.getInt(); |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 657 | mMaxTxPacketSize = (bb.getShort() & 0xffff); // Convert to unsigned value |
| 658 | mMaxRxPacketSize = (bb.getShort() & 0xffff); // Convert to unsigned value |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 659 | String RemoteAddr = convertAddr(addr); |
| Matthew Xie | f8035a7 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 660 | if (VDBG) Log.d(TAG, "waitSocketSignal: sig size: " + size + ", remote addr: " |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 661 | + RemoteAddr + ", channel: " + channel + ", status: " + status |
| 662 | + " MaxRxPktSize: " + mMaxRxPacketSize + " MaxTxPktSize: " + mMaxTxPacketSize); |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 663 | if(status != 0) |
| 664 | throw new IOException("Connection failure, status: " + status); |
| 665 | return RemoteAddr; |
| 666 | } |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 667 | |
| 668 | private void createL2capRxBuffer(){ |
| 669 | if(mType == TYPE_L2CAP) { |
| 670 | // Allocate the buffer to use for reads. |
| 671 | if(VDBG) Log.v(TAG, " Creating mL2capBuffer: mMaxPacketSize: " + mMaxRxPacketSize); |
| 672 | mL2capBuffer = ByteBuffer.wrap(new byte[mMaxRxPacketSize]); |
| 673 | if(VDBG) Log.v(TAG, "mL2capBuffer.remaining()" + mL2capBuffer.remaining()); |
| 674 | mL2capBuffer.limit(0); // Ensure we do a real read at the first read-request |
| 675 | if(VDBG) Log.v(TAG, "mL2capBuffer.remaining() after limit(0):" + |
| 676 | mL2capBuffer.remaining()); |
| 677 | } |
| 678 | } |
| 679 | |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 680 | private int readAll(InputStream is, byte[] b) throws IOException { |
| 681 | int left = b.length; |
| 682 | while(left > 0) { |
| 683 | int ret = is.read(b, b.length - left, left); |
| fredc | e8d1519 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 684 | if(ret <= 0) |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 685 | throw new IOException("read failed, socket might closed or timeout, read ret: " |
| 686 | + ret); |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 687 | left -= ret; |
| 688 | if(left != 0) |
| 689 | Log.w(TAG, "readAll() looping, read partial size: " + (b.length - left) + |
| 690 | ", expect size: " + b.length); |
| Nick Pelly | 07b84cb | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 691 | } |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 692 | return b.length; |
| 693 | } |
| 694 | |
| 695 | private int readInt(InputStream is) throws IOException { |
| 696 | byte[] ibytes = new byte[4]; |
| 697 | int ret = readAll(is, ibytes); |
| Matthew Xie | f8035a7 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 698 | if (VDBG) Log.d(TAG, "inputStream.read ret: " + ret); |
| zzy | fab62db | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 699 | ByteBuffer bb = ByteBuffer.wrap(ibytes); |
| 700 | bb.order(ByteOrder.nativeOrder()); |
| 701 | return bb.getInt(); |
| Nick Pelly | 07b84cb | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 702 | } |
| Casper Bonde | c0a7c93 | 2015-04-09 09:24:48 +0200 | [diff] [blame] | 703 | |
| 704 | private int fillL2capRxBuffer() throws IOException { |
| 705 | mL2capBuffer.rewind(); |
| 706 | int ret = mSocketIS.read(mL2capBuffer.array()); |
| 707 | if(ret == -1) { |
| 708 | // reached end of stream - return -1 |
| 709 | mL2capBuffer.limit(0); |
| 710 | return -1; |
| 711 | } |
| 712 | mL2capBuffer.limit(ret); |
| 713 | return ret; |
| 714 | } |
| 715 | |
| 716 | |
| Nick Pelly | 53f441b | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 717 | } |