123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- package de.tudarmstadt.informatik.hostage.sync;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.UUID;
- import de.tudarmstadt.informatik.hostage.R;
- import de.tudarmstadt.informatik.hostage.logging.DatabaseHandler;
- import android.app.AlertDialog;
- import android.bluetooth.BluetoothAdapter;
- import android.bluetooth.BluetoothDevice;
- import android.bluetooth.BluetoothServerSocket;
- import android.bluetooth.BluetoothSocket;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.util.Log;
- import android.widget.ArrayAdapter;
- public class BluetoothSync{
-
- private final UUID serviceUUID;
- private final int SERVER = 0;
- private final int CLIENT = 1;
- private BluetoothAdapter mBluetoothAdapter;
- private Context context;
- private ArrayAdapter<String> arrayAdapter;
-
- private ServerThread serverThread;
- private AlertDialog ad;
-
- public BluetoothSync(Context context){
- this.context = context;
- serviceUUID = UUID.fromString(context.getResources().getString(R.string.UUID));
- mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
- }
- public boolean syncData(){
- if(!bluetoothAvaible())
- return false;
- syncDataPassive();
- return syncDataActive();
- }
-
- private boolean syncDataActive() {
- registerBroadcastReceiver();
- arrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1);
- //Start scanning for devices
- if(!mBluetoothAdapter.startDiscovery())
- return false;
-
- deviceDialog();
- return true;
- }
-
- private void syncDataPassive(){
- Intent discoverableIntent = new
- Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
- discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
- context.startActivity(discoverableIntent);
-
- serverThread = new ServerThread();
- serverThread.start();
- }
-
- private void manageConnectedSocket(BluetoothSocket socket, int identifier){
- if(identifier == SERVER){
- ad.dismiss();
- }
- mBluetoothAdapter.cancelDiscovery();
- context.unregisterReceiver(mReceiver);
- CommunicationThread commThread = new CommunicationThread(socket, identifier);
- commThread.start();
- }
-
- private boolean bluetoothAvaible(){
- if (mBluetoothAdapter == null) {
- // Device does not support Bluetooth
- return false;
- }
- if (!mBluetoothAdapter.isEnabled()) {
- Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
- context.startActivity(enableBtIntent);
- //TODO CHECK
- // if(!mBluetoothAdapter.isEnabled())
- // return false;
- }
- return true;
- }
- // Create a BroadcastReceiver for ACTION_FOUND
- private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- // When discovery finds a device
- if (BluetoothDevice.ACTION_FOUND.equals(action)) {
- // Get the BluetoothDevice object from the Intent
- BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
- // Add the name and address to an array adapter to show in a ListView
- arrayAdapter.add(device.getName() + "\n" + device.getAddress());
- arrayAdapter.notifyDataSetChanged();
- }
- }
- };
-
-
- // Register the BroadcastReceiver
- private void registerBroadcastReceiver(){
- IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
- context.registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
- }
-
- private void deviceDialog() {
- AlertDialog.Builder builder = new AlertDialog.Builder(context);
- builder.setTitle(R.string.delete_dialog_title);
- builder.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int position) {
- String deviceInfo = arrayAdapter.getItem(position);
- String mac = deviceInfo.substring(deviceInfo.indexOf("\n") + 1);
- ClientThread clientThread = new ClientThread(mBluetoothAdapter.getRemoteDevice(mac));
- clientThread.start();
- }
- });
- // builder.create();
- ad = builder.show();
- }
-
-
- private class ServerThread extends Thread {
- private final BluetoothServerSocket serverSocket;
-
- public ServerThread() {
- BluetoothServerSocket tmp = null;
- try {
- tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(context.getResources().getString(R.string.app_name), serviceUUID);
- } catch (IOException e) { }
- serverSocket = tmp;
- }
-
- public void run() {
- BluetoothSocket socket = null;
- while(true){
- try {
- socket = serverSocket.accept();
- } catch (IOException e) {
- e.printStackTrace();
- break;
- }
-
- if (socket != null) {
- // Do work to manage the connection (in a separate thread)
- manageConnectedSocket(socket, SERVER);
- try {
- serverSocket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- break;
- }
- }
- }
-
- /** Will cancel the listening socket, and cause the thread to finish */
- public void cancel() {
- try {
- serverSocket.close();
- } catch (IOException e) { }
- }
- }
-
- private class ClientThread extends Thread {
- private final BluetoothSocket socket;
-
- public ClientThread(BluetoothDevice device) {
- BluetoothSocket tmp = null;
- try {
- tmp = device.createRfcommSocketToServiceRecord(serviceUUID);
- } catch (IOException e) { }
- socket = tmp;
- }
-
- public void run() {
-
- try {
- socket.connect();
- } catch (IOException connectException) {
- // Unable to connect; close the socket and get out
- try {
- socket.close();
- } catch (IOException closeException) { }
- return;
- }
-
- manageConnectedSocket(socket, CLIENT);
- }
-
- /** Will cancel an in-progress connection, and close the socket */
- public void cancel() {
- try {
- socket.close();
- } catch (IOException e) { }
- }
- }
-
- private class CommunicationThread extends Thread {
- private final BluetoothSocket mmSocket;
- private final ObjectInputStream objectInput;
- private final ObjectOutputStream objectOuput;
- private final int identifier;
-
- public CommunicationThread(BluetoothSocket socket, int identifier) {
- mmSocket = socket;
- ObjectInputStream tmpIn = null;
- ObjectOutputStream tmpOut = null;
-
- // Get the input and output streams, using temp objects because
- // member streams are final
- try {
- tmpOut = new ObjectOutputStream(socket.getOutputStream());
- tmpIn = new ObjectInputStream(socket.getInputStream());
- } catch (IOException e) { e.printStackTrace();}
-
- objectInput = tmpIn;
- objectOuput = tmpOut;
- this.identifier = identifier;
- }
-
- public void run() {
- // Keep listening to the InputStream until an exception occurs
- // while (true) {
- try {
- DatabaseHandler dbh = new DatabaseHandler(context);
- ArrayList<HashMap<String, Object>> localNetworkInformation = dbh.getNetworkInformation();
- if(identifier == SERVER){
- // Read from the InputStream
- Object object = objectInput.readObject();
- ArrayList<HashMap<String, Object>> remoteNetworkInformation = (ArrayList<HashMap<String, Object>>) object;
- dbh.updateNetworkInformation(remoteNetworkInformation);
- objectOuput.writeObject(localNetworkInformation);
- }else{
- objectOuput.writeObject(localNetworkInformation);
- // Read from the InputStream
- Object object = objectInput.readObject();
- ArrayList<HashMap<String, Object>> remoteNetworkInformation = (ArrayList<HashMap<String, Object>>) object;
- dbh.updateNetworkInformation(remoteNetworkInformation);
- mmSocket.close();
- }
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- // }
- }
-
- /* Call this from the main activity to send data to the remote device */
- public void write(ArrayList<HashMap<String, Object>> networkInformation) {
- try {
- objectOuput.writeObject(networkInformation);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- /* Call this from the main activity to shutdown the connection */
- public void cancel() {
- try {
- mmSocket.close();
- } catch (IOException e) { }
- }
- }
- }
|