summaryrefslogtreecommitdiff
path: root/android/sdl_android/src/main/java/com/smartdevicelink/transport/MultiplexBluetoothTransport.java
blob: 31cea21c82396a36d3319745766c2b6c00169e61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *
 * Note: This file has been modified from its original form.
 */

package com.smartdevicelink.transport;

import android.Manifest;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import androidx.annotation.RequiresPermission;

import com.smartdevicelink.protocol.SdlPacket;
import com.smartdevicelink.transport.enums.TransportType;
import com.smartdevicelink.transport.utl.TransportRecord;
import com.smartdevicelink.util.DebugTool;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.UUID;

/**
 * This class does all the work for setting up and managing Bluetooth
 * connections with other devices. It has a thread that listens for
 * incoming connections, a thread for connecting with a device, and a
 * thread for performing data transmissions when connected.
 *
 * @author Joey Grover
 */
public class MultiplexBluetoothTransport extends MultiplexBaseTransport {
    //finals
    private static final String TAG = "Bluetooth Transport";
    private static final UUID SERVER_UUID = new UUID(0x936DA01F9ABD4D9DL, 0x80C702AF85C822A8L);
    // Name for the SDP record when creating server socket
    private static final String NAME_SECURE = " SdlRouterService";
    // Key names received from the BluetoothSerialServer Handler
    private static final long MS_TILL_TIMEOUT = 2500;
    private static final int READ_BUFFER_SIZE = 4096;
    private final Object THREAD_LOCK = new Object();

    protected static final String SHARED_PREFS = "sdl.bluetoothprefs";


    // Member fields
    private final BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter();
    private AcceptThread mSecureAcceptThread;
    private ConnectThread mConnectThread;
    private ConnectedThread mConnectedThread;
    private ConnectedWriteThread mConnectedWriteThread;
    Handler timeOutHandler;
    Runnable socketRunnable;
    boolean keepSocketAlive = true;
    BluetoothDevice connectedDevice;
    
    /**
     * Constructor. Prepares a new BluetoothChat session.
     *
     * @param handler A Handler to send messages back to the UI Activity
     */
    public MultiplexBluetoothTransport(Handler handler) {
        super(handler, TransportType.BLUETOOTH);
    }

    //These methods are used so we can have a semi-static reference to the Accept Thread (Static reference inherited by housing class)
    private synchronized AcceptThread getAcceptThread() {
        return mSecureAcceptThread;
    }

    private synchronized void setAcceptThread(AcceptThread aThread) {
        mSecureAcceptThread = aThread;
    }

    protected synchronized void setStateManually(int state) {
        //Log.d(TAG, "Setting state from: " +mState + " to: " +state);
        mState = state;
    }

    /**
     * Return the current connection state.
     */
    public synchronized int getState() {
        return mState;
    }

    public void setKeepSocketAlive(boolean keepSocketAlive) {
        this.keepSocketAlive = keepSocketAlive;
    }

    /**
     * A method to retrieve the currently connected bluetooth device
     * @return the connected bluetooth device if connected, null otherwise.
     */
    public BluetoothDevice getConnectedDevice(){
        return connectedDevice;
    }

    /**
     * Start the chat service. Specifically start AcceptThread to begin a
     * session in listening (server) mode. Called by the Activity onResume()
     */
    @SuppressLint("MissingPermission")
    @RequiresPermission(Manifest.permission.BLUETOOTH)
    public synchronized void start() {
        //Log.d(TAG, "Starting up Bluetooth Server to Listen");
        // Cancel any thread attempting to make a connection
        if (mConnectThread != null) {
            mConnectThread.cancel();
            mConnectThread = null;
        }

        // Cancel any thread currently running a connection
        if (mConnectedThread != null) {
            mConnectedThread.cancel();
            mConnectedThread = null;
        }
        if (mConnectedWriteThread != null) {
            mConnectedWriteThread.cancel();
            mConnectedWriteThread = null;
        }


        // Start the thread to listen on a BluetoothServerSocket
        if (getAcceptThread() == null
                && mAdapter != null
                && mAdapter.isEnabled()) {
            //Log.d(TAG, "Secure thread was null, attempting to create new");
            setAcceptThread(new AcceptThread(true));
            if (getAcceptThread() != null) {
                setState(STATE_LISTEN);
                getAcceptThread().start();
            }
        }
    }

    /**
     * Start the ConnectThread to initiate a connection to a remote device.
     *
     * @param device The BluetoothDevice to connect
     */
    public synchronized void connect(BluetoothDevice device) {
        // Cancel any thread attempting to make a connection
        if (mState == STATE_CONNECTING) {
            if (mConnectThread != null) {
                mConnectThread.cancel();
                mConnectThread = null;
            }
        }

        // Cancel any thread currently running a connection
        if (mConnectedThread != null) {
            mConnectedThread.cancel();
            mConnectedThread = null;
        }
        if (mConnectedWriteThread != null) {
            mConnectedWriteThread.cancel();
            mConnectedWriteThread = null;
        }


        // Cancel the accept thread because we only want to connect to one device
        if (mSecureAcceptThread != null) {
            mSecureAcceptThread.cancel();
            mSecureAcceptThread = null;
        }

        // Start the thread to connect with the given device
        mConnectThread = new ConnectThread(device);
        mConnectThread.start();
        setState(STATE_CONNECTING);
    }

    /**
     * Start the ConnectedThread to begin managing a Bluetooth connection
     *
     * @param socket The BluetoothSocket on which the connection was made
     * @param device The BluetoothDevice that has been connected
     */
    @RequiresPermission(Manifest.permission.BLUETOOTH)
    public synchronized void connected(BluetoothSocket socket, BluetoothDevice device) {
        // Cancel the thread that completed the connection
        if (mConnectThread != null) {
            mConnectThread.cancel();
            mConnectThread = null;
        }

        // Cancel any thread currently running a connection
        if (mConnectedThread != null) {
            mConnectedThread.cancel();
            mConnectedThread = null;
        }
        if (mConnectedWriteThread != null) {
            mConnectedWriteThread.cancel();
            mConnectedWriteThread = null;
        }
        // Cancel the accept thread because we only want to connect to one device
        if (!keepSocketAlive && mSecureAcceptThread != null) {
            mSecureAcceptThread.cancel();
            mSecureAcceptThread = null;
        }

        // Start the thread to manage the connection and perform transmissions
        mConnectedThread = new ConnectedThread(socket);
        mConnectedThread.start();
        // Start the thread to manage the connection and perform transmissions
        mConnectedWriteThread = new ConnectedWriteThread(socket);
        mConnectedWriteThread.start();

        //Store a static name of the device that is connected.
        if (device != null) {
            connectedDevice = device;
            connectedDeviceName = device.getName();
            connectedDeviceAddress = device.getAddress();
            if (connectedDeviceAddress != null) {
                //Update the transport record with the address
                transportRecord = new TransportRecord(transportType, connectedDeviceAddress);
            }
        }

        // Send the name of the connected device back to the UI Activity
        Message msg = handler.obtainMessage(SdlRouterService.MESSAGE_DEVICE_NAME);
        Bundle bundle = new Bundle();
        if (connectedDeviceName != null) {
            bundle.putString(DEVICE_NAME, connectedDeviceName);
            bundle.putString(DEVICE_ADDRESS, connectedDeviceAddress);
        }
        msg.setData(bundle);
        handler.sendMessage(msg);
        setState(STATE_CONNECTED);
    }

    /**
     * Stop all threads
     */
    public synchronized void stop() {
        stop(STATE_NONE, REASON_NONE);
    }

    protected synchronized void stop(int stateToTransitionTo) {
        this.stop(stateToTransitionTo, REASON_NONE);
    }

    @Override
    protected synchronized void stop(int stateToTransitionTo, byte error) {
        super.stop(stateToTransitionTo, error);
        DebugTool.logInfo(TAG, "Attempting to close the bluetooth serial server");
        if (mConnectThread != null) {
            mConnectThread.cancel();
            mConnectThread = null;
        }

        if (mConnectedThread != null) {
            mConnectedThread.cancel();
            mConnectedThread = null;
        }
        if (mConnectedWriteThread != null) {
            mConnectedWriteThread.cancel();
            mConnectedWriteThread = null;
        }

        if (mSecureAcceptThread != null) {
            mSecureAcceptThread.cancel();
            mSecureAcceptThread = null;
        }

        if (stateToTransitionTo == MultiplexBaseTransport.STATE_ERROR) {
            Bundle bundle = new Bundle();
            bundle.putByte(ERROR_REASON_KEY, error);
            setState(stateToTransitionTo, bundle);
        } else {
            setState(stateToTransitionTo, null);
        }
    }


    /**
     * Write to the ConnectedThread in an unsynchronized manner
     *
     * @param out The bytes to write
     * @see ConnectedWriteThread#write(byte[], int, int)
     */
    public void write(byte[] out, int offset, int count) {
        // Create temporary object
        ConnectedWriteThread r;
        // Synchronize a copy of the ConnectedThread
        synchronized (this) {
            if (mState != STATE_CONNECTED) return;
            r = mConnectedWriteThread;
            //r.write(out,offset,count);
        }
        // Perform the write unsynchronized
        r.write(out, offset, count);
    }

    /**
     * Indicate that the connection attempt failed and notify the UI Activity.
     */
    private void connectionFailed() {
        // Send a failure message back to the Activity
        Message msg = handler.obtainMessage(SdlRouterService.MESSAGE_LOG);
        Bundle bundle = new Bundle();
        bundle.putString(LOG, "Unable to connect device");
        msg.setData(bundle);
        handler.sendMessage(msg);

        // Start the service over to restart listening mode
        // BluetoothSerialServer.this.start();
    }

    /**
     * Indicate that the connection was lost and notify the UI Activity.
     */
    private void connectionLost() {
        // Send a failure message back to the Activity
        Message msg = handler.obtainMessage(SdlRouterService.MESSAGE_LOG);
        Bundle bundle = new Bundle();
        bundle.putString(LOG, "Device connection was lost");
        msg.setData(bundle);
        handler.sendMessage(msg);
        stop();

    }

    private void timerDelayRemoveDialog(final BluetoothSocket sock) {
        if (Looper.myLooper() == null) {
            Looper.prepare();
        }
        timeOutHandler = new Handler(Looper.myLooper());
        socketRunnable = new Runnable() {
            public void run() {
                //Log.e(TAG, "BLUETOOTH SOCKET CONNECT TIMEOUT - ATTEMPT TO CLOSE SOCKET");
                try {
                    sock.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };
        timeOutHandler.postDelayed(socketRunnable, MS_TILL_TIMEOUT);
    }


    /**
     * This thread runs while listening for incoming connections. It behaves
     * like a server-side client. It runs until a connection is accepted
     * (or until cancelled).
     */
    private class AcceptThread extends Thread {
        // The local server socket
        private final String mSocketType;
        final BluetoothServerSocket mmServerSocket;

        @SuppressLint("NewApi")
        @RequiresPermission(Manifest.permission.BLUETOOTH)
        public AcceptThread(boolean secure) {
            synchronized (THREAD_LOCK) {
                //Log.d(TAG, "Creating an Accept Thread");
                BluetoothServerSocket tmp = null;
                mSocketType = secure ? "Secure" : "Insecure";
                // Create a new listening server socket
                try {
                    if (secure) {
                        tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE, SERVER_UUID);
                    }
                } catch (IOException e) {
                    //Log.e(TAG, "Socket Type: " + mSocketType + "listen() failed", e);
                    MultiplexBluetoothTransport.this.stop(STATE_ERROR, REASON_SPP_ERROR);
                    //Let's try to shut down this thread
                } catch (SecurityException e2) {
                    //Log.e(TAG, "<LIVIO> Security Exception in Accept Thread - "+e2.toString());
                    interrupt();
                }
                mmServerSocket = tmp;
                //Should only log on debug
                //BluetoothSocket mySock = getBTSocket(mmServerSocket);
                //Log.d(TAG, "Accepting Connections on SDP Server Port Number: " + getChannel(mySock) + "\r\n");
            }
        }

        @RequiresPermission(Manifest.permission.BLUETOOTH)
        public void run() {
            synchronized (THREAD_LOCK) {
                DebugTool.logInfo(TAG, "Socket Type: " + mSocketType +
                        " BEGIN mAcceptThread" + this);
                setName("AcceptThread" + mSocketType);

                BluetoothSocket socket;
                int listenAttempts = 0;

                // Listen to the server socket if we're not connected
                while (mState != STATE_CONNECTED) {
                    try {
                        if (listenAttempts >= 5) {
                            DebugTool.logError(TAG, "Complete failure in attempting to listen for Bluetooth connection, erroring out.");
                            MultiplexBluetoothTransport.this.stop(STATE_ERROR, REASON_NONE);
                            return;
                        }
                        listenAttempts++;
                        DebugTool.logInfo(TAG, "SDL Bluetooth Accept thread is running.");

                        // This is a blocking call and will only return on a
                        // successful connection or an exception
                        if (mmServerSocket != null) {

                            socket = mmServerSocket.accept();

                        } else {
                            DebugTool.logError(TAG, "Listening Socket was null, stopping the bluetooth serial server.");
                            MultiplexBluetoothTransport.this.stop(STATE_ERROR, REASON_NONE);
                            return;
                        }
                    } catch (IOException e) {
                        DebugTool.logError(TAG, "Socket Type: " + mSocketType + "accept() failed");
                        MultiplexBluetoothTransport.this.stop(STATE_ERROR, REASON_SPP_ERROR);
                        return;
                    }

                    // If a connection was accepted
                    if (socket != null) {
                        synchronized (MultiplexBluetoothTransport.this) {
                            switch (mState) {
                                case STATE_LISTEN:
                                case STATE_CONNECTING:
                                    // Situation normal. Start the connected thread.
                                    connected(socket, socket.getRemoteDevice());

                                    break;
                                case STATE_NONE:
                                case STATE_CONNECTED:
                                    // Either not ready or already connected. Terminate new socket.
                                    try {
                                        DebugTool.logInfo(TAG, "Close unwanted socket");
                                        socket.close();
                                    } catch (IOException e) {
                                        DebugTool.logError(TAG, "Could not close unwanted socket", e);
                                    }
                                    break;
                            }
                        }
                    }
                }
                DebugTool.logInfo(TAG, mState + " END mAcceptThread, socket Type: " + mSocketType);
            }
        }

        public synchronized void cancel() {
            DebugTool.logInfo(TAG, mState + " Socket Type " + mSocketType + " cancel ");
            try {
                if (mmServerSocket != null) {
                    mmServerSocket.close();
                }

            } catch (IOException e) {
                DebugTool.logError(TAG, mState + " Socket Type " + mSocketType + " close() of server failed " + Arrays.toString(e.getStackTrace()));
            }
        }
    }


    /**
     * This thread runs while attempting to make an outgoing connection
     * with a device. It runs straight through; the connection either
     * succeeds or fails.
     */
    private class ConnectThread extends Thread {
        private BluetoothSocket mmSocket;
        private final BluetoothDevice mmDevice;

        public ConnectThread(BluetoothDevice device) {
            mmDevice = device;
            //Log.d(TAG, "Attempting to connect to " + device.getName());
            //Log.d(TAG, "UUID to connect to " + SERVER_UUID.toString());

        }

        @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN)
        public void attemptCancelDiscovery() {
            try {
                mAdapter.cancelDiscovery();
            } catch (SecurityException e2) {
                DebugTool.logError(TAG, "Don't have required permission to cancel discovery. Moving on");
            }
        }

        @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN)
        public void run() {
            setName("ConnectThread");
            // Always cancel discovery because it will slow down a connection
            attemptCancelDiscovery();
            // Make a connection to the BluetoothSocket
            int attemptCount = 0;
            boolean success = false;
            Looper.prepare();

            while (attemptCount < 5) {
                //Looper.loop()
                attemptCount++;
                try {
                    // This is a blocking call and will only return on a
                    // successful connection or an exception
                    long waitTime = 3000;
                    try {
                        Thread.sleep(waitTime);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //This sequence tries to use reflection first to connect with a certain number of phones that require this
                    //Next is the most common methods for phones
                    //Finally if both have failed an insecure connection is attempted, though this is not available on lower SDK's
                    boolean tryInsecure = false;
                    boolean trySecure = false;
                    //Log.i(TAG,mmDevice.getName() + " socket connecting...");

                    try {
                        Method m = mmDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class});
                        //Log.i(TAG,"connecting using createRfcommSocket");
                        mmSocket = (BluetoothSocket) m.invoke(mmDevice, 1);
                        if (mmSocket != null) {
                            //Looper.prepare();
                            timerDelayRemoveDialog(mmSocket);
                            //Looper.loop();
                            mmSocket.connect();
                            timeOutHandler.removeCallbacks(socketRunnable);
                            if (Looper.myLooper() != null) {
                                Looper.myLooper().quit();
                            }
                            success = true;
                            break;
                        } else {
                            trySecure = true;
                        }

                    } catch (Exception e) {
                        //Log.e(TAG,"createRfcommSocket exception - " + e.toString());
                        trySecure = true;
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e2) {
                            e2.printStackTrace();
                        }
                    }
                    if (trySecure) {
                        try {
                            //Log.i(TAG, "connecting using createRfcommSocketToServiceRecord ");
                            mmSocket = mmDevice.createRfcommSocketToServiceRecord(SERVER_UUID);
                            if (mmSocket != null) {
                                //Looper.prepare();
                                timerDelayRemoveDialog(mmSocket);
                                //Looper.loop();
                                mmSocket.connect();
                                timeOutHandler.removeCallbacks(socketRunnable);
                                if (Looper.myLooper() != null) {
                                    Looper.myLooper().quit();
                                }
                                success = true;
                                break;
                            } else {
                                tryInsecure = true;
                            }
                        } catch (IOException io) {
                            tryInsecure = true;
                            DebugTool.logError(TAG, "createRfcommSocketToServiceRecord exception - " + io.toString());
                        } catch (Exception e) {
                            DebugTool.logError(TAG, "createRfcommSocketToServiceRecord exception - " + e.toString());
                        }
                    } else {
                        tryInsecure = true;
                    }

                    if (tryInsecure) {
                        // try again using insecure comm if available
                        try {
                            //Log.i(TAG,"connecting using createInsecureRfcommSocketToServiceRecord");
                            Method m = mmDevice.getClass().getMethod("createInsecureRfcommSocketToServiceRecord", new Class[]{UUID.class});
                            mmSocket = (BluetoothSocket) m.invoke(mmDevice, new Object[]{SERVER_UUID});
                            //Looper.prepare();
                            timerDelayRemoveDialog(mmSocket);
                            //Looper.loop();
                            mmSocket.connect();
                            timeOutHandler.removeCallbacks(socketRunnable);
                            if (Looper.myLooper() != null) {
                                Looper.myLooper().quit();
                            }
                            success = true;
                            tryInsecure = false;
                            break;
                        } catch (NoSuchMethodException ie) {
                        } catch (IllegalAccessException ie) {
                        } catch (InvocationTargetException ie) {
                        }
                    }
                    if (tryInsecure) {
                        // try again using insecure comm if available
                        try {
                            //Log.i(TAG,"connecting using createInsecureRfcommSocket()");
                            Method m = mmDevice.getClass().getMethod("createInsecureRfcommSocket()", new Class[]{UUID.class});
                            mmSocket = (BluetoothSocket) m.invoke(mmDevice, new Object[]{SERVER_UUID});
                            //Looper.prepare();
                            timerDelayRemoveDialog(mmSocket);
                            //Looper.loop();
                            mmSocket.connect();
                            timeOutHandler.removeCallbacks(socketRunnable);
                            if (Looper.myLooper() != null) {
                                Looper.myLooper().quit();
                            }
                            success = true;
                            break;
                        } catch (NoSuchMethodException ie) {
                        } catch (IllegalAccessException ie) {
                        } catch (InvocationTargetException ie) {
                        }
                    }
                } catch (IOException e) {
                    connectionFailed();
                    DebugTool.logError(TAG, e.getClass().getSimpleName()
                            + " caught connecting to the bluetooth socket: "
                            + e.toString());
                    try {
                        mmSocket.close();
                    } catch (IOException e2) {
                        DebugTool.logError(TAG, "unable to close() socket during connection failure" + e2);
                    }
                    return;
                }
            }
            // Reset the ConnectThread because we're done
            if (success) {
                synchronized (MultiplexBluetoothTransport.this) {
                    mConnectThread = null;
                }


                // Start the connected thread

                connected(mmSocket, mmDevice);
            } else {
                DebugTool.logError(TAG, "There was a problem opening up RFCOMM");
            }
        }

        public void cancel() {
            try {
                DebugTool.logInfo(TAG, "Calling Cancel in the connect thread");
                mmSocket.close();
            } catch (IOException e) {
                // close() of connect socket failed
            } catch (NullPointerException e) {
                //mSocket was pry never initialized
            }
        }
    }

    /**
     * This thread runs during a connection with a remote device.
     * It handles all incoming and outgoing transmissions.
     */
    private class ConnectedWriteThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final OutputStream mmOutStream;


        public ConnectedWriteThread(BluetoothSocket socket) {
            //Log.d(TAG, "Creating a Connected - Write Thread");
            mmSocket = socket;
            OutputStream tmpOut = null;
            setName("SDL Router BT Write Thread");
            // Get the BluetoothSocket input and output streams
            try {
                tmpOut = socket.getOutputStream();
            } catch (IOException e) {
                // temp sockets not created
                DebugTool.logError(TAG, "Connected Write Thread: " + e.getMessage());
            }
            mmOutStream = tmpOut;


        }

        /**
         * Write to the connected OutStream.
         *
         * @param buffer The bytes to write
         */
        public void write(byte[] buffer, int offset, int count) {
            try {
                if (buffer == null) {
                    DebugTool.logWarning(TAG, "Can't write to device, nothing to send");
                    return;
                }
                //This would be a good spot to log out all bytes received
                mmOutStream.write(buffer, offset, count);
                //Log.w(TAG, "Wrote out to device: bytes = "+ count);
            } catch (IOException | NullPointerException e) { // STRICTLY to catch mmOutStream NPE
                // Exception during write
                //OMG! WE MUST NOT BE CONNECTED ANYMORE! LET THE USER KNOW
                DebugTool.logError(TAG, "Error sending bytes to connected device!");
                connectionLost();
            }
        }

        public synchronized void cancel() {
            try {
                if (mmOutStream != null) {
                    mmOutStream.flush();
                    mmOutStream.close();

                }
                if (mmSocket != null) {
                    mmSocket.close();
                }
            } catch (IOException e) {
                // close() of connect socket failed
                DebugTool.logInfo(TAG, "Write Thread: " + e.getMessage());
            }
        }
    }

    private class ConnectedThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final InputStream mmInStream;
        final SdlPsm psm;

        public ConnectedThread(BluetoothSocket socket) {
            this.psm = new SdlPsm();
            //Log.d(TAG, "Creating a Connected - Read Thread");
            mmSocket = socket;
            InputStream tmpIn = null;
            setName("SDL Router BT Read Thread");
            // Get the BluetoothSocket input and output streams
            try {
                tmpIn = socket.getInputStream();
            } catch (IOException e) {
                // temp sockets not created
                DebugTool.logError(TAG, "Connected Read Thread: " + e.getMessage());
            }
            mmInStream = tmpIn;

        }

        @SuppressLint("NewApi")
        public void run() {
            DebugTool.logInfo(TAG, "Running the Connected Thread");
            byte input = 0;
            int bytesRead = 0;
            byte[] buffer = new byte[READ_BUFFER_SIZE];
            // Keep listening to the InputStream while connected
            boolean stateProgress;

            psm.reset();

            while (true) {
                try {
                    bytesRead = mmInStream.read(buffer);
                    // Log.i(getClass().getName(), "Received " + bytesRead + " bytes from Bluetooth");
                    for (int i = 0; i < bytesRead; i++) {
                        input = buffer[i];

                        // Send the response of what we received
                        stateProgress = psm.handleByte(input);
                        if (!stateProgress) { //We are trying to weed through the bad packet info until we get something
                            //Log.w(TAG, "Packet State Machine did not move forward from state - "+ psm.getState()+". PSM being Reset.");
                            psm.reset();
                            continue;
                        }

                        if (psm.getState() == SdlPsm.FINISHED_STATE) {
                            //Log.d(TAG, "Packet formed, sending off");
                            SdlPacket packet = psm.getFormedPacket();
                            packet.setTransportRecord(getTransportRecord());
                            handler.obtainMessage(SdlRouterService.MESSAGE_READ, packet).sendToTarget();
                            psm.reset();
                        }
                    }
                } catch (IOException | NullPointerException e) { // NPE is ONLY to catch error on mmInStream
                    DebugTool.logError(TAG, "Lost connection in the Connected Thread");
                    if (DebugTool.isDebugEnabled()) {
                        e.printStackTrace();
                    }
                    connectionLost();
                    break;
                }
            }
        }


        public synchronized void cancel() {
            try {
                //Log.d(TAG, "Calling Cancel in the Read thread");
                if (mmInStream != null) {
                    mmInStream.close();
                }
                if (mmSocket != null) {
                    mmSocket.close();
                }

            } catch (IOException | NullPointerException e) { // NPE is ONLY to catch error on mmInStream
                // Log.trace(TAG, "Read Thread: " + e.getMessage());
                // Socket or stream is already closed
            }
        }
    }


    public BluetoothSocket getBTSocket(BluetoothServerSocket bsSocket) {
        if (bsSocket == null) {
            return null;
        }
        Field[] f = bsSocket.getClass().getDeclaredFields();

        //int channel = -1;
        BluetoothSocket mySocket;
        for (Field field : f) {
            if (field.getName().equals("mSocket")) {
                field.setAccessible(true);
                try {

                    mySocket = (BluetoothSocket) field.get(bsSocket);
                    return mySocket;
                    //channel = field.getInt(bsSocket);
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                field.setAccessible(false);
            }
        }

        return null;
    }

    public int getChannel(BluetoothSocket bsSocket) {

        int channel = -1;
        if (bsSocket == null) {
            return channel;
        }

        Field[] f = bsSocket.getClass().getDeclaredFields();

        for (Field field : f) {
            if (field.getName().equals("mPort")) {
                field.setAccessible(true);
                try {


                    channel = field.getInt(bsSocket);
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                field.setAccessible(false);
            }
        }

        return channel;
    }

}