FileDocCategorySizeDatePackage
BluetoothPbapSession.javaAPI DocAndroid 5.1 API10219Thu Mar 12 22:22:50 GMT 2015android.bluetooth.client.pbap

BluetoothPbapSession

public class BluetoothPbapSession extends Object implements android.os.Handler.Callback

Fields Summary
private static final String
TAG
private static final int
RFCOMM_CONNECTED
private static final int
RFCOMM_FAILED
public static final int
REQUEST_COMPLETED
public static final int
REQUEST_FAILED
public static final int
SESSION_CONNECTING
public static final int
SESSION_CONNECTED
public static final int
SESSION_DISCONNECTED
public static final int
AUTH_REQUESTED
public static final int
AUTH_TIMEOUT
public static final int
ACTION_LISTING
public static final int
ACTION_VCARD
public static final int
ACTION_PHONEBOOK_SIZE
private static final String
PBAP_UUID
private final android.bluetooth.BluetoothAdapter
mAdapter
private final android.bluetooth.BluetoothDevice
mDevice
private final android.os.Handler
mParentHandler
private final android.os.HandlerThread
mHandlerThread
private final android.os.Handler
mSessionHandler
private RfcommConnectThread
mConnectThread
private BluetoothPbapObexTransport
mTransport
private BluetoothPbapObexSession
mObexSession
private BluetoothPbapRequest
mPendingRequest
Constructors Summary
public BluetoothPbapSession(android.bluetooth.BluetoothDevice device, android.os.Handler handler)


         

        mAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mAdapter == null) {
            throw new NullPointerException("No Bluetooth adapter in the system");
        }

        mDevice = device;
        mParentHandler = handler;
        mConnectThread = null;
        mTransport = null;
        mObexSession = null;

        mHandlerThread = new HandlerThread("PBAP session handler",
                Process.THREAD_PRIORITY_BACKGROUND);
        mHandlerThread.start();
        mSessionHandler = new Handler(mHandlerThread.getLooper(), this);
    
Methods Summary
public voidabort()

        Log.d(TAG, "abort");

        /* fail pending request immediately */
        if (mPendingRequest != null) {
            mParentHandler.obtainMessage(REQUEST_FAILED, mPendingRequest).sendToTarget();
            mPendingRequest = null;
        }

        if (mObexSession != null) {
            mObexSession.abort();
        }
    
public booleanhandleMessage(android.os.Message msg)

        Log.d(TAG, "Handler: msg: " + msg.what);

        switch (msg.what) {
            case RFCOMM_FAILED:
                mConnectThread = null;

                mParentHandler.obtainMessage(SESSION_DISCONNECTED).sendToTarget();

                if (mPendingRequest != null) {
                    mParentHandler.obtainMessage(REQUEST_FAILED, mPendingRequest).sendToTarget();
                    mPendingRequest = null;
                }
                break;

            case RFCOMM_CONNECTED:
                mConnectThread = null;
                mTransport = (BluetoothPbapObexTransport) msg.obj;
                startObexSession();
                break;

            case BluetoothPbapObexSession.OBEX_SESSION_FAILED:
                stopObexSession();

                mParentHandler.obtainMessage(SESSION_DISCONNECTED).sendToTarget();

                if (mPendingRequest != null) {
                    mParentHandler.obtainMessage(REQUEST_FAILED, mPendingRequest).sendToTarget();
                    mPendingRequest = null;
                }
                break;

            case BluetoothPbapObexSession.OBEX_SESSION_CONNECTED:
                mParentHandler.obtainMessage(SESSION_CONNECTED).sendToTarget();

                if (mPendingRequest != null) {
                    mObexSession.schedule(mPendingRequest);
                    mPendingRequest = null;
                }
                break;

            case BluetoothPbapObexSession.OBEX_SESSION_DISCONNECTED:
                mParentHandler.obtainMessage(SESSION_DISCONNECTED).sendToTarget();
                stopRfcomm();
                break;

            case BluetoothPbapObexSession.OBEX_SESSION_REQUEST_COMPLETED:
                /* send to parent, process there */
                mParentHandler.obtainMessage(REQUEST_COMPLETED, msg.obj).sendToTarget();
                break;

            case BluetoothPbapObexSession.OBEX_SESSION_REQUEST_FAILED:
                /* send to parent, process there */
                mParentHandler.obtainMessage(REQUEST_FAILED, msg.obj).sendToTarget();
                break;

            case BluetoothPbapObexSession.OBEX_SESSION_AUTHENTICATION_REQUEST:
                /* send to parent, process there */
                mParentHandler.obtainMessage(AUTH_REQUESTED).sendToTarget();

                mSessionHandler
                        .sendMessageDelayed(
                                mSessionHandler
                                        .obtainMessage(BluetoothPbapObexSession.OBEX_SESSION_AUTHENTICATION_TIMEOUT),
                                30000);
                break;

            case BluetoothPbapObexSession.OBEX_SESSION_AUTHENTICATION_TIMEOUT:
                /* stop authentication */
                setAuthResponse(null);

                mParentHandler.obtainMessage(AUTH_TIMEOUT).sendToTarget();
                break;

            default:
                return false;
        }

        return true;
    
public booleanmakeRequest(BluetoothPbapRequest request)

        Log.v(TAG, "makeRequest: " + request.getClass().getSimpleName());

        if (mPendingRequest != null) {
            Log.w(TAG, "makeRequest: request already queued, exiting");
            return false;
        }

        if (mObexSession == null) {
            mPendingRequest = request;

            /*
             * since there is no pending request and no session it's safe to
             * assume that RFCOMM does not exist either and we should start
             * connecting it
             */
            startRfcomm();

            return true;
        }

        return mObexSession.schedule(request);
    
public booleansetAuthResponse(java.lang.String key)

        Log.d(TAG, "setAuthResponse key=" + key);

        mSessionHandler
                .removeMessages(BluetoothPbapObexSession.OBEX_SESSION_AUTHENTICATION_TIMEOUT);

        /* does not make sense to set auth response when OBEX session is down */
        if (mObexSession == null) {
            return false;
        }

        return mObexSession.setAuthReply(key);
    
public voidstart()

        Log.d(TAG, "start");

        startRfcomm();
    
private voidstartObexSession()

        Log.d(TAG, "startObexSession");

        mObexSession = new BluetoothPbapObexSession(mTransport);
        mObexSession.start(mSessionHandler);
    
private voidstartRfcomm()

        Log.d(TAG, "startRfcomm");

        if (mConnectThread == null && mObexSession == null) {
            mParentHandler.obtainMessage(SESSION_CONNECTING).sendToTarget();

            mConnectThread = new RfcommConnectThread();
            mConnectThread.start();
        }

        /*
         * don't care if mConnectThread is not null - it means RFCOMM is being
         * connected anyway
         */
    
public voidstop()

        Log.d(TAG, "Stop");

        stopObexSession();
        stopRfcomm();
    
private voidstopObexSession()

        Log.d(TAG, "stopObexSession");

        if (mObexSession != null) {
            mObexSession.stop();
            mObexSession = null;
        }
    
private voidstopRfcomm()

        Log.d(TAG, "stopRfcomm");

        if (mConnectThread != null) {
            try {
                mConnectThread.join();
            } catch (InterruptedException e) {
            }

            mConnectThread = null;
        }

        if (mTransport != null) {
            try {
                mTransport.close();
            } catch (IOException e) {
            }

            mTransport = null;
        }