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