BluetoothSync.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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.UglyDbHelper;
  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. return false;
  76. }
  77. return true;
  78. }
  79. // Create a BroadcastReceiver for ACTION_FOUND
  80. private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
  81. public void onReceive(Context context, Intent intent) {
  82. String action = intent.getAction();
  83. // When discovery finds a device
  84. if (BluetoothDevice.ACTION_FOUND.equals(action)) {
  85. // Get the BluetoothDevice object from the Intent
  86. BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
  87. // Add the name and address to an array adapter to show in a ListView
  88. arrayAdapter.add(device.getName() + "\n" + device.getAddress());
  89. arrayAdapter.notifyDataSetChanged();
  90. }
  91. }
  92. };
  93. // Register the BroadcastReceiver
  94. private void registerBroadcastReceiver(){
  95. IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
  96. context.registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
  97. }
  98. private void deviceDialog() {
  99. AlertDialog.Builder builder = new AlertDialog.Builder(context);
  100. builder.setTitle(R.string.delete_dialog_title);
  101. builder.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() {
  102. public void onClick(DialogInterface dialog, int position) {
  103. String deviceInfo = arrayAdapter.getItem(position);
  104. String mac = deviceInfo.substring(deviceInfo.indexOf("\n") + 1);
  105. ClientThread clientThread = new ClientThread(mBluetoothAdapter.getRemoteDevice(mac));
  106. clientThread.start();
  107. }
  108. });
  109. // builder.create();
  110. ad = builder.show();
  111. }
  112. private class ServerThread extends Thread {
  113. private final BluetoothServerSocket serverSocket;
  114. public ServerThread() {
  115. BluetoothServerSocket tmp = null;
  116. try {
  117. tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(context.getResources().getString(R.string.app_name), serviceUUID);
  118. } catch (IOException e) { }
  119. serverSocket = tmp;
  120. }
  121. public void run() {
  122. BluetoothSocket socket = null;
  123. while(true){
  124. try {
  125. socket = serverSocket.accept();
  126. } catch (IOException e) {
  127. e.printStackTrace();
  128. break;
  129. }
  130. if (socket != null) {
  131. // Do work to manage the connection (in a separate thread)
  132. manageConnectedSocket(socket, SERVER);
  133. try {
  134. serverSocket.close();
  135. } catch (IOException e) {
  136. e.printStackTrace();
  137. }
  138. break;
  139. }
  140. }
  141. }
  142. /** Will cancel the listening socket, and cause the thread to finish */
  143. public void cancel() {
  144. try {
  145. serverSocket.close();
  146. } catch (IOException e) { }
  147. }
  148. }
  149. private class ClientThread extends Thread {
  150. private final BluetoothSocket socket;
  151. public ClientThread(BluetoothDevice device) {
  152. BluetoothSocket tmp = null;
  153. try {
  154. tmp = device.createRfcommSocketToServiceRecord(serviceUUID);
  155. } catch (IOException e) { }
  156. socket = tmp;
  157. }
  158. public void run() {
  159. try {
  160. socket.connect();
  161. } catch (IOException connectException) {
  162. // Unable to connect; close the socket and get out
  163. try {
  164. socket.close();
  165. } catch (IOException closeException) { }
  166. return;
  167. }
  168. manageConnectedSocket(socket, CLIENT);
  169. }
  170. /** Will cancel an in-progress connection, and close the socket */
  171. public void cancel() {
  172. try {
  173. socket.close();
  174. } catch (IOException e) { }
  175. }
  176. }
  177. private class CommunicationThread extends Thread {
  178. private final BluetoothSocket mmSocket;
  179. private final ObjectInputStream objectInput;
  180. private final ObjectOutputStream objectOuput;
  181. private final int identifier;
  182. public CommunicationThread(BluetoothSocket socket, int identifier) {
  183. mmSocket = socket;
  184. ObjectInputStream tmpIn = null;
  185. ObjectOutputStream tmpOut = null;
  186. // Get the input and output streams, using temp objects because
  187. // member streams are final
  188. try {
  189. tmpOut = new ObjectOutputStream(socket.getOutputStream());
  190. tmpIn = new ObjectInputStream(socket.getInputStream());
  191. } catch (IOException e) { e.printStackTrace();}
  192. objectInput = tmpIn;
  193. objectOuput = tmpOut;
  194. this.identifier = identifier;
  195. }
  196. public void run() {
  197. // Keep listening to the InputStream until an exception occurs
  198. // while (true) {
  199. try {
  200. //TODO Ersetze dbh mit Logger
  201. UglyDbHelper dbh = new UglyDbHelper(context);
  202. ArrayList<HashMap<String, Object>> localNetworkInformation = dbh.getNetworkInformation();
  203. if(identifier == SERVER){
  204. // Read from the InputStream
  205. ArrayList<HashMap<String, Object>> remoteNetworkInformation = (ArrayList<HashMap<String, Object>>) objectInput.readObject();;
  206. dbh.updateNetworkInformation(remoteNetworkInformation);
  207. objectOuput.writeObject(localNetworkInformation);
  208. }else{
  209. objectOuput.writeObject(localNetworkInformation);
  210. // Read from the InputStream
  211. ArrayList<HashMap<String, Object>> remoteNetworkInformation = (ArrayList<HashMap<String, Object>>) objectInput.readObject();
  212. dbh.updateNetworkInformation(remoteNetworkInformation);
  213. mmSocket.close();
  214. }
  215. } catch (ClassNotFoundException e) {
  216. e.printStackTrace();
  217. } catch (IOException e) {
  218. e.printStackTrace();
  219. }
  220. // }
  221. }
  222. /* Call this from the main activity to send data to the remote device */
  223. public void write(ArrayList<HashMap<String, Object>> networkInformation) {
  224. try {
  225. objectOuput.writeObject(networkInformation);
  226. } catch (IOException e) {
  227. e.printStackTrace();
  228. }
  229. }
  230. /* Call this from the main activity to shutdown the connection */
  231. public void cancel() {
  232. try {
  233. mmSocket.close();
  234. } catch (IOException e) { }
  235. }
  236. }
  237. }