Advertisement
Guest User

Untitled

a guest
Mar 17th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. package com.example.kkipn.bluetoothchatfinal;
  2.  
  3. /**
  4. * Created by kkipn on 17/03/2018.
  5. */
  6.  
  7. import android.bluetooth.BluetoothDevice;
  8. import android.bluetooth.BluetoothSocket;
  9. import android.content.Context;
  10. import android.content.Intent;
  11. import android.util.Log;
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. import java.io.OutputStream;
  15.  
  16. public class MyConnectedThread
  17. extends Thread
  18. {
  19. String TAG = "ConnectedThread";
  20. Context mContext;
  21. private final InputStream mmInStream;
  22. private final OutputStream mmOutStream;
  23. private final BluetoothSocket mmSocket;
  24. BluetoothDevice remoteDevice;
  25.  
  26. public MyConnectedThread(BluetoothSocket paramBluetoothSocket, BluetoothDevice paramBluetoothDevice,
  27. Context paramContext)
  28. {
  29. Log.d(this.TAG, "create ConnectedThread");
  30. this.mmSocket = paramBluetoothSocket;
  31. this.remoteDevice = paramBluetoothDevice;
  32. this.mContext = paramContext;
  33. InputStream localInputStream = null;
  34.  
  35. //added declaration
  36. OutputStream localOutputStream1 =null;
  37. try
  38. {
  39. localInputStream = paramBluetoothSocket.getInputStream();
  40. OutputStream localOutputStream2 = paramBluetoothSocket.getOutputStream();
  41. localOutputStream1 = localOutputStream2;
  42. }
  43. catch (IOException localIOException)
  44. {
  45. for (;;)
  46. {
  47. Log.e(this.TAG, "temp sockets not created", localIOException);
  48. //OutputStream localOutputStream1 = null;
  49. }
  50. }
  51. this.mmInStream = localInputStream;
  52. this.mmOutStream = localOutputStream1;
  53. }
  54.  
  55. public void cancel()
  56. {
  57. try
  58. {
  59. this.mmSocket.close();
  60. return;
  61. }
  62. catch (IOException localIOException)
  63. {
  64. Log.e(this.TAG, "close() of connect socket failed", localIOException);
  65. }
  66. }
  67.  
  68. public void run()
  69. {
  70. Log.i(this.TAG, "BEGIN mConnectedThread");
  71. byte[] arrayOfByte = new byte[2048];
  72. try
  73. {
  74. for (;;)
  75. {
  76. String str = new String(arrayOfByte, 0, this.mmInStream.read(arrayOfByte));
  77. MyMessageBean localMyMessageBean = new MyMessageBean();
  78. localMyMessageBean.setContent(str);
  79. localMyMessageBean.setInMesage(true);
  80. localMyMessageBean.setReceiverName("");
  81. localMyMessageBean.setSenderAddr(this.remoteDevice.getAddress());
  82. Intent localIntent2 = new Intent("com.example.kkipn.bluetoothchatfinal.MESSAGE_READ");
  83. localIntent2.putExtra("value", localMyMessageBean);
  84. this.mContext.sendBroadcast(localIntent2);
  85. }
  86.  
  87. return;
  88. }
  89. catch (IOException localIOException)
  90. {
  91. Log.e(this.TAG, "disconnected", localIOException);
  92. Intent localIntent1 = new Intent("liu.niu.soft.bluetoothchatsingle.ACTION_CONNECT_LOST");
  93. localIntent1.putExtra("value", this.remoteDevice.getAddress());
  94. this.mContext.sendBroadcast(localIntent1);
  95. }
  96. }
  97.  
  98. public void write(byte[] paramArrayOfByte)
  99. {
  100. try
  101. {
  102. this.mmOutStream.write(paramArrayOfByte);
  103. String str = new String(paramArrayOfByte, 0, paramArrayOfByte.length);
  104. Log.d("ConnectedThread write:", str);
  105. MyMessageBean localMyMessageBean = new MyMessageBean();
  106. localMyMessageBean.setContent(str);
  107. localMyMessageBean.setInMesage(false);
  108. localMyMessageBean.setReceiverName(this.remoteDevice.getAddress());
  109. localMyMessageBean.setSenderAddr("");
  110. Intent localIntent = new Intent("liu.niu.soft.bluetoothchatsingle.MESSAGE_WRITE");
  111. localIntent.putExtra("value", localMyMessageBean);
  112. this.mContext.sendBroadcast(localIntent);
  113. return;
  114. }
  115. catch (IOException localIOException)
  116. {
  117. Log.e(this.TAG, "Exception during write", localIOException);
  118. }
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement