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