OSDN Git Service

Initial checkin of OPP and PBAP
authorfredc <fredc@broadcom.com>
Sat, 31 Mar 2012 06:27:24 +0000 (23:27 -0700)
committerAndroid (Google) Code Review <android-gerrit@google.com>
Tue, 17 Jul 2012 04:55:24 +0000 (21:55 -0700)
Change-Id: Ie43a26874bd5f0c00d69c2ce02430f4a16da327a

src/com/android/bluetooth/opp/BluetoothOppRfcommListener.java
src/com/android/bluetooth/opp/BluetoothOppTransfer.java
src/com/android/bluetooth/pbap/BluetoothPbapService.java

index 3f13984..2e4f200 100644 (file)
@@ -39,6 +39,7 @@ import java.net.Socket;
 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;
@@ -61,10 +62,6 @@ public class BluetoothOppRfcommListener {
 
     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;
@@ -72,14 +69,10 @@ public class BluetoothOppRfcommListener {
     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;
@@ -120,19 +113,18 @@ public class BluetoothOppRfcommListener {
                          * 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)");
@@ -148,15 +140,14 @@ public class BluetoothOppRfcommListener {
                             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);
@@ -168,7 +159,7 @@ public class BluetoothOppRfcommListener {
                             } catch (IOException e) {
                                 Log.e(TAG, "Error accept connection " + e);
                             }
-                        }*/
+                        }
                         Log.i(TAG, "BluetoothSocket listen thread finished");
                     }
                 }
index ec9c7db..1f8690b 100755 (executable)
@@ -76,8 +76,6 @@ public class BluetoothOppTransfer implements BluetoothOppBatch.BluetoothOppBatch
 
     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;
@@ -137,32 +135,10 @@ public class BluetoothOppTransfer implements BluetoothOppBatch.BluetoothOppBatch
         @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:
@@ -510,79 +486,11 @@ public class BluetoothOppTransfer implements BluetoothOppBatch.BluetoothOppBatch
             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 {
@@ -620,6 +528,17 @@ public class BluetoothOppTransfer implements BluetoothOppBatch.BluetoothOppBatch
             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) {
@@ -694,9 +613,9 @@ public class BluetoothOppTransfer implements BluetoothOppBatch.BluetoothOppBatch
                 /* 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;
                 }
@@ -708,15 +627,13 @@ public class BluetoothOppTransfer implements BluetoothOppBatch.BluetoothOppBatch
                     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
@@ -724,12 +641,9 @@ public class BluetoothOppTransfer implements BluetoothOppBatch.BluetoothOppBatch
                     // 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);
                     }
                 }
index 93d9b44..a5c5f20 100644 (file)
@@ -44,6 +44,7 @@ import android.bluetooth.BluetoothServerSocket;
 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;
@@ -56,6 +57,8 @@ import android.telephony.TelephonyManager;
 import android.text.TextUtils;
 import android.util.Log;
 
+
+
 import com.android.bluetooth.R;
 
 import java.io.IOException;
@@ -127,7 +130,6 @@ public class BluetoothPbapService extends Service {
 
     private static final int AUTH_TIMEOUT = 3;
 
-    private static final int PORT_NUM = 19;
 
     private static final int USER_CONFIRM_TIMEOUT_VALUE = 30000;
 
@@ -338,7 +340,7 @@ public class BluetoothPbapService extends Service {
             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;
@@ -359,7 +361,7 @@ public class BluetoothPbapService extends Service {
         }
 
         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");
@@ -468,15 +470,14 @@ public class BluetoothPbapService extends Service {
 
         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);
     }
@@ -512,7 +513,9 @@ public class BluetoothPbapService extends Service {
         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) {
@@ -583,8 +586,7 @@ public class BluetoothPbapService extends Service {
             switch (msg.what) {
                 case START_LISTENER:
                     if (mAdapter.isEnabled()) {
-                        //TODO(BT)
-                        //startRfcommSocketListener();
+                        startRfcommSocketListener();
                     } else {
                         closeService();// release all resources
                     }