Guest User

Untitled

a guest
Jan 17th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.52 KB | None | 0 0
  1. public class BluetoothService {
  2.  
  3. private static final UUID MY_UUID =
  4. UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");
  5. private static final String NAME = "Guess the Movie";
  6. private BluetoothAdapter mBluetoothAdapter;
  7. private Context context;
  8.  
  9. private AcceptThread mAcceptThread;
  10. private ConnectThread mConnectThread;
  11. private ConnectedThread mConnectedThread;
  12.  
  13.  
  14. public BluetoothService(Context context) {
  15. mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  16. this.context = context;
  17. }
  18.  
  19. private void manageConnectedSocket(BluetoothSocket socket, BluetoothDevice device) {
  20. // TODO: 25/06/2017
  21. Log.d(TAG, "socket received" + socket);
  22. connected(socket, device);
  23. }
  24.  
  25. public synchronized void connect(BluetoothDevice device) {
  26. Log.d(TAG, "connect to: " + device);
  27.  
  28. // Cancel any thread attempting to make a connection
  29. if (mConnectThread != null) {
  30. mConnectThread.cancel();
  31. mConnectThread = null;
  32. }
  33.  
  34. // Cancel any thread currently running a connection
  35. if (mConnectedThread != null) {
  36. mConnectedThread.cancel();
  37. mConnectedThread = null;
  38. }
  39.  
  40. // Start the thread to connect with the given device
  41. mConnectThread = new ConnectThread(device);
  42. mConnectThread.start();
  43. // Update UI title
  44. //updateUserInterfaceTitle();
  45. }
  46.  
  47. public synchronized void start() {
  48. Log.d(TAG, "start");
  49.  
  50. // Cancel any thread attempting to make a connection
  51. if (mConnectThread != null) {
  52. mConnectThread.cancel();
  53. mConnectThread = null;
  54. }
  55.  
  56. // Cancel any thread currently running a connection
  57. if (mConnectedThread != null) {
  58. mConnectedThread.cancel();
  59. mConnectedThread = null;
  60. }
  61.  
  62. // Start the thread to listen on a BluetoothServerSocket
  63. if (mAcceptThread == null) {
  64. mAcceptThread = new AcceptThread();
  65. mAcceptThread.start();
  66. }
  67. // Update UI title
  68. //updateUserInterfaceTitle();
  69. }
  70.  
  71. public synchronized void connected(BluetoothSocket socket, BluetoothDevice
  72. device) {
  73. Log.d(TAG, "connected() called");
  74.  
  75. // Cancel the thread that completed the connection
  76. if (mConnectThread != null) {
  77. mConnectThread.cancel();
  78. mConnectThread = null;
  79. }
  80.  
  81. // Cancel any thread currently running a connection
  82. if (mConnectedThread != null) {
  83. mConnectedThread.cancel();
  84. mConnectedThread = null;
  85. }
  86.  
  87. // Cancel the accept thread because we only want to connect to one device
  88. if (mAcceptThread != null) {
  89. mAcceptThread.cancel();
  90. mAcceptThread = null;
  91. }
  92.  
  93.  
  94. // Start the thread to manage the connection and perform transmissions
  95. mConnectedThread = new ConnectedThread(socket);
  96. mConnectedThread.start();
  97.  
  98. Log.d(TAG, "connected() finished");
  99.  
  100. // Send the name of the connected device back to the UI Activity
  101. /*
  102. Message msg = mHandler.obtainMessage(Constants.MESSAGE_DEVICE_NAME);
  103. Bundle bundle = new Bundle();
  104. bundle.putString(Constants.DEVICE_NAME, device.getName());
  105. msg.setData(bundle);
  106. mHandler.sendMessage(msg);
  107. // Update UI title
  108. updateUserInterfaceTitle();
  109. */
  110. }
  111.  
  112. public void write(byte[] out) {
  113. // Create temporary object
  114. ConnectedThread r;
  115. // Synchronize a copy of the ConnectedThread
  116. synchronized (this) {
  117. r = mConnectedThread;
  118. }
  119. // Perform the write unsynchronized
  120. Log.d(TAG, "write() called");
  121. r.write(out);
  122. Log.d(TAG, "write() finished");
  123. }
  124.  
  125. private class AcceptThread extends Thread {
  126. private final BluetoothServerSocket mmServerSocket;
  127.  
  128. public AcceptThread() {
  129. // Use a temporary object that is later assigned to mmServerSocket,
  130. // because mmServerSocket is final
  131. BluetoothServerSocket tmp = null;
  132. try {
  133. // MY_UUID is the app's UUID string, also used by the client code
  134. tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
  135. } catch (IOException e) {
  136. }
  137. mmServerSocket = tmp;
  138. }
  139.  
  140. public void run() {
  141. BluetoothSocket socket = null;
  142. // Keep listening until exception occurs or a socket is returned
  143. while (true) {
  144. try {
  145. socket = mmServerSocket.accept();
  146. } catch (IOException e) {
  147. break;
  148. }
  149. // If a connection was accepted
  150. if (socket != null) {
  151. // Do work to manage the connection (in a separate thread)
  152. manageConnectedSocket(socket, socket.getRemoteDevice());
  153. try {
  154. mmServerSocket.close();
  155. } catch (IOException e) {
  156. Log.e(TAG, "Could not close unwanted socket", e);
  157. }
  158. break;
  159. }
  160. }
  161. }
  162.  
  163. /**
  164. * Will cancel the listening socket, and cause the thread to finish
  165. */
  166. public void cancel() {
  167. try {
  168. mmServerSocket.close();
  169. } catch (IOException e) {
  170. }
  171. }
  172. }
  173.  
  174. private class ConnectThread extends Thread {
  175. private final BluetoothSocket mmSocket;
  176. private final BluetoothDevice mmDevice;
  177.  
  178. public ConnectThread(BluetoothDevice device) {
  179. // Use a temporary object that is later assigned to mmSocket,
  180. // because mmSocket is final
  181. BluetoothSocket tmp = null;
  182. mmDevice = device;
  183.  
  184. // Get a BluetoothSocket to connect with the given BluetoothDevice
  185. try {
  186. // MY_UUID is the app's UUID string, also used by the server code
  187. tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
  188. } catch (IOException e) {
  189. }
  190. mmSocket = tmp;
  191. }
  192.  
  193. public void run() {
  194. // Cancel discovery because it will slow down the connection
  195. mBluetoothAdapter.cancelDiscovery();
  196.  
  197. try {
  198. // Connect the device through the socket. This will block
  199. // until it succeeds or throws an exception
  200. mmSocket.connect();
  201. } catch (IOException connectException) {
  202. // Unable to connect; close the socket and get out
  203. try {
  204. mmSocket.close();
  205. } catch (IOException closeException) {
  206. }
  207. return;
  208. }
  209.  
  210. // Do work to manage the connection (in a separate thread)
  211. manageConnectedSocket(mmSocket, mmDevice);
  212. }
  213.  
  214. /**
  215. * Will cancel an in-progress connection, and close the socket
  216. */
  217. public void cancel() {
  218. try {
  219. mmSocket.close();
  220. } catch (IOException e) {
  221. }
  222. }
  223. }
  224.  
  225. private class ConnectedThread extends Thread {
  226. private final BluetoothSocket mmSocket;
  227. private final InputStream mmInStream;
  228. private final OutputStream mmOutStream;
  229.  
  230. public ConnectedThread(BluetoothSocket socket) {
  231. mmSocket = socket;
  232. InputStream tmpIn = null;
  233. OutputStream tmpOut = null;
  234.  
  235. // Get the input and output streams, using temp objects because
  236. // member streams are final
  237. try {
  238. tmpIn = socket.getInputStream();
  239. tmpOut = socket.getOutputStream();
  240. } catch (IOException e) {
  241. }
  242.  
  243. mmInStream = tmpIn;
  244. mmOutStream = tmpOut;
  245. }
  246.  
  247. public void run() {
  248. byte[] buffer = new byte[1024]; // buffer store for the stream
  249. int bytes; // bytes returned from read()
  250.  
  251. // Keep listening to the InputStream until an exception occurs
  252. while (true) {
  253. try {
  254. // Read from the InputStream
  255. bytes = mmInStream.read(buffer);
  256. // Send the obtained bytes to the UI activity
  257. //mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
  258. // .sendToTarget();
  259. processarMensagem(bytes);
  260. Log.d(TAG, "received: " + bytes);
  261. } catch (IOException e) {
  262. break;
  263. }
  264. }
  265. }
  266.  
  267. /* Call this from the main activity to send data to the remote device */
  268. public void write(byte[] bytes) {
  269. try {
  270. mmOutStream.write(bytes);
  271. Log.d(TAG, "sent: " + bytes);
  272. } catch (IOException e) {
  273. Log.e(TAG, e.getMessage(), e);
  274. }
  275. }
  276.  
  277. /* Call this from the main activity to shutdown the connection */
  278. public void cancel() {
  279. try {
  280. mmSocket.close();
  281. } catch (IOException e) {
  282. }
  283. }
  284. }
  285.  
  286. public void Disconnect Socket(){
  287. try {
  288. if (DpBtOutputStream!=null)
  289. DpBtOutputStream.close();
  290. }
  291. if (DpBluetoothSocket!=null)
  292.  
  293. DpBluetoothSocket.close();
  294.  
  295. catch (Exception e){
  296.  
  297. }
  298. }
Add Comment
Please, Sign In to add comment