BluetoothOppRfcommListener.java revision 3fafe61ef25c1899fdc817c52163aec16c31055c
1/*
2 * Copyright (c) 2008-2009, Motorola, Inc.
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * - Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * - Neither the name of the Motorola, Inc. nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33package com.android.bluetooth.opp;
34
35import java.io.IOException;
36import java.net.ServerSocket;
37import java.net.Socket;
38
39import android.bluetooth.BluetoothAdapter;
40import android.bluetooth.BluetoothServerSocket;
41import android.bluetooth.BluetoothSocket;
42import android.bluetooth.BluetoothUuid;
43import android.os.Handler;
44import android.os.Message;
45import android.util.Log;
46
47/**
48 * This class listens on OPUSH channel for incoming connection
49 */
50public class BluetoothOppRfcommListener {
51    private static final String TAG = "BtOppRfcommListener";
52
53    private static final boolean V = Constants.VERBOSE;
54
55    public static final int MSG_INCOMING_BTOPP_CONNECTION = 100;
56
57    private volatile boolean mInterrupted;
58
59    private Thread mSocketAcceptThread;
60
61    private Handler mCallback;
62
63    private static final int CREATE_RETRY_TIME = 10;
64
65    private final BluetoothAdapter mAdapter;
66
67    private BluetoothServerSocket mBtServerSocket = null;
68
69    private ServerSocket mTcpServerSocket = null;
70
71    public BluetoothOppRfcommListener(BluetoothAdapter adapter) {
72        mAdapter = adapter;
73    }
74
75
76    public synchronized boolean start(Handler callback) {
77        if (mSocketAcceptThread == null) {
78            mCallback = callback;
79
80            mSocketAcceptThread = new Thread(TAG) {
81
82                public void run() {
83                    if (Constants.USE_TCP_DEBUG) {
84                        try {
85                            if (V) Log.v(TAG, "Create TCP ServerSocket");
86                            mTcpServerSocket = new ServerSocket(Constants.TCP_DEBUG_PORT, 1);
87                        } catch (IOException e) {
88                            Log.e(TAG, "Error listing on port" + Constants.TCP_DEBUG_PORT);
89                            mInterrupted = true;
90                        }
91                        while (!mInterrupted) {
92                            try {
93                                Socket clientSocket = mTcpServerSocket.accept();
94
95                                if (V) Log.v(TAG, "Socket connected!");
96                                TestTcpTransport transport = new TestTcpTransport(clientSocket);
97                                Message msg = Message.obtain();
98                                msg.setTarget(mCallback);
99                                msg.what = MSG_INCOMING_BTOPP_CONNECTION;
100                                msg.obj = transport;
101                                msg.sendToTarget();
102
103                            } catch (IOException e) {
104                                Log.e(TAG, "Error accept connection " + e);
105                            }
106                        }
107                        if (V) Log.v(TAG, "TCP listen thread finished");
108                    } else {
109                        boolean serverOK = true;
110
111                        /*
112                         * it's possible that create will fail in some cases.
113                         * retry for 10 times
114                         */
115                        for (int i = 0; i < CREATE_RETRY_TIME && !mInterrupted; i++) {
116                            try {
117                                if (V) Log.v(TAG, "Starting RFCOMM listener....");
118                                mBtServerSocket = mAdapter.listenUsingInsecureRfcommWithServiceRecord("OBEX Object Push", BluetoothUuid.ObexObjectPush.getUuid());
119                                if (V) Log.v(TAG, "Started RFCOMM listener....");
120                            } catch (IOException e1) {
121                                Log.e(TAG, "Error create RfcommServerSocket " + e1);
122                                serverOK = false;
123                            }
124                            if (!serverOK) {
125                                synchronized (this) {
126                                    try {
127                                        if (V) Log.v(TAG, "Wait 3 seconds");
128                                        Thread.sleep(3000);
129                                    } catch (InterruptedException e) {
130                                        Log.e(TAG, "socketAcceptThread thread was interrupted (3)");
131                                        mInterrupted = true;
132                                    }
133                                }
134                            } else {
135                                break;
136                            }
137                        }
138                        if (!serverOK) {
139                            Log.e(TAG, "Error start listening after " + CREATE_RETRY_TIME + " try");
140                            mInterrupted = true;
141                        }
142                        if (!mInterrupted) {
143                            Log.i(TAG, "Accept thread started.");
144                        }
145                        BluetoothSocket clientSocket;
146                        while (!mInterrupted) {
147                            try {
148                                if (V) Log.v(TAG, "Accepting connection...");
149                                clientSocket = mBtServerSocket.accept();
150                                if (V) Log.v(TAG, "Accepted connection from "
151                                        + clientSocket.getRemoteDevice());
152                                BluetoothOppRfcommTransport transport = new BluetoothOppRfcommTransport(
153                                        clientSocket);
154                                Message msg = Message.obtain();
155                                msg.setTarget(mCallback);
156                                msg.what = MSG_INCOMING_BTOPP_CONNECTION;
157                                msg.obj = transport;
158                                msg.sendToTarget();
159                            } catch (IOException e) {
160                                Log.e(TAG, "Error accept connection " + e);
161                            }
162                        }
163                        Log.i(TAG, "BluetoothSocket listen thread finished");
164                    }
165                }
166            };
167            mInterrupted = false;
168            if(!Constants.USE_TCP_SIMPLE_SERVER) {
169                mSocketAcceptThread.start();
170            }
171        }
172        return true;
173    }
174
175    public synchronized void stop() {
176        if (mSocketAcceptThread != null) {
177            Log.i(TAG, "stopping Accept Thread");
178
179            mInterrupted = true;
180            if (Constants.USE_TCP_DEBUG) {
181                if (V) Log.v(TAG, "close mTcpServerSocket");
182                if (mTcpServerSocket != null) {
183                    try {
184                        mTcpServerSocket.close();
185                        mTcpServerSocket = null;
186                    } catch (IOException e) {
187                        Log.e(TAG, "Error close mTcpServerSocket");
188                    }
189                }
190            } else {
191                if (V) Log.v(TAG, "close mBtServerSocket");
192
193                if (mBtServerSocket != null) {
194                    try {
195                        mBtServerSocket.close();
196                        mBtServerSocket = null;
197                    } catch (IOException e) {
198                        Log.e(TAG, "Error close mBtServerSocket");
199                    }
200                }
201            }
202            try {
203                mSocketAcceptThread.interrupt();
204                if (V) Log.v(TAG, "waiting for thread to terminate");
205                mSocketAcceptThread.join();
206                mSocketAcceptThread = null;
207                mCallback = null;
208            } catch (InterruptedException e) {
209                if (V) Log.v(TAG, "Interrupted waiting for Accept Thread to join");
210            }
211        }
212    }
213}
214