Initial implementation of bluetooth classes

This commit is contained in:
Ziver Koc 2015-01-20 18:07:28 +01:00
parent a7704dc9cd
commit 97328f9978
6 changed files with 254 additions and 182 deletions

View file

@ -38,6 +38,8 @@
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SEND_SMS" />

View file

@ -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<BluetoothDevice> 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<BluetoothDevice> 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";
}
}

View file

@ -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);
}
}
}

View file

@ -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) { }
}
}

View file

@ -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<BluetoothDevice> getPairedDevices() {
ArrayList<BluetoothDevice> list = new ArrayList<BluetoothDevice>();
BluetoothAdapter mBluetoothAdapter = getAdapter();
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
for (BluetoothDevice device : pairedDevices) {
log.debug("Paired device: " + device.getName() + " | " + device.getAddress());
list.add(device);
}
return list;
}
public static List<BluetoothDevice> discover() {
final ArrayList<BluetoothDevice> list = new ArrayList<BluetoothDevice>();
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<device.getUuids().length; i++){
if(device.getUuids()[i].getUuid().equals(uuid)){
return true;
}
}
return false;
}
public static List<BluetoothDevice> filterWithService(List<BluetoothDevice> list, String service_uuid){
ArrayList<BluetoothDevice> filtered = new ArrayList<BluetoothDevice>();
for(int i=0; i<list.size(); i++){
if(containsService(list.get(i), service_uuid))
filtered.add(list.get(i));
}
return filtered;
}
public void setDiscoverable() {
Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 60);
MainActivity.getContext().startActivity(discoverableIntent);
}
}

View file

@ -13,6 +13,7 @@
<item>com.ericsson.uecontrol.core.logic.UeBehaviourIterator</item>
<item>com.ericsson.uecontrol.core.logic.UeBehaviourStop</item>
<item>com.ericsson.uecontrol.core.logic.UeBehaviourSynchronize</item>
</string-array>
<string-array name="throughput_types">