Page MenuHomec4science

EcgManager.java
No OneTemporary

File Metadata

Created
Fri, May 10, 12:34

EcgManager.java

package ch.epfl.esl.elevatedmonitor.Devices.ESL_ECG;
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 android.util.Log;
import ch.epfl.esl.elevatedmonitor.Constants;
import ch.epfl.esl.elevatedmonitor.Interfaces.InterfaceManager;
import ch.epfl.esl.elevatedmonitor.ServiceManager;
import ch.epfl.esl.elevatedmonitor.SuperClasses.Device;
import ch.epfl.esl.elevatedmonitor.SuperClasses.Manager;
public class EcgManager extends Manager implements InterfaceManager {
//--- Variable declaration
//------ Private
private BluetoothAdapter mBluetoothAdapter;
private BluetoothBroadcast bluetoothBroadcast = new BluetoothBroadcast();
//--------- Statics
private static final String TAG = "ECG Manager";
private static final String[] mac_address = {"76:B7:B5:C7:7B:55"};
//--------- Flags
private Boolean scanning = false;
private Boolean connecting = false;
//--- Constructor
public EcgManager(Context context) {
super(context, TAG);
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
context.registerReceiver(bluetoothBroadcast, filter);
}
//--- Functions to handle events from update status
private void connected() {
Log.d(TAG, "Device Connected");
//Stop searching for connection
connecting = false;
stopScan();
//Insert into file memory connected device
saveConnectedFile();
// Notify the Service Manager that there is a new connection
ServiceManager.startActionConnected(managerContext);
}
private void disconnected() {
for (Device device : getConnectedDevice()) {
if (device.isConnected()) {
device.setConnected(false);
ServiceManager.startActionDisconnect(managerContext, device.getName());
managerContext.unregisterReceiver(bluetoothBroadcast);
}
}
deleteDisconnectedDevice();
}
//--- Interface Manager
@Override
public void scan() {
if (!scanning) {
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter != null) {
if (!mBluetoothAdapter.isEnabled()) {
//--- Request Bluetooth right
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
ServiceManager.startActivityForResult(enableBtIntent, Constants.REQUEST_ENABLE_BT);
} else {
mBluetoothAdapter.startDiscovery();
}
}
scanning = true;
Log.d(TAG, "Scan started");
}
}
@Override
public void isDevice(String name, String address) {
}
@Override
public void stopScan() {
if (scanning) {
//Put flags false
scanning = false;
//Stop the process
Log.d(TAG, "Scan stopped");
} else {
Log.d(TAG, "Nothing to stop.");
}
}
@Override
public void connect(String id) {
if (!connecting) {
//Inform that we have to connect.
connecting = true;
Log.d(TAG, "Start the scan to connect.");
scan();
}
}
@Override
public void disconnect(String id) {
}
@Override
public void onActivityResult(String requestCode, int resultCode) {
/*
if (requestCode.equals(BluetoothAdapter.ACTION_REQUEST_ENABLE)) {
if (resultCode == Activity.RESULT_OK) {
if (scanning) {
scan();
}
}
}*/
}
@Override
public void readBatteryLevel(String id) {
}
// Create a BroadcastReceiver for ACTION_FOUND.
public class BluetoothBroadcast extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Discovery has found a device. Get the BluetoothDevice
// object and its info from the Intent.
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress();
Log.d(TAG, "found device: " + deviceName + " " + deviceHardwareAddress);
for (String mac : mac_address) {
if (deviceHardwareAddress.equals(mac)) {
Ecg ecg = new Ecg(managerContext, deviceName, deviceHardwareAddress);
addScannedDevice(ecg);
ServiceManager.startActionScanned(managerContext);
}
}
}
}
}
;
}

Event Timeline