opp分析(MSG_INCOMING_BTOPP_CONNECTION)

来源:互联网 发布:简易视频剪辑配乐软件 编辑:程序博客网 时间:2024/04/30 05:23

I/activity_thread(  882): TID:952 .../ActivityThread.cpp:6033:  [LIFESTATE][Service] onStart: ServiceArgsData{token=0xb869ee98 startId=2 args=Intent { cmp=BluetoothOppPbapService/.BluetoothOppService }}

.......

I/activity_thread(  882): TID:952 .../ActivityThread.cpp:6061:  Start Service is accomplished


710 int32_t BluetoothOppService::onStartCommand(const android::sp<Intent>& intent, int32_t flags, int32_t startId) {      //有疑问,这个什么时候被调用
711    GLOGENTRY();
712    GLOGI("Service onStartCommand");
713    int32_t retCode = Service::onStartCommand(intent, flags, startId);
714    if (retCode == START_STICKY) {
715        if (mAdapter == NULL) {
716            GLOGW("Local BT device is not enabled");
717        } else {
718            startListener();
719        }
720        updateFromProvider();
721    }
722    GLOGD("BluetoothOppService:: count %d", getStrongCount());
723    return retCode;
724 }


726 void BluetoothOppService::startListener() {
727    GLOGENTRY();
728    if (!mListenStarted) {
729        if (mAdapter->isEnabled()) {
730            GLOGV("Starting RfcommListener");
731            mHandler->sendMessage(mHandler->obtainMessage(START_LISTENER));
732            mListenStarted = true;
733        }
734    }
735 }


236 void BtoppServiceHandler::handleMessage(const sp<Message>& msg) {
237    GLOGENTRY();
238    GLOGI("BtoppServiceHandler::handleMessage, msg->what %d", msg->what);
239    sp<BluetoothOppService> mspbtoppser = mwpbtoppserice.promote();
240    CHECK_NULL_POINTER_RETURN(mspbtoppser);
241
242    switch (msg->what) {
243        case BluetoothOppService::START_LISTENER:
244            if (mspbtoppser->mAdapter->isEnabled()) {
245                mspbtoppser->startSocketListener();
246            }
247            break;


671    mSocketListener = new BluetoothOppRfcommListener(mAdapter);

737 void BluetoothOppService::startSocketListener() {
738    GLOGENTRY();
739    GLOGI("start RfcommListener");
740    mSocketListener->start(mHandler);
741    GLOGV("RfcommListener started");
742 }


193 bool BluetoothOppRfcommListener::start(const sp<Handler>& callback) {
194    GLOGENTRY();
195    if (mSocketAcceptThread == NULL) {
196        mCallback = callback;
197
198        mSocketAcceptThread = new BTORFCLThread(this);
199        mInterrupted = false;
200        if (!Constants::USE_TCP_SIMPLE_SERVER) {
201            mSocketAcceptThread->run("BtOppRfcommListener");
202        }
203    }
204    return true;
205 }


87 bool BTORFCLThread::threadLoop() {
88    GLOGENTRY();
89    sp<BluetoothOppRfcommListener> spbtorfcl = mwpbtorfcl.promote();
90    if (spbtorfcl == NULL) {
91        LOGE("spbtorfcl is NULL");
92        return false;
93    }
94
95    if (Constants::USE_TCP_DEBUG) {
96        GLOGV("Create TCP ServerSocket");
97        // TODO TODO TODO
98        // need ServerSocket implement
99        // spbtorfcl->mTcpServerSocket = new ServerSocket(Constants::TCP_DEBUG_PORT, 1);
100
101        while (!spbtorfcl->mInterrupted) {
102            // TODO TODO TODO
103            // need Socket implement
104            // Socket clientSocket = mTcpServerSocket.accept();
105
106            GLOGV("Socket connected!");
107            // TODO TODO TODO
108            // need TestTcpTransport implement
109#if 0
110            sp<TestTcpTransport> transport = new TestTcpTransport(clientSocket);
111            sp<Message> msg = Message::obtain();
112            msg->setTarget(spbtorfcl->mCallback);
113            msg->what = BluetoothOppRfcommListener::MSG_INCOMING_BTOPP_CONNECTION;
114            msg->obj = transport;
115            msg->sendToTarget();
116#endif  // mark
117        }
118        GLOGV("TCP listen thread finished");
119    } else {
120        bool serverOK = true;
121        GLOGI("Create BtServerSocket");
122        /*
123         * it's possible that create will fail in some cases.
124         * retry for 10 times
125         */
126        for (int32_t i = 0; i < BluetoothOppRfcommListener::CREATE_RETRY_TIME && !spbtorfcl->mInterrupted; i++) {
127            GLOGD("BTORFCLThread::threadLoop() i = %d", i);
128            spbtorfcl->mBtServerSocket = spbtorfcl->mAdapter->listenUsingInsecureRfcommOn(spbtorfcl->mBtOppRfcommChannel);
129            if (spbtorfcl->mBtServerSocket == NULL) {
130                LOGE("Error create RfcommServerSocket");
131                serverOK = false;
132            }
133//通过调用BluetoothAdapter的listenUsingRfcommWithServiceRecord(String, UUID)方法来获取BluetoothServerSocket(UUID用于客户端与服务器端之间的配对)
//调用BluetoothServerSocket的accept()方法监听连接请求,如果收到请求,则返回一个BluetoothSocket实例(此方法为block方法,应置于新线程中)


134            if (!serverOK) {
135                {
136                    RecursiveMutex::Autolock _l(spbtorfcl->mBluetoothOppRfcommListenerMutex);
137                    GLOGI("wait 3 seconds");
138                    // TODO TODO TODO
139                    // check
140                    Thread::milliSleep(3000);
141                }
142            } else {
143                break;
144            }
145        }
146        if (!serverOK) {
147            LOGE("Error start listening after %d try", BluetoothOppRfcommListener::CREATE_RETRY_TIME);
148            spbtorfcl->mInterrupted = true;
149        }
150        if (!spbtorfcl->mInterrupted) {
151            GLOGI("Accept thread started on channel %d", spbtorfcl->mBtOppRfcommChannel);
152        }
153        sp<BluetoothSocket> clientSocket = NULL;
154        while (!spbtorfcl->mInterrupted) {
155            GLOGI("BTORFCLThread::threadLoop() while loop");
156            clientSocket = spbtorfcl->mBtServerSocket->accept();
157            if (clientSocket != NULL) {
158                sp<BluetoothDevice> btdevice = clientSocket->getRemoteDevice();
159                GLOGI("Accepted connectoin from %s", SP_TOSTRING_STR(btdevice));
160                sp<BluetoothOppRfcommTransport> transport = new BluetoothOppRfcommTransport(clientSocket);
161                sp<Message> msg = Message::obtain();
162                msg->setTarget(spbtorfcl->mCallback);
163                msg->what = BluetoothOppRfcommListener::MSG_INCOMING_BTOPP_CONNECTION;
164                msg->obj = transport;
165                msg->sendToTarget();
166            } else  {
167                LOGE("Error accept connection");
168                return false;
169            }
170        }
171        GLOGI("BluetoothSocket listen thread finished");
172    }
173
174    return false;
175}



278        case BluetoothOppRfcommListener::MSG_INCOMING_BTOPP_CONNECTION:

279        {
280            GLOGI("Get incoming connection");
281            sp<ObexTransport> transport = safe_cast<ObexTransport*>(msg->obj);
282            /*
283             * Strategy for incoming connections:
284             * 1. If there is no ongoing transfer, no on-hold connection, start it
285             * 2. If there is ongoing transfer, hold it for 20 seconds(1 seconds * 20 times)
286             * 3. If there is on-hold connection, reject directly
287             */
288            if (mspbtoppser->mBatchs->size() == 0 && mspbtoppser->mPendingConnection == NULL) {
289                GLOGI("Start Obex Server");
290                mspbtoppser->createServerSession(transport);
291            } else {
292                if (mspbtoppser->mPendingConnection != NULL) {
293                    GLOGW("OPP busy! Reject connection");
294                    // try {
295                        transport->close();
296                    // } catch(IOException e) {
297                    //    LOGE("close tranport error");
298                    // }
299                } else if (Constants::USE_TCP_DEBUG && !Constants::USE_TCP_SIMPLE_SERVER) {
300                    GLOGI("Start Obex Server in TCP DEBUG mode");
301                    mspbtoppser->createServerSession(transport);
302                } else {
303                    GLOGI("OPP busy! Retry after 1 second");
304                    mspbtoppser->mIncomingRetries = mspbtoppser->mIncomingRetries + 1;
305                    mspbtoppser->mPendingConnection = transport;
306                    sp<Message> msg1 = Message::obtain(mspbtoppser->mHandler);
307                    msg1->what = BluetoothOppService::MSG_INCOMING_CONNECTION_RETRY;
308                    mspbtoppser->mHandler->sendMessageDelayed(msg1, 1000);
309                }
310            }
311            break;
312        }
0 0
原创粉丝点击