BluetoothSync.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. package de.tudarmstadt.informatik.hostage.sync;
  2. import java.io.IOException;
  3. import java.io.ObjectInputStream;
  4. import java.io.ObjectOutputStream;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.UUID;
  8. import android.app.AlertDialog;
  9. import android.bluetooth.BluetoothAdapter;
  10. import android.bluetooth.BluetoothDevice;
  11. import android.bluetooth.BluetoothServerSocket;
  12. import android.bluetooth.BluetoothSocket;
  13. import android.content.BroadcastReceiver;
  14. import android.content.Context;
  15. import android.content.DialogInterface;
  16. import android.content.Intent;
  17. import android.content.IntentFilter;
  18. import android.widget.ArrayAdapter;
  19. import de.tudarmstadt.informatik.hostage.R;
  20. import de.tudarmstadt.informatik.hostage.logging.DatabaseHandler;
  21. public class BluetoothSync{
  22. private final UUID serviceUUID;
  23. private final int SERVER = 0;
  24. private final int CLIENT = 1;
  25. private BluetoothAdapter mBluetoothAdapter;
  26. private Context context;
  27. private ArrayAdapter<String> arrayAdapter;
  28. private ServerThread serverThread;
  29. private AlertDialog ad;
  30. public BluetoothSync(Context context){
  31. this.context = context;
  32. serviceUUID = UUID.fromString(context.getResources().getString(R.string.UUID));
  33. mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  34. }
  35. public boolean syncData(){
  36. if(!bluetoothAvaible())
  37. return false;
  38. syncDataPassive();
  39. return syncDataActive();
  40. }
  41. private boolean syncDataActive() {
  42. registerBroadcastReceiver();
  43. arrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1);
  44. //Start scanning for devices
  45. if(!mBluetoothAdapter.startDiscovery())
  46. return false;
  47. deviceDialog();
  48. return true;
  49. }
  50. private void syncDataPassive(){
  51. Intent discoverableIntent = new
  52. Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
  53. discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
  54. context.startActivity(discoverableIntent);
  55. serverThread = new ServerThread();
  56. serverThread.start();
  57. }
  58. private void manageConnectedSocket(BluetoothSocket socket, int identifier){
  59. if(identifier == SERVER){
  60. ad.dismiss();
  61. }
  62. mBluetoothAdapter.cancelDiscovery();
  63. context.unregisterReceiver(mReceiver);
  64. CommunicationThread commThread = new CommunicationThread(socket, identifier);
  65. commThread.start();
  66. }
  67. private boolean bluetoothAvaible(){
  68. if (mBluetoothAdapter == null) {
  69. // Device does not support Bluetooth
  70. return false;
  71. }
  72. if (!mBluetoothAdapter.isEnabled()) {
  73. Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  74. context.startActivity(enableBtIntent);
  75. //TODO CHECK
  76. // if(!mBluetoothAdapter.isEnabled())
  77. // return false;
  78. }
  79. return true;
  80. }
  81. // Create a BroadcastReceiver for ACTION_FOUND
  82. private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
  83. public void onReceive(Context context, Intent intent) {
  84. String action = intent.getAction();
  85. // When discovery finds a device
  86. if (BluetoothDevice.ACTION_FOUND.equals(action)) {
  87. // Get the BluetoothDevice object from the Intent
  88. BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
  89. // Add the name and address to an array adapter to show in a ListView
  90. arrayAdapter.add(device.getName() + "\n" + device.getAddress());
  91. arrayAdapter.notifyDataSetChanged();
  92. }
  93. }
  94. };
  95. // Register the BroadcastReceiver
  96. private void registerBroadcastReceiver(){
  97. IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
  98. context.registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
  99. }
  100. private void deviceDialog() {
  101. AlertDialog.Builder builder = new AlertDialog.Builder(context);
  102. builder.setTitle(R.string.delete_dialog_title);
  103. builder.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() {
  104. public void onClick(DialogInterface dialog, int position) {
  105. String deviceInfo = arrayAdapter.getItem(position);
  106. String mac = deviceInfo.substring(deviceInfo.indexOf("\n") + 1);
  107. ClientThread clientThread = new ClientThread(mBluetoothAdapter.getRemoteDevice(mac));
  108. clientThread.start();
  109. }
  110. });
  111. // builder.create();
  112. ad = builder.show();
  113. }
  114. private class ServerThread extends Thread {
  115. private final BluetoothServerSocket serverSocket;
  116. public ServerThread() {
  117. BluetoothServerSocket tmp = null;
  118. try {
  119. tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(context.getResources().getString(R.string.app_name), serviceUUID);
  120. } catch (IOException e) { }
  121. serverSocket = tmp;
  122. }
  123. public void run() {
  124. BluetoothSocket socket = null;
  125. while(true){
  126. try {
  127. socket = serverSocket.accept();
  128. } catch (IOException e) {
  129. e.printStackTrace();
  130. break;
  131. }
  132. if (socket != null) {
  133. // Do work to manage the connection (in a separate thread)
  134. manageConnectedSocket(socket, SERVER);
  135. try {
  136. serverSocket.close();
  137. } catch (IOException e) {
  138. e.printStackTrace();
  139. }
  140. break;
  141. }
  142. }
  143. }
  144. /** Will cancel the listening socket, and cause the thread to finish */
  145. public void cancel() {
  146. try {
  147. serverSocket.close();
  148. } catch (IOException e) { }
  149. }
  150. }
  151. private class ClientThread extends Thread {
  152. private final BluetoothSocket socket;
  153. public ClientThread(BluetoothDevice device) {
  154. BluetoothSocket tmp = null;
  155. try {
  156. tmp = device.createRfcommSocketToServiceRecord(serviceUUID);
  157. } catch (IOException e) { }
  158. socket = tmp;
  159. }
  160. public void run() {
  161. try {
  162. socket.connect();
  163. } catch (IOException connectException) {
  164. // Unable to connect; close the socket and get out
  165. try {
  166. socket.close();
  167. } catch (IOException closeException) { }
  168. return;
  169. }
  170. manageConnectedSocket(socket, CLIENT);
  171. }
  172. /** Will cancel an in-progress connection, and close the socket */
  173. public void cancel() {
  174. try {
  175. socket.close();
  176. } catch (IOException e) { }
  177. }
  178. }
  179. private class CommunicationThread extends Thread {
  180. private final BluetoothSocket mmSocket;
  181. private final ObjectInputStream objectInput;
  182. private final ObjectOutputStream objectOuput;
  183. private final int identifier;
  184. public CommunicationThread(BluetoothSocket socket, int identifier) {
  185. mmSocket = socket;
  186. ObjectInputStream tmpIn = null;
  187. ObjectOutputStream tmpOut = null;
  188. // Get the input and output streams, using temp objects because
  189. // member streams are final
  190. try {
  191. tmpOut = new ObjectOutputStream(socket.getOutputStream());
  192. tmpIn = new ObjectInputStream(socket.getInputStream());
  193. } catch (IOException e) { e.printStackTrace();}
  194. objectInput = tmpIn;
  195. objectOuput = tmpOut;
  196. this.identifier = identifier;
  197. }
  198. public void run() {
  199. // Keep listening to the InputStream until an exception occurs
  200. // while (true) {
  201. try {
  202. DatabaseHandler dbh = new DatabaseHandler(context);
  203. ArrayList<HashMap<String, Object>> localNetworkInformation = dbh.getNetworkInformation();
  204. if(identifier == SERVER){
  205. // Read from the InputStream
  206. ArrayList<HashMap<String, Object>> remoteNetworkInformation = (ArrayList<HashMap<String, Object>>) objectInput.readObject();;
  207. dbh.updateNetworkInformation(remoteNetworkInformation);
  208. objectOuput.writeObject(localNetworkInformation);
  209. }else{
  210. objectOuput.writeObject(localNetworkInformation);
  211. // Read from the InputStream
  212. ArrayList<HashMap<String, Object>> remoteNetworkInformation = (ArrayList<HashMap<String, Object>>) objectInput.readObject();
  213. dbh.updateNetworkInformation(remoteNetworkInformation);
  214. mmSocket.close();
  215. }
  216. } catch (ClassNotFoundException e) {
  217. e.printStackTrace();
  218. } catch (IOException e) {
  219. e.printStackTrace();
  220. }
  221. // }
  222. }
  223. /* Call this from the main activity to send data to the remote device */
  224. public void write(ArrayList<HashMap<String, Object>> networkInformation) {
  225. try {
  226. objectOuput.writeObject(networkInformation);
  227. } catch (IOException e) {
  228. e.printStackTrace();
  229. }
  230. }
  231. /* Call this from the main activity to shutdown the connection */
  232. public void cancel() {
  233. try {
  234. mmSocket.close();
  235. } catch (IOException e) { }
  236. }
  237. }
  238. }