import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
+import android.bluetooth.BluetoothUuid;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
private static final int CREATE_RETRY_TIME = 10;
- private static final int DEFAULT_OPP_CHANNEL = 12;
-
- private final int mBtOppRfcommChannel;
-
private final BluetoothAdapter mAdapter;
private BluetoothServerSocket mBtServerSocket = null;
private ServerSocket mTcpServerSocket = null;
public BluetoothOppRfcommListener(BluetoothAdapter adapter) {
- this(adapter, DEFAULT_OPP_CHANNEL);
- }
-
- public BluetoothOppRfcommListener(BluetoothAdapter adapter, int channel) {
- mBtOppRfcommChannel = channel;
mAdapter = adapter;
}
+
public synchronized boolean start(Handler callback) {
if (mSocketAcceptThread == null) {
mCallback = callback;
* retry for 10 times
*/
for (int i = 0; i < CREATE_RETRY_TIME && !mInterrupted; i++) {
- //TODO(BT)
- /*
try {
- mBtServerSocket = mAdapter
- .listenUsingInsecureRfcommOn(mBtOppRfcommChannel);
+ if (V) Log.v(TAG, "Starting RFCOMM listener....");
+ mBtServerSocket = mAdapter.listenUsingInsecureRfcommWithServiceRecord("OBEX Object Push", BluetoothUuid.ObexObjectPush.getUuid());
+ if (V) Log.v(TAG, "Started RFCOMM listener....");
} catch (IOException e1) {
Log.e(TAG, "Error create RfcommServerSocket " + e1);
serverOK = false;
- }*/
+ }
if (!serverOK) {
synchronized (this) {
try {
- if (V) Log.v(TAG, "wait 3 seconds");
+ if (V) Log.v(TAG, "Wait 3 seconds");
Thread.sleep(3000);
} catch (InterruptedException e) {
Log.e(TAG, "socketAcceptThread thread was interrupted (3)");
mInterrupted = true;
}
if (!mInterrupted) {
- Log.i(TAG, "Accept thread started on channel " + mBtOppRfcommChannel);
+ Log.i(TAG, "Accept thread started.");
}
BluetoothSocket clientSocket;
- //TODO(BT)
- /*
while (!mInterrupted) {
try {
+ if (V) Log.v(TAG, "Accepting connection...");
clientSocket = mBtServerSocket.accept();
- Log.i(TAG, "Accepted connectoin from "
+ if (V) Log.v(TAG, "Accepted connection from "
+ clientSocket.getRemoteDevice());
BluetoothOppRfcommTransport transport = new BluetoothOppRfcommTransport(
clientSocket);
} catch (IOException e) {
Log.e(TAG, "Error accept connection " + e);
}
- }*/
+ }
Log.i(TAG, "BluetoothSocket listen thread finished");
}
}
private static final int RFCOMM_CONNECTED = 11;
- private static final int SDP_RESULT = 12;
-
private static final int SOCKET_ERROR_RETRY = 13;
private static final int CONNECT_WAIT_TIMEOUT = 45000;
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
- case SDP_RESULT:
- if (V) Log.v(TAG, "SDP request returned " + msg.arg1 + " (" +
- (System.currentTimeMillis() - mTimestamp + " ms)"));
- if (!((BluetoothDevice)msg.obj).equals(mBatch.mDestination)) {
- return;
- }
- try {
- mContext.unregisterReceiver(mReceiver);
- } catch (IllegalArgumentException e) {
- // ignore
- }
- if (msg.arg1 > 0) {
- mConnectThread = new
- SocketConnectThread(mBatch.mDestination, msg.arg1, false);
- mConnectThread.start();
- } else {
- /* SDP query fail case */
- Log.e(TAG, "SDP query failed!");
- markBatchFailed(BluetoothShare.STATUS_CONNECTION_ERROR);
- mBatch.mStatus = Constants.BATCH_STATUS_FAILED;
- }
-
- break;
case SOCKET_ERROR_RETRY:
mConnectThread = new
- SocketConnectThread((BluetoothDevice)msg.obj, msg.arg1, true);
+ SocketConnectThread((BluetoothDevice)msg.obj, true);
+
mConnectThread.start();
break;
case RFCOMM_ERROR:
mConnectThread = new SocketConnectThread("localhost", Constants.TCP_DEBUG_PORT, 0);
mConnectThread.start();
} else {
- int channel = BluetoothOppPreference.getInstance(mContext).getChannel(
- mBatch.mDestination, OPUSH_UUID16);
- if (channel != -1) {
- if (D) Log.d(TAG, "Get OPUSH channel " + channel + " from cache for " +
- mBatch.mDestination);
- mTimestamp = System.currentTimeMillis();
- mSessionHandler.obtainMessage(SDP_RESULT, channel, -1, mBatch.mDestination)
- .sendToTarget();
- } else {
- doOpushSdp();
- }
- }
- }
-
- private void doOpushSdp() {
- if (V) Log.v(TAG, "Do Opush SDP request for address " + mBatch.mDestination);
-
- mTimestamp = System.currentTimeMillis();
-
- int channel;
- channel = mBatch.mDestination.getServiceChannel(BluetoothUuid.ObexObjectPush);
- if (channel != -1) {
- if (D) Log.d(TAG, "Get OPUSH channel " + channel + " from SDP for "
- + mBatch.mDestination);
-
- mSessionHandler.obtainMessage(SDP_RESULT, channel, -1, mBatch.mDestination)
- .sendToTarget();
- return;
-
- } else {
- if (V) Log.v(TAG, "Remote Service channel not in cache");
-
- if (!mBatch.mDestination.fetchUuidsWithSdp()) {
- Log.e(TAG, "Start SDP query failed");
- } else {
- // we expect framework send us Intent ACTION_UUID. otherwise we will fail
- if (V) Log.v(TAG, "Start new SDP, wait for result");
- IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_UUID);
- mContext.registerReceiver(mReceiver, intentFilter);
- return;
- }
+ mConnectThread = new SocketConnectThread(mBatch.mDestination,false);
+ mConnectThread.start();
}
- Message msg = mSessionHandler.obtainMessage(SDP_RESULT, channel, -1, mBatch.mDestination);
- mSessionHandler.sendMessageDelayed(msg, 2000);
}
- private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- if (intent.getAction().equals(BluetoothDevice.ACTION_UUID)) {
- BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
- if (V) Log.v(TAG, "ACTION_UUID for device " + device);
- if (device.equals(mBatch.mDestination)) {
- int channel = -1;
- Parcelable[] uuid = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);
- if (uuid != null) {
- ParcelUuid[] uuids = new ParcelUuid[uuid.length];
- for (int i = 0; i < uuid.length; i++) {
- uuids[i] = (ParcelUuid)uuid[i];
- }
- if (BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.ObexObjectPush)) {
- if (V) Log.v(TAG, "SDP get OPP result for device " + device);
- channel = mBatch.mDestination
- .getServiceChannel(BluetoothUuid.ObexObjectPush);
- }
- }
- mSessionHandler.obtainMessage(SDP_RESULT, channel, -1, mBatch.mDestination)
- .sendToTarget();
- }
- }
- }
- };
-
private SocketConnectThread mConnectThread;
private class SocketConnectThread extends Thread {
mRetry = retry;
}
+ /* create a Rfcomm Socket */
+ public SocketConnectThread(BluetoothDevice device, boolean
+ retry) {
+ super("Socket Connect Thread");
+ this.device = device;
+ this.host = null;
+ this.channel = -1;
+ isConnected = false;
+ mRetry = retry;
+ }
+
public void interrupt() {
if (!Constants.USE_TCP_DEBUG) {
if (btSocket != null) {
/* Use BluetoothSocket to connect */
try {
- btSocket = device.createInsecureRfcommSocket(channel);
+ btSocket = device.createInsecureRfcommSocketToServiceRecord(BluetoothUuid.ObexObjectPush.getUuid());
} catch (IOException e1) {
- Log.e(TAG, "Rfcomm socket create error");
+ Log.e(TAG, "Rfcomm socket create error",e1);
markConnectionFailed(btSocket);
return;
}
BluetoothOppRfcommTransport transport;
transport = new BluetoothOppRfcommTransport(btSocket);
- BluetoothOppPreference.getInstance(mContext).setChannel(device, OPUSH_UUID16,
- channel);
BluetoothOppPreference.getInstance(mContext).setName(device, device.getName());
if (V) Log.v(TAG, "Send transport message " + transport.toString());
mSessionHandler.obtainMessage(RFCOMM_CONNECTED, transport).sendToTarget();
} catch (IOException e) {
- Log.e(TAG, "Rfcomm socket connect exception");
+ Log.e(TAG, "Rfcomm socket connect exception",e);
// If the devices were paired before, but unpaired on the
// remote end, it will return an error for the auth request
// for the socket connection. Link keys will get exchanged
// inform this socket asking it to retry apart from a blind
// delayed retry.
if (!mRetry && e.getMessage().equals(SOCKET_LINK_KEY_ERROR)) {
- Message msg = mSessionHandler.obtainMessage(SOCKET_ERROR_RETRY,
- channel, -1, device);
+ Message msg = mSessionHandler.obtainMessage(SOCKET_ERROR_RETRY,-1,-1,device);
mSessionHandler.sendMessageDelayed(msg, 1500);
} else {
- BluetoothOppPreference.getInstance(mContext)
- .removeChannel(device, OPUSH_UUID16);
markConnectionFailed(btSocket);
}
}
import android.bluetooth.BluetoothSocket;
import android.bluetooth.IBluetooth;
import android.bluetooth.IBluetoothPbap;
+import android.bluetooth.BluetoothUuid;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
+
+
import com.android.bluetooth.R;
import java.io.IOException;
private static final int AUTH_TIMEOUT = 3;
- private static final int PORT_NUM = 19;
private static final int USER_CONFIRM_TIMEOUT_VALUE = 30000;
try {
// It is mandatory for PSE to support initiation of bonding and
// encryption.
- mServerSocket = mAdapter.listenUsingEncryptedRfcommOn(PORT_NUM);
+ mServerSocket = mAdapter.listenUsingRfcommWithServiceRecord("OBEX Phoneboox Access Server", BluetoothUuid.PBAP_PSE.getUuid());
} catch (IOException e) {
Log.e(TAG, "Error create RfcommServerSocket " + e.toString());
initSocketOK = false;
}
if (initSocketOK) {
- if (VERBOSE) Log.v(TAG, "Succeed to create listening socket on channel " + PORT_NUM);
+ if (VERBOSE) Log.v(TAG, "Succeed to create listening socket ");
} else {
Log.e(TAG, "Error to create listening socket after " + CREATE_RETRY_TIME + " try");
try {
closeSocket(false, true);
- mConnSocket = null;
+ mConnSocket = null;
} catch (IOException e) {
Log.e(TAG, "closeSocket error: " + e.toString());
}
// Last obex transaction is finished, we start to listen for incoming
// connection again
if (mAdapter.isEnabled()) {
- //TODO(BT)
- // startRfcommSocketListener();
+ startRfcommSocketListener();
}
setState(BluetoothPbap.STATE_DISCONNECTED);
}
public void run() {
while (!stopped) {
try {
+ if (VERBOSE) Log.v(TAG, "Accepting socket connection...");
mConnSocket = mServerSocket.accept();
+ if (VERBOSE) Log.v(TAG, "Accepted socket connection...");
mRemoteDevice = mConnSocket.getRemoteDevice();
if (mRemoteDevice == null) {
switch (msg.what) {
case START_LISTENER:
if (mAdapter.isEnabled()) {
- //TODO(BT)
- //startRfcommSocketListener();
+ startRfcommSocketListener();
} else {
closeService();// release all resources
}