diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 81f7784..055eca5 100755 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -38,6 +38,8 @@ + + diff --git a/app/src/main/java/com/ericsson/uecontrol/core/logic/UeBehaviourSynchronize.java b/app/src/main/java/com/ericsson/uecontrol/core/logic/UeBehaviourSynchronize.java index 91cbf60..5f6e58a 100755 --- a/app/src/main/java/com/ericsson/uecontrol/core/logic/UeBehaviourSynchronize.java +++ b/app/src/main/java/com/ericsson/uecontrol/core/logic/UeBehaviourSynchronize.java @@ -1,22 +1,12 @@ package com.ericsson.uecontrol.core.logic; -import android.app.Activity; -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.Intent; -import android.content.IntentFilter; import com.ericsson.uecontrol.core.UeBehaviour; -import com.ericsson.uecontrol.core.util.Configurator.Configurable; -import com.ericsson.uecontrol.gui.MainActivity; +import com.ericsson.uecontrol.core.util.BluetoothServer; +import com.ericsson.uecontrol.core.util.BluetoothUtil; import org.apache.log4j.Logger; -import java.io.IOException; -import java.util.Set; -import java.util.UUID; +import java.util.List; /** * Created by ezivkoc on 2015-01-19. @@ -27,11 +17,16 @@ public class UeBehaviourSynchronize extends UeBehaviour { @Override protected void execute() throws Exception { - log.debug("Terminating execution"); - super.getExecutor().setNextBehaviour(0); - log.debug("Resetting executor"); - super.getExecutor().reset(); - Thread.sleep(VISUAL_SLEEP_PERIOD); // Sleep as a visual queue + log.debug("Searching paired devices..."); + List devices = BluetoothUtil.filterWithService(BluetoothUtil.getPairedDevices(), SERVICE_UUID); + if(devices.isEmpty()){ + log.debug("No paired devices found, starting discovery..."); + devices = BluetoothUtil.filterWithService(BluetoothUtil.discover(), SERVICE_UUID); + if(devices.isEmpty()){ + log.debug("No discoverable devices, setting up server..."); + //BluetoothServer server = new BluetoothServer("uecontrol_sync", SERVICE_UUID); + } + } } @@ -42,169 +37,6 @@ public class UeBehaviourSynchronize extends UeBehaviour { @Override public String toString() { - return "Will stop synchronize with a remote device"; - } - - -class AcceptThread extends Thread { - private final BluetoothServerSocket mmServerSocket; - - public AcceptThread() { - // Use a temporary object that is later assigned to mmServerSocket, - // because mmServerSocket is final - BluetoothServerSocket tmp = null; - try { - BluetoothAdapter mBluetoothAdapter = BluetoothUtil.getAdapter(); - // MY_UUID is the app's UUID string, also used by the client code - tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord("uecontrol", UUID.fromString(SERVICE_UUID)); - } catch (IOException e) { } - mmServerSocket = tmp; - } - - public void run() { - try { - BluetoothSocket socket = null; - // Keep listening until exception occurs or a socket is returned - while (true) { - try { - socket = mmServerSocket.accept(); - } catch (IOException e) { - break; - } - // If a connection was accepted - if (socket != null) { - // Do work to manage the connection (in a separate thread) - manageConnectedSocket(socket); - mmServerSocket.close(); - break; - } - } - }catch (Exception e){ - log.trace(null, e); - } - } - - private void manageConnectedSocket(BluetoothSocket socket) { - - } - - /** Will cancel the listening socket, and cause the thread to finish */ - public void cancel() { - try { - mmServerSocket.close(); - } catch (IOException e) { } - } - - - -} - -private class ConnectThread extends Thread { - private final BluetoothSocket mmSocket; - private final BluetoothDevice mmDevice; - - public ConnectThread(BluetoothDevice device) { - // Use a temporary object that is later assigned to mmSocket, - // because mmSocket is final - BluetoothSocket tmp = null; - mmDevice = device; - - // Get a BluetoothSocket to connect with the given BluetoothDevice - try { - // MY_UUID is the app's UUID string, also used by the server code - tmp = device.createRfcommSocketToServiceRecord(UUID.fromString(SERVICE_UUID)); - } catch (IOException e) { } - mmSocket = tmp; - } - - public void run() { - BluetoothAdapter mBluetoothAdapter = BluetoothUtil.getAdapter(); - // Cancel discovery because it will slow down the connection - mBluetoothAdapter.cancelDiscovery(); - - try { - // Connect the device through the socket. This will block - // until it succeeds or throws an exception - mmSocket.connect(); - } catch (IOException connectException) { - // Unable to connect; close the socket and get out - try { - mmSocket.close(); - } catch (IOException closeException) { } - return; - } - - // Do work to manage the connection (in a separate thread) - manageConnectedSocket(mmSocket); - } - - private void manageConnectedSocket(BluetoothSocket mmSocket) { - - } - - /** Will cancel an in-progress connection, and close the socket */ - public void cancel() { - try { - mmSocket.close(); - } catch (IOException e) { } - } - - - - - -} -} - - -class BluetoothUtil{ - public static BluetoothAdapter getAdapter(){ - BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); - if (mBluetoothAdapter == null) { - if (!mBluetoothAdapter.isEnabled()) { - Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); - ((Activity)MainActivity.getContext()).startActivityForResult(enableBtIntent, 1515); - } - } - return mBluetoothAdapter; - } - - public static void getPairedDevices(){ - BluetoothAdapter mBluetoothAdapter = getAdapter(); - Set pairedDevices = mBluetoothAdapter.getBondedDevices(); - // If there are paired devices - if (pairedDevices.size() > 0) { - // Loop through paired devices - for (BluetoothDevice device : pairedDevices) { - // Add the name and address to an array adapter to show in a ListView - //log.debug("Paired: " + device.getName() + "\n" + device.getAddress()); - } - } - } - - public static void discover(){ - // Create a BroadcastReceiver for ACTION_FOUND - 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 - //log.debug("Discovered: "+device.getName() + "\n" + device.getAddress()); - } - } - }; - // Register the BroadcastReceiver - IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); - MainActivity.getContext().registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy - } - - public void setDiscoverable(){ - Intent discoverableIntent = new - Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); - discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); - MainActivity.getContext().startActivity(discoverableIntent); + return "Will synchronize with another remote device"; } } \ No newline at end of file diff --git a/app/src/main/java/com/ericsson/uecontrol/core/util/BluetoothClient.java b/app/src/main/java/com/ericsson/uecontrol/core/util/BluetoothClient.java new file mode 100755 index 0000000..79b0ef2 --- /dev/null +++ b/app/src/main/java/com/ericsson/uecontrol/core/util/BluetoothClient.java @@ -0,0 +1,65 @@ +package com.ericsson.uecontrol.core.util; + +import android.bluetooth.BluetoothAdapter; +import android.bluetooth.BluetoothDevice; +import android.bluetooth.BluetoothSocket; +import org.apache.log4j.Logger; + +import java.io.IOException; +import java.util.UUID; + +public class BluetoothClient extends Thread { + private static final Logger log = Logger.getLogger(BluetoothClient.class); + + private BluetoothSocket mmSocket; + private BluetoothDevice mmDevice; + + public BluetoothClient(BluetoothDevice device, String service_uuid) { + mmDevice = device; + + // Get a BluetoothSocket to connect with the given BluetoothDevice + try { + // MY_UUID is the app's UUID string, also used by the server code + mmSocket = device.createRfcommSocketToServiceRecord(UUID.fromString(service_uuid)); + } catch (IOException e) { + log.trace(null, e); + } + } + + public void run() { + BluetoothAdapter mBluetoothAdapter = BluetoothUtil.getAdapter(); + // Cancel discovery because it will slow down the connection + mBluetoothAdapter.cancelDiscovery(); + + try { + // Connect the device through the socket. This will block + // until it succeeds or throws an exception + mmSocket.connect(); + } catch (IOException connectException) { + // Unable to connect; close the socket and get out + try { + mmSocket.close(); + } catch (IOException closeException) { + } + return; + } + + // Do work to manage the connection (in a separate thread) + manageConnectedSocket(mmSocket); + } + + private void manageConnectedSocket(BluetoothSocket mmSocket) { + + } + + /** + * Will terminate an in-progress connection, and close the socket + */ + public void close() { + try { + mmSocket.close(); + } catch (IOException e) { + log.trace(null, e); + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/ericsson/uecontrol/core/util/BluetoothServer.java b/app/src/main/java/com/ericsson/uecontrol/core/util/BluetoothServer.java new file mode 100755 index 0000000..eb0efa3 --- /dev/null +++ b/app/src/main/java/com/ericsson/uecontrol/core/util/BluetoothServer.java @@ -0,0 +1,61 @@ +package com.ericsson.uecontrol.core.util; + +import android.bluetooth.BluetoothAdapter; +import android.bluetooth.BluetoothServerSocket; +import android.bluetooth.BluetoothSocket; +import org.apache.log4j.Logger; + +import java.io.IOException; +import java.util.UUID; + +public class BluetoothServer extends Thread { + private static final Logger log = Logger.getLogger(BluetoothServer.class); + + private BluetoothServerSocket mmServerSocket; + + public BluetoothServer(String service, String service_uuid) { + try { + BluetoothAdapter mBluetoothAdapter = BluetoothUtil.getAdapter(); + // MY_UUID is the app's UUID string, also used by the client code + mmServerSocket = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(service, UUID.fromString(service_uuid)); + } catch (IOException e) { + log.trace(null, e); + } + } + + public void run() { + try { + BluetoothSocket socket = null; + // Keep listening until exception occurs or a socket is returned + while (true) { + try { + socket = mmServerSocket.accept(); + } catch (IOException e) { + break; + } + // If a connection was accepted + if (socket != null) { + // Do work to manage the connection (in a separate thread) + manageConnectedSocket(socket); + mmServerSocket.close(); + break; + } + } + }catch (Exception e){ + log.trace(null, e); + } + } + + private void manageConnectedSocket(BluetoothSocket socket) { + + } + + /** + * Will cancel the listening socket, and cause the thread to finish + */ + public void close() { + try { + mmServerSocket.close(); + } catch (IOException e) { } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/ericsson/uecontrol/core/util/BluetoothUtil.java b/app/src/main/java/com/ericsson/uecontrol/core/util/BluetoothUtil.java new file mode 100755 index 0000000..fb75331 --- /dev/null +++ b/app/src/main/java/com/ericsson/uecontrol/core/util/BluetoothUtil.java @@ -0,0 +1,111 @@ +package com.ericsson.uecontrol.core.util; + +import android.app.Activity; +import android.bluetooth.BluetoothAdapter; +import android.bluetooth.BluetoothDevice; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import com.ericsson.uecontrol.gui.MainActivity; +import org.apache.log4j.Logger; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import java.util.UUID; + +public class BluetoothUtil { + private static final Logger log = Logger.getLogger(BluetoothUtil.class); + + public static BluetoothAdapter getAdapter() { + BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); + if (mBluetoothAdapter != null) { + if (!mBluetoothAdapter.isEnabled()) { + Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); + ((Activity) MainActivity.getContext()).startActivityForResult(enableBtIntent, 1515); + } + } + else + log.error("Device does not support Bluetooth"); + return mBluetoothAdapter; + } + + public static List getPairedDevices() { + ArrayList list = new ArrayList(); + BluetoothAdapter mBluetoothAdapter = getAdapter(); + + Set pairedDevices = mBluetoothAdapter.getBondedDevices(); + for (BluetoothDevice device : pairedDevices) { + log.debug("Paired device: " + device.getName() + " | " + device.getAddress()); + list.add(device); + } + return list; + } + + public static List discover() { + final ArrayList list = new ArrayList(); + BluetoothAdapter mBluetoothAdapter = getAdapter(); + + // Create a BroadcastReceiver for ACTION_FOUND + 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); + + log.debug("Discovered: " + device.getName() + " | " + device.getAddress()); + list.add(device); + } + } + }; + + try { + // Register the BroadcastReceiver + IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); + MainActivity.getContext().registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy + + log.debug("Starting Bluetooth discovery..."); + mBluetoothAdapter.startDiscovery(); + + Thread.sleep(12000); + } catch (InterruptedException e) { + log.trace(null, e); + } finally { + log.debug("Canceling Bluetooth discovery..."); + mBluetoothAdapter.cancelDiscovery(); + MainActivity.getContext().unregisterReceiver(mReceiver); + } + return list; + } + + public static boolean containsService(BluetoothDevice device, String service_uuid){ + if(device == null || device.getUuids() == null) + return false; + UUID uuid = UUID.fromString(service_uuid); + for(int i=0; i filterWithService(List list, String service_uuid){ + ArrayList filtered = new ArrayList(); + for(int i=0; icom.ericsson.uecontrol.core.logic.UeBehaviourIterator com.ericsson.uecontrol.core.logic.UeBehaviourStop + com.ericsson.uecontrol.core.logic.UeBehaviourSynchronize