Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 1 | /* |
Zhihai Xu | fa0fd39 | 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 | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | package android.bluetooth; |
| 18 | |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 19 | import android.os.ParcelUuid; |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 20 | import android.os.ParcelFileDescriptor; |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 21 | import android.os.RemoteException; |
| 22 | import android.util.Log; |
| 23 | |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 24 | import java.io.Closeable; |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 25 | import java.io.FileDescriptor; |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 26 | import java.io.IOException; |
| 27 | import java.io.InputStream; |
| 28 | import java.io.OutputStream; |
Jeff Sharkey | fea17de | 2013-06-11 14:13:09 -0700 | [diff] [blame] | 29 | import java.util.Locale; |
zzy | b49a896 | 2012-10-11 14:52:43 -0700 | [diff] [blame] | 30 | import java.util.UUID; |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 31 | import android.net.LocalSocket; |
| 32 | import java.nio.ByteOrder; |
| 33 | import java.nio.ByteBuffer; |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 34 | /** |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 35 | * A connected or connecting Bluetooth socket. |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 36 | * |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 37 | * <p>The interface for Bluetooth Sockets is similar to that of TCP sockets: |
| 38 | * {@link java.net.Socket} and {@link java.net.ServerSocket}. On the server |
| 39 | * side, use a {@link BluetoothServerSocket} to create a listening server |
Scott Main | 9fab0ae | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 40 | * socket. When a connection is accepted by the {@link BluetoothServerSocket}, |
| 41 | * it will return a new {@link BluetoothSocket} to manage the connection. |
Jake Hamby | f51eada | 2010-09-21 13:39:53 -0700 | [diff] [blame] | 42 | * On the client side, use a single {@link BluetoothSocket} to both initiate |
Scott Main | 9fab0ae | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 43 | * an outgoing connection and to manage the connection. |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 44 | * |
Scott Main | 9fab0ae | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 45 | * <p>The most common type of Bluetooth socket is RFCOMM, which is the type |
| 46 | * supported by the Android APIs. RFCOMM is a connection-oriented, streaming |
| 47 | * transport over Bluetooth. It is also known as the Serial Port Profile (SPP). |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 48 | * |
Scott Main | 9fab0ae | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 49 | * <p>To create a {@link BluetoothSocket} for connecting to a known device, use |
| 50 | * {@link BluetoothDevice#createRfcommSocketToServiceRecord |
| 51 | * BluetoothDevice.createRfcommSocketToServiceRecord()}. |
| 52 | * Then call {@link #connect()} to attempt a connection to the remote device. |
| 53 | * This call will block until a connection is established or the connection |
| 54 | * fails. |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 55 | * |
Scott Main | 9fab0ae | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 56 | * <p>To create a {@link BluetoothSocket} as a server (or "host"), see the |
| 57 | * {@link BluetoothServerSocket} documentation. |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 58 | * |
Scott Main | 9fab0ae | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 59 | * <p>Once the socket is connected, whether initiated as a client or accepted |
| 60 | * as a server, open the IO streams by calling {@link #getInputStream} and |
| 61 | * {@link #getOutputStream} in order to retrieve {@link java.io.InputStream} |
| 62 | * and {@link java.io.OutputStream} objects, respectively, which are |
| 63 | * automatically connected to the socket. |
| 64 | * |
| 65 | * <p>{@link BluetoothSocket} is thread |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 66 | * safe. In particular, {@link #close} will always immediately abort ongoing |
| 67 | * operations and close the socket. |
Nick Pelly | cf44059 | 2009-09-08 10:12:06 -0700 | [diff] [blame] | 68 | * |
Scott Main | 9fab0ae | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 69 | * <p class="note"><strong>Note:</strong> |
| 70 | * Requires the {@link android.Manifest.permission#BLUETOOTH} permission. |
| 71 | * |
Joe Fernandez | 3aef8e1d | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 72 | * <div class="special reference"> |
| 73 | * <h3>Developer Guides</h3> |
| 74 | * <p>For more information about using Bluetooth, read the |
| 75 | * <a href="{@docRoot}guide/topics/wireless/bluetooth.html">Bluetooth</a> developer guide.</p> |
| 76 | * </div> |
| 77 | * |
Scott Main | 9fab0ae | 2009-11-03 18:17:59 -0800 | [diff] [blame] | 78 | * {@see BluetoothServerSocket} |
| 79 | * {@see java.io.InputStream} |
| 80 | * {@see java.io.OutputStream} |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 81 | */ |
| 82 | public final class BluetoothSocket implements Closeable { |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 83 | private static final String TAG = "BluetoothSocket"; |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 84 | private static final boolean DBG = true; |
| 85 | private static final boolean VDBG = false; |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 86 | |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 87 | /** @hide */ |
| 88 | public static final int MAX_RFCOMM_CHANNEL = 30; |
| 89 | |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 90 | /** Keep TYPE_ fields in sync with BluetoothSocket.cpp */ |
Nick Pelly | 6a669fa | 2009-06-02 15:57:18 -0700 | [diff] [blame] | 91 | /*package*/ static final int TYPE_RFCOMM = 1; |
| 92 | /*package*/ static final int TYPE_SCO = 2; |
| 93 | /*package*/ static final int TYPE_L2CAP = 3; |
| 94 | |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 95 | /*package*/ static final int EBADFD = 77; |
| 96 | /*package*/ static final int EADDRINUSE = 98; |
| 97 | |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 98 | /*package*/ static final int SEC_FLAG_ENCRYPT = 1; |
| 99 | /*package*/ static final int SEC_FLAG_AUTH = 1 << 1; |
| 100 | |
Nick Pelly | 6a669fa | 2009-06-02 15:57:18 -0700 | [diff] [blame] | 101 | private final int mType; /* one of TYPE_RFCOMM etc */ |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 102 | private BluetoothDevice mDevice; /* remote device */ |
| 103 | private String mAddress; /* remote address */ |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 104 | private final boolean mAuth; |
| 105 | private final boolean mEncrypt; |
| 106 | private final BluetoothInputStream mInputStream; |
| 107 | private final BluetoothOutputStream mOutputStream; |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 108 | private final ParcelUuid mUuid; |
| 109 | private ParcelFileDescriptor mPfd; |
| 110 | private LocalSocket mSocket; |
| 111 | private InputStream mSocketIS; |
| 112 | private OutputStream mSocketOS; |
Mike Lockwood | 68c692d | 2013-10-02 07:56:46 -0700 | [diff] [blame] | 113 | private int mPort; /* RFCOMM channel or L2CAP psm */ |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 114 | private int mFd; |
| 115 | private String mServiceName; |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 116 | private static int PROXY_CONNECTION_TIMEOUT = 5000; |
| 117 | |
| 118 | private static int SOCK_SIGNAL_SIZE = 16; |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 119 | |
Matthew Xie | ceb6c9f | 2011-05-03 19:50:20 -0700 | [diff] [blame] | 120 | private enum SocketState { |
| 121 | INIT, |
| 122 | CONNECTED, |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 123 | LISTENING, |
| 124 | CLOSED, |
Matthew Xie | ceb6c9f | 2011-05-03 19:50:20 -0700 | [diff] [blame] | 125 | } |
Nick Pelly | 71c3c78 | 2009-09-02 11:51:35 -0700 | [diff] [blame] | 126 | |
Matthew Xie | ceb6c9f | 2011-05-03 19:50:20 -0700 | [diff] [blame] | 127 | /** prevents all native calls after destroyNative() */ |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 128 | private volatile SocketState mSocketState; |
Matthew Xie | ceb6c9f | 2011-05-03 19:50:20 -0700 | [diff] [blame] | 129 | |
| 130 | /** protects mSocketState */ |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 131 | //private final ReentrantReadWriteLock mLock; |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 132 | |
| 133 | /** |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 134 | * Construct a BluetoothSocket. |
Nick Pelly | 6a669fa | 2009-06-02 15:57:18 -0700 | [diff] [blame] | 135 | * @param type type of socket |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 136 | * @param fd fd to use for connected socket, or -1 for a new socket |
| 137 | * @param auth require the remote device to be authenticated |
| 138 | * @param encrypt require the connection to be encrypted |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 139 | * @param device remote device that this socket can connect to |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 140 | * @param port remote port |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 141 | * @param uuid SDP uuid |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 142 | * @throws IOException On error, for example Bluetooth not available, or |
Jake Hamby | f51eada | 2010-09-21 13:39:53 -0700 | [diff] [blame] | 143 | * insufficient privileges |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 144 | */ |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 145 | /*package*/ BluetoothSocket(int type, int fd, boolean auth, boolean encrypt, |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 146 | BluetoothDevice device, int port, ParcelUuid uuid) throws IOException { |
| 147 | if (type == BluetoothSocket.TYPE_RFCOMM && uuid == null && fd == -1) { |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 148 | if (port < 1 || port > MAX_RFCOMM_CHANNEL) { |
| 149 | throw new IOException("Invalid RFCOMM channel: " + port); |
| 150 | } |
| 151 | } |
zzy | b49a896 | 2012-10-11 14:52:43 -0700 | [diff] [blame] | 152 | if(uuid != null) |
| 153 | mUuid = uuid; |
| 154 | else mUuid = new ParcelUuid(new UUID(0, 0)); |
Nick Pelly | 6a669fa | 2009-06-02 15:57:18 -0700 | [diff] [blame] | 155 | mType = type; |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 156 | mAuth = auth; |
| 157 | mEncrypt = encrypt; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 158 | mDevice = device; |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 159 | mPort = port; |
| 160 | mFd = fd; |
| 161 | |
| 162 | mSocketState = SocketState.INIT; |
| 163 | |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 164 | if (device == null) { |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 165 | // Server socket |
| 166 | mAddress = BluetoothAdapter.getDefaultAdapter().getAddress(); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 167 | } else { |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 168 | // Remote socket |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 169 | mAddress = device.getAddress(); |
| 170 | } |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 171 | mInputStream = new BluetoothInputStream(this); |
| 172 | mOutputStream = new BluetoothOutputStream(this); |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 173 | } |
| 174 | private BluetoothSocket(BluetoothSocket s) { |
| 175 | mUuid = s.mUuid; |
| 176 | mType = s.mType; |
| 177 | mAuth = s.mAuth; |
| 178 | mEncrypt = s.mEncrypt; |
| 179 | mPort = s.mPort; |
| 180 | mInputStream = new BluetoothInputStream(this); |
| 181 | mOutputStream = new BluetoothOutputStream(this); |
| 182 | mServiceName = s.mServiceName; |
| 183 | } |
| 184 | private BluetoothSocket acceptSocket(String RemoteAddr) throws IOException { |
| 185 | BluetoothSocket as = new BluetoothSocket(this); |
| 186 | as.mSocketState = SocketState.CONNECTED; |
| 187 | FileDescriptor[] fds = mSocket.getAncillaryFileDescriptors(); |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 188 | if (VDBG) Log.d(TAG, "socket fd passed by stack fds: " + fds); |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 189 | if(fds == null || fds.length != 1) { |
| 190 | Log.e(TAG, "socket fd passed from stack failed, fds: " + fds); |
zzy | 71bfafc | 2013-04-16 17:17:37 -0700 | [diff] [blame] | 191 | as.close(); |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 192 | throw new IOException("bt socket acept failed"); |
| 193 | } |
| 194 | as.mSocket = new LocalSocket(fds[0]); |
| 195 | as.mSocketIS = as.mSocket.getInputStream(); |
| 196 | as.mSocketOS = as.mSocket.getOutputStream(); |
| 197 | as.mAddress = RemoteAddr; |
| 198 | as.mDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(RemoteAddr); |
| 199 | return as; |
| 200 | } |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 201 | /** |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 202 | * Construct a BluetoothSocket from address. Used by native code. |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 203 | * @param type type of socket |
| 204 | * @param fd fd to use for connected socket, or -1 for a new socket |
| 205 | * @param auth require the remote device to be authenticated |
| 206 | * @param encrypt require the connection to be encrypted |
| 207 | * @param address remote device that this socket can connect to |
| 208 | * @param port remote port |
| 209 | * @throws IOException On error, for example Bluetooth not available, or |
Jake Hamby | f51eada | 2010-09-21 13:39:53 -0700 | [diff] [blame] | 210 | * insufficient privileges |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 211 | */ |
| 212 | private BluetoothSocket(int type, int fd, boolean auth, boolean encrypt, String address, |
| 213 | int port) throws IOException { |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 214 | this(type, fd, auth, encrypt, new BluetoothDevice(address), port, null); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 217 | /** @hide */ |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 218 | @Override |
| 219 | protected void finalize() throws Throwable { |
| 220 | try { |
| 221 | close(); |
| 222 | } finally { |
| 223 | super.finalize(); |
| 224 | } |
| 225 | } |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 226 | private int getSecurityFlags() { |
| 227 | int flags = 0; |
| 228 | if(mAuth) |
| 229 | flags |= SEC_FLAG_AUTH; |
| 230 | if(mEncrypt) |
| 231 | flags |= SEC_FLAG_ENCRYPT; |
| 232 | return flags; |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | /** |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 236 | * Get the remote device this socket is connecting, or connected, to. |
| 237 | * @return remote device |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 238 | */ |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 239 | public BluetoothDevice getRemoteDevice() { |
| 240 | return mDevice; |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Get the input stream associated with this socket. |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 245 | * <p>The input stream will be returned even if the socket is not yet |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 246 | * connected, but operations on that stream will throw IOException until |
| 247 | * the associated socket is connected. |
| 248 | * @return InputStream |
| 249 | */ |
| 250 | public InputStream getInputStream() throws IOException { |
| 251 | return mInputStream; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Get the output stream associated with this socket. |
Nick Pelly | 45e2704 | 2009-08-19 11:00:00 -0700 | [diff] [blame] | 256 | * <p>The output stream will be returned even if the socket is not yet |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 257 | * connected, but operations on that stream will throw IOException until |
| 258 | * the associated socket is connected. |
| 259 | * @return OutputStream |
| 260 | */ |
| 261 | public OutputStream getOutputStream() throws IOException { |
| 262 | return mOutputStream; |
| 263 | } |
| 264 | |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 265 | /** |
Matthew Xie | ceb6c9f | 2011-05-03 19:50:20 -0700 | [diff] [blame] | 266 | * Get the connection status of this socket, ie, whether there is an active connection with |
| 267 | * remote device. |
| 268 | * @return true if connected |
| 269 | * false if not connected |
| 270 | */ |
| 271 | public boolean isConnected() { |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 272 | return mSocketState == SocketState.CONNECTED; |
| 273 | } |
| 274 | |
| 275 | /*package*/ void setServiceName(String name) { |
| 276 | mServiceName = name; |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Attempt to connect to a remote device. |
| 281 | * <p>This method will block until a connection is made or the connection |
| 282 | * fails. If this method returns without an exception then this socket |
| 283 | * is now connected. |
| 284 | * <p>Creating new connections to |
| 285 | * remote Bluetooth devices should not be attempted while device discovery |
| 286 | * is in progress. Device discovery is a heavyweight procedure on the |
| 287 | * Bluetooth adapter and will significantly slow a device connection. |
| 288 | * Use {@link BluetoothAdapter#cancelDiscovery()} to cancel an ongoing |
| 289 | * discovery. Discovery is not managed by the Activity, |
| 290 | * but is run as a system service, so an application should always call |
| 291 | * {@link BluetoothAdapter#cancelDiscovery()} even if it |
| 292 | * did not directly request a discovery, just to be sure. |
| 293 | * <p>{@link #close} can be used to abort this call from another thread. |
| 294 | * @throws IOException on error, for example connection failure |
| 295 | */ |
| 296 | public void connect() throws IOException { |
| 297 | if (mDevice == null) throw new IOException("Connect is called on null device"); |
| 298 | |
| 299 | try { |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 300 | if (mSocketState == SocketState.CLOSED) throw new IOException("socket closed"); |
fredc | 903ac6f | 2012-04-24 03:59:57 -0700 | [diff] [blame] | 301 | IBluetooth bluetoothProxy = BluetoothAdapter.getDefaultAdapter().getBluetoothService(null); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 302 | if (bluetoothProxy == null) throw new IOException("Bluetooth is off"); |
| 303 | mPfd = bluetoothProxy.connectSocket(mDevice, mType, |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 304 | mUuid, mPort, getSecurityFlags()); |
| 305 | synchronized(this) |
| 306 | { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 307 | if (DBG) Log.d(TAG, "connect(), SocketState: " + mSocketState + ", mPfd: " + mPfd); |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 308 | if (mSocketState == SocketState.CLOSED) throw new IOException("socket closed"); |
| 309 | if (mPfd == null) throw new IOException("bt socket connect failed"); |
| 310 | FileDescriptor fd = mPfd.getFileDescriptor(); |
| 311 | mSocket = new LocalSocket(fd); |
| 312 | mSocketIS = mSocket.getInputStream(); |
| 313 | mSocketOS = mSocket.getOutputStream(); |
| 314 | } |
| 315 | int channel = readInt(mSocketIS); |
| 316 | if (channel <= 0) |
| 317 | throw new IOException("bt socket connect failed"); |
| 318 | mPort = channel; |
| 319 | waitSocketSignal(mSocketIS); |
| 320 | synchronized(this) |
| 321 | { |
| 322 | if (mSocketState == SocketState.CLOSED) |
| 323 | throw new IOException("bt socket closed"); |
| 324 | mSocketState = SocketState.CONNECTED; |
| 325 | } |
| 326 | } catch (RemoteException e) { |
| 327 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 328 | } |
Matthew Xie | ceb6c9f | 2011-05-03 19:50:20 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /** |
Nick Pelly | 24bb9b8 | 2009-10-02 20:34:18 -0700 | [diff] [blame] | 332 | * Currently returns unix errno instead of throwing IOException, |
| 333 | * so that BluetoothAdapter can check the error code for EADDRINUSE |
| 334 | */ |
| 335 | /*package*/ int bindListen() { |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 336 | int ret; |
| 337 | if (mSocketState == SocketState.CLOSED) return EBADFD; |
fredc | 903ac6f | 2012-04-24 03:59:57 -0700 | [diff] [blame] | 338 | IBluetooth bluetoothProxy = BluetoothAdapter.getDefaultAdapter().getBluetoothService(null); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 339 | if (bluetoothProxy == null) { |
| 340 | Log.e(TAG, "bindListen fail, reason: bluetooth is off"); |
| 341 | return -1; |
| 342 | } |
Nick Pelly | 71c3c78 | 2009-09-02 11:51:35 -0700 | [diff] [blame] | 343 | try { |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 344 | mPfd = bluetoothProxy.createSocketChannel(mType, mServiceName, |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 345 | mUuid, mPort, getSecurityFlags()); |
| 346 | } catch (RemoteException e) { |
| 347 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 348 | return -1; |
| 349 | } |
| 350 | |
| 351 | // read out port number |
| 352 | try { |
| 353 | synchronized(this) { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 354 | if (VDBG) Log.d(TAG, "bindListen(), SocketState: " + mSocketState + ", mPfd: " + |
| 355 | mPfd); |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 356 | if(mSocketState != SocketState.INIT) return EBADFD; |
| 357 | if(mPfd == null) return -1; |
| 358 | FileDescriptor fd = mPfd.getFileDescriptor(); |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 359 | if (VDBG) Log.d(TAG, "bindListen(), new LocalSocket "); |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 360 | mSocket = new LocalSocket(fd); |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 361 | if (VDBG) Log.d(TAG, "bindListen(), new LocalSocket.getInputStream() "); |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 362 | mSocketIS = mSocket.getInputStream(); |
| 363 | mSocketOS = mSocket.getOutputStream(); |
| 364 | } |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 365 | if (VDBG) Log.d(TAG, "bindListen(), readInt mSocketIS: " + mSocketIS); |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 366 | int channel = readInt(mSocketIS); |
| 367 | synchronized(this) { |
| 368 | if(mSocketState == SocketState.INIT) |
| 369 | mSocketState = SocketState.LISTENING; |
| 370 | } |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 371 | if (VDBG) Log.d(TAG, "channel: " + channel); |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 372 | if (mPort == -1) { |
| 373 | mPort = channel; |
| 374 | } // else ASSERT(mPort == channel) |
| 375 | ret = 0; |
| 376 | } catch (IOException e) { |
| 377 | Log.e(TAG, "bindListen, fail to get port number, exception: " + e); |
| 378 | return -1; |
| 379 | } |
| 380 | return ret; |
Nick Pelly | 71c3c78 | 2009-09-02 11:51:35 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | /*package*/ BluetoothSocket accept(int timeout) throws IOException { |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 384 | BluetoothSocket acceptedSocket; |
| 385 | if (mSocketState != SocketState.LISTENING) throw new IOException("bt socket is not in listen state"); |
zzy | 652678a | 2012-11-22 11:52:44 -0800 | [diff] [blame] | 386 | if(timeout > 0) { |
| 387 | Log.d(TAG, "accept() set timeout (ms):" + timeout); |
| 388 | mSocket.setSoTimeout(timeout); |
| 389 | } |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 390 | String RemoteAddr = waitSocketSignal(mSocketIS); |
zzy | 652678a | 2012-11-22 11:52:44 -0800 | [diff] [blame] | 391 | if(timeout > 0) |
| 392 | mSocket.setSoTimeout(0); |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 393 | synchronized(this) |
| 394 | { |
| 395 | if (mSocketState != SocketState.LISTENING) |
| 396 | throw new IOException("bt socket is not in listen state"); |
| 397 | acceptedSocket = acceptSocket(RemoteAddr); |
| 398 | //quick drop the reference of the file handle |
Nick Pelly | 71c3c78 | 2009-09-02 11:51:35 -0700 | [diff] [blame] | 399 | } |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 400 | return acceptedSocket; |
Nick Pelly | 71c3c78 | 2009-09-02 11:51:35 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | /*package*/ int available() throws IOException { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 404 | if (VDBG) Log.d(TAG, "available: " + mSocketIS); |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 405 | return mSocketIS.available(); |
Nick Pelly | 71c3c78 | 2009-09-02 11:51:35 -0700 | [diff] [blame] | 406 | } |
zzy | 71bfafc | 2013-04-16 17:17:37 -0700 | [diff] [blame] | 407 | /** |
| 408 | * Wait until the data in sending queue is emptied. A polling version |
| 409 | * for flush implementation. Used to ensure the writing data afterwards will |
| 410 | * be packed in new RFCOMM frame. |
| 411 | * @throws IOException |
| 412 | * if an i/o error occurs. |
| 413 | */ |
| 414 | /*package*/ void flush() throws IOException { |
Zhihai Xu | 610770d | 2013-12-17 11:39:20 -0800 | [diff] [blame] | 415 | if (mSocketOS == null) throw new IOException("flush is called on null OutputStream"); |
zzy | 71bfafc | 2013-04-16 17:17:37 -0700 | [diff] [blame] | 416 | if (VDBG) Log.d(TAG, "flush: " + mSocketOS); |
| 417 | mSocketOS.flush(); |
| 418 | } |
Nick Pelly | 71c3c78 | 2009-09-02 11:51:35 -0700 | [diff] [blame] | 419 | |
| 420 | /*package*/ int read(byte[] b, int offset, int length) throws IOException { |
Zhihai Xu | 610770d | 2013-12-17 11:39:20 -0800 | [diff] [blame] | 421 | if (mSocketIS == null) throw new IOException("read is called on null InputStream"); |
| 422 | if (VDBG) Log.d(TAG, "read in: " + mSocketIS + " len: " + length); |
| 423 | int ret = mSocketIS.read(b, offset, length); |
| 424 | if(ret < 0) |
| 425 | throw new IOException("bt socket closed, read return: " + ret); |
| 426 | if (VDBG) Log.d(TAG, "read out: " + mSocketIS + " ret: " + ret); |
| 427 | return ret; |
Nick Pelly | 71c3c78 | 2009-09-02 11:51:35 -0700 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | /*package*/ int write(byte[] b, int offset, int length) throws IOException { |
Zhihai Xu | 610770d | 2013-12-17 11:39:20 -0800 | [diff] [blame] | 431 | if (mSocketOS == null) throw new IOException("write is called on null OutputStream"); |
| 432 | if (VDBG) Log.d(TAG, "write: " + mSocketOS + " length: " + length); |
| 433 | mSocketOS.write(b, offset, length); |
| 434 | // There is no good way to confirm since the entire process is asynchronous anyway |
| 435 | if (VDBG) Log.d(TAG, "write out: " + mSocketOS + " length: " + length); |
| 436 | return length; |
Nick Pelly | 71c3c78 | 2009-09-02 11:51:35 -0700 | [diff] [blame] | 437 | } |
| 438 | |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 439 | @Override |
| 440 | public void close() throws IOException { |
Matthew Xie | d77982e | 2012-11-29 20:26:19 -0800 | [diff] [blame] | 441 | if (VDBG) Log.d(TAG, "close() in, this: " + this + ", channel: " + mPort + ", state: " + mSocketState); |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 442 | if(mSocketState == SocketState.CLOSED) |
| 443 | return; |
| 444 | else |
| 445 | { |
| 446 | synchronized(this) |
| 447 | { |
| 448 | if(mSocketState == SocketState.CLOSED) |
| 449 | return; |
| 450 | mSocketState = SocketState.CLOSED; |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 451 | if (VDBG) Log.d(TAG, "close() this: " + this + ", channel: " + mPort + ", mSocketIS: " + mSocketIS + |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 452 | ", mSocketOS: " + mSocketOS + "mSocket: " + mSocket); |
| 453 | if(mSocket != null) { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 454 | if (VDBG) Log.d(TAG, "Closing mSocket: " + mSocket); |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 455 | mSocket.shutdownInput(); |
| 456 | mSocket.shutdownOutput(); |
| 457 | mSocket.close(); |
| 458 | mSocket = null; |
| 459 | } |
Zhihai Xu | 0177102 | 2014-01-20 12:04:23 -0800 | [diff] [blame] | 460 | if (mPfd != null) { |
| 461 | mPfd.close(); |
| 462 | mPfd = null; |
| 463 | } |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 464 | } |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 465 | } |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 466 | } |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 467 | |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 468 | /*package */ void removeChannel() { |
| 469 | } |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 470 | |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 471 | /*package */ int getPort() { |
| 472 | return mPort; |
| 473 | } |
| 474 | private String convertAddr(final byte[] addr) { |
Jeff Sharkey | fea17de | 2013-06-11 14:13:09 -0700 | [diff] [blame] | 475 | return String.format(Locale.US, "%02X:%02X:%02X:%02X:%02X:%02X", |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 476 | addr[0] , addr[1], addr[2], addr[3] , addr[4], addr[5]); |
| 477 | } |
| 478 | private String waitSocketSignal(InputStream is) throws IOException { |
| 479 | byte [] sig = new byte[SOCK_SIGNAL_SIZE]; |
| 480 | int ret = readAll(is, sig); |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 481 | if (VDBG) Log.d(TAG, "waitSocketSignal read 16 bytes signal ret: " + ret); |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 482 | ByteBuffer bb = ByteBuffer.wrap(sig); |
| 483 | bb.order(ByteOrder.nativeOrder()); |
| 484 | int size = bb.getShort(); |
zzy | 652678a | 2012-11-22 11:52:44 -0800 | [diff] [blame] | 485 | if(size != SOCK_SIGNAL_SIZE) |
| 486 | throw new IOException("Connection failure, wrong signal size: " + size); |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 487 | byte [] addr = new byte[6]; |
| 488 | bb.get(addr); |
| 489 | int channel = bb.getInt(); |
| 490 | int status = bb.getInt(); |
| 491 | String RemoteAddr = convertAddr(addr); |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 492 | if (VDBG) Log.d(TAG, "waitSocketSignal: sig size: " + size + ", remote addr: " |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 493 | + RemoteAddr + ", channel: " + channel + ", status: " + status); |
| 494 | if(status != 0) |
| 495 | throw new IOException("Connection failure, status: " + status); |
| 496 | return RemoteAddr; |
| 497 | } |
| 498 | private int readAll(InputStream is, byte[] b) throws IOException { |
| 499 | int left = b.length; |
| 500 | while(left > 0) { |
| 501 | int ret = is.read(b, b.length - left, left); |
fredc | d688353 | 2012-04-25 17:46:13 -0700 | [diff] [blame] | 502 | if(ret <= 0) |
zzy | 652678a | 2012-11-22 11:52:44 -0800 | [diff] [blame] | 503 | throw new IOException("read failed, socket might closed or timeout, read ret: " + ret); |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 504 | left -= ret; |
| 505 | if(left != 0) |
| 506 | Log.w(TAG, "readAll() looping, read partial size: " + (b.length - left) + |
| 507 | ", expect size: " + b.length); |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 508 | } |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 509 | return b.length; |
| 510 | } |
| 511 | |
| 512 | private int readInt(InputStream is) throws IOException { |
| 513 | byte[] ibytes = new byte[4]; |
| 514 | int ret = readAll(is, ibytes); |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 515 | if (VDBG) Log.d(TAG, "inputStream.read ret: " + ret); |
zzy | 3b147b7 | 2012-04-03 19:48:32 -0700 | [diff] [blame] | 516 | ByteBuffer bb = ByteBuffer.wrap(ibytes); |
| 517 | bb.order(ByteOrder.nativeOrder()); |
| 518 | return bb.getInt(); |
Nick Pelly | 16fb88a | 2009-10-07 07:44:03 +0200 | [diff] [blame] | 519 | } |
Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 520 | } |