Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.35 KB | None | 0 0
  1. private class ConnectThread extends Thread {
  2. private final BluetoothSocket mmSocket;
  3. private final BluetoothDevice mmDevice;
  4. private String mSocketType;
  5.  
  6. public ConnectThread(BluetoothDevice device, boolean secure) {
  7. mmDevice = device;
  8. BluetoothSocket tmp = null;
  9. mSocketType = secure ? "Secure" : "Insecure";
  10. // Get a BluetoothSocket for a connection with the
  11. // given BluetoothDevice
  12. try {
  13. if (secure) {
  14. tmp = device.createRfcommSocketToServiceRecord(
  15. MY_UUID_SECURE);
  16. } else {
  17. tmp = device.createInsecureRfcommSocketToServiceRecord(MY_UUID_INSECURE);
  18. }
  19. } catch (IOException e) {
  20. Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
  21. }
  22. mmSocket = tmp;
  23. mState = STATE_CONNECTING;
  24. }
  25.  
  26. public void run() {
  27. Log.i(TAG, "BEGIN mConnectThread SocketType:" + mSocketType);
  28. setName("ConnectThread" + mSocketType);
  29. // Always cancel discovery because it will slow down a connection
  30. mAdapter.cancelDiscovery();
  31. // Make a connection to the BluetoothSocket
  32. try {
  33. // This is a blocking call and will only return on a
  34. // successful connection or an exception
  35. mmSocket.connect();
  36. } catch (IOException e) {
  37. // Close the socket
  38. try {
  39. mmSocket.close();
  40. } catch (IOException e2) {
  41. Log.e(TAG, "unable to close() " + mSocketType +
  42. " socket during connection failure", e2);
  43. }
  44. connectionFailed();
  45. return;
  46. }
  47. // Reset the ConnectThread because we're done
  48. synchronized (BluetoothChatService.this) {
  49. mConnectThread = null;
  50. }
  51. // Start the connected thread
  52. connected(mmSocket, mmDevice, mSocketType);
  53. }
  54.  
  55. public void cancel() {
  56. try {
  57. mmSocket.close();
  58. } catch (IOException e) {
  59. Log.e(TAG, "close() of connect " + mSocketType + " socket failed", e);
  60. }
  61. }
  62. }
  63.  
  64. public synchronized void connected(BluetoothSocket socket, BluetoothDevice
  65. device, final String socketType) {
  66. Log.d(TAG, "connected, Socket Type:" + socketType);
  67. // Cancel the thread that completed the connection
  68. if (mConnectThread != null) {
  69. mConnectThread.cancel();
  70. mConnectThread = null;
  71. }// Cancel any thread currently running a connection
  72. if (mConnectedThread != null) {
  73. mConnectedThread.cancel();
  74. mConnectedThread = null;
  75. } // Cancel the accept thread because we only want to connect to one device
  76. if (mSecureAcceptThread != null) {
  77. mSecureAcceptThread.cancel();
  78. mSecureAcceptThread = null;
  79. }
  80. if (mInsecureAcceptThread != null) {
  81. mInsecureAcceptThread.cancel();
  82. mInsecureAcceptThread = null;
  83. }
  84.  
  85.  
  86.  
  87. // Start the thread to manage the connection and perform transmissions
  88.  
  89. mConnectedThread = new ConnectedThread(socket, socketType);
  90.  
  91. mConnectedThread.start();
  92.  
  93.  
  94.  
  95. // Send the name of the connected device back to the UI Activity
  96.  
  97. Message msg = mHandler.obtainMessage(Constants.MESSAGE_DEVICE_NAME);
  98.  
  99. Bundle bundle = new Bundle();
  100.  
  101. bundle.putString(Constants.DEVICE_NAME, device.getName());
  102.  
  103. msg.setData(bundle);
  104.  
  105. mHandler.sendMessage(msg);
  106.  
  107. // Update UI title
  108.  
  109. updateUserInterfaceTitle();
  110.  
  111. }
  112.  
  113. /**
  114.  
  115. * This thread runs during a connection with a remote device.
  116.  
  117. * It handles all incoming and outgoing transmissions.
  118.  
  119. */
  120.  
  121. private class ConnectedThread extends Thread {
  122.  
  123. private final BluetoothSocket mmSocket;
  124.  
  125. private final InputStream mmInStream;
  126.  
  127. private final OutputStream mmOutStream;
  128.  
  129.  
  130.  
  131. public ConnectedThread(BluetoothSocket socket, String socketType) {
  132.  
  133. Log.d(TAG, "create ConnectedThread: " + socketType);
  134.  
  135. mmSocket = socket;
  136.  
  137. InputStream tmpIn = null;
  138.  
  139. OutputStream tmpOut = null;
  140.  
  141.  
  142.  
  143. // Get the BluetoothSocket input and output streams
  144.  
  145. try {
  146.  
  147. tmpIn = socket.getInputStream();
  148.  
  149. tmpOut = socket.getOutputStream();
  150.  
  151. } catch (IOException e) {
  152.  
  153. Log.e(TAG, "temp sockets not created", e);
  154.  
  155. }
  156.  
  157.  
  158.  
  159. mmInStream = tmpIn;
  160.  
  161. mmOutStream = tmpOut;
  162.  
  163. mState = STATE_CONNECTED;
  164.  
  165. }
  166.  
  167.  
  168.  
  169. public void run() {
  170.  
  171. Log.i(TAG, "BEGIN mConnectedThread");
  172.  
  173. byte[] buffer = new byte[1024];
  174.  
  175. int bytes;
  176.  
  177.  
  178.  
  179. // Keep listening to the InputStream while connected
  180.  
  181. while (mState == STATE_CONNECTED) {
  182.  
  183. try {
  184.  
  185. // Read from the InputStream
  186.  
  187. bytes = mmInStream.read(buffer);
  188.  
  189.  
  190.  
  191. // Send the obtained bytes to the UI Activity
  192.  
  193. mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer)
  194.  
  195. .sendToTarget();
  196.  
  197. } catch (IOException e) {
  198.  
  199. Log.e(TAG, "disconnected", e);
  200.  
  201. connectionLost();
  202.  
  203. break;
  204.  
  205. }
  206.  
  207. }
  208.  
  209. }
  210.  
  211.  
  212.  
  213. /**
  214.  
  215. * Write to the connected OutStream.
  216.  
  217. *
  218.  
  219. * @param buffer The bytes to write
  220.  
  221. */
  222.  
  223. public void write(byte[] buffer) {
  224.  
  225. try {
  226.  
  227. mmOutStream.write(buffer);
  228.  
  229.  
  230.  
  231. // Share the sent message back to the UI Activity
  232.  
  233. mHandler.obtainMessage(Constants.MESSAGE_WRITE, -1, -1, buffer)
  234.  
  235. .sendToTarget();
  236.  
  237. } catch (IOException e) {
  238.  
  239. Log.e(TAG, "Exception during write", e);
  240.  
  241. }
  242.  
  243. }
  244.  
  245.  
  246.  
  247. public void cancel() {
  248.  
  249. try {
  250.  
  251. mmSocket.close();
  252.  
  253. } catch (IOException e) {
  254.  
  255. Log.e(TAG, "close() of connect socket failed", e);
  256.  
  257. }
  258.  
  259. }
  260.  
  261. }
  262.  
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement