Skip to content

Commit 016917e

Browse files
committed
Add and use SKBluetoothData and SKBluetoothDeviceData
1 parent 607eccf commit 016917e

File tree

3 files changed

+87
-5
lines changed

3 files changed

+87
-5
lines changed

SensingKitLib/src/main/java/org/sensingkit/sensingkitlib/data/SKBluetoothData.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,29 @@
2121

2222
package org.sensingkit.sensingkitlib.data;
2323

24+
import java.util.ArrayList;
25+
2426
public class SKBluetoothData extends SKAbstractData {
2527

2628
@SuppressWarnings("unused")
2729
private static final String TAG = "SKBluetoothData";
2830

29-
public SKBluetoothData(long timestamp) {
31+
private final ArrayList<SKBluetoothDeviceData> mBluetoothDevices;
32+
33+
public SKBluetoothData(long timestamp, ArrayList<SKBluetoothDeviceData> bluetoothDevices) {
34+
3035
super(timestamp);
36+
37+
this.mBluetoothDevices = bluetoothDevices;
3138
}
3239

3340
@Override
3441
public String getDataInCSV() {
3542
return null;
3643
}
44+
45+
@SuppressWarnings("unused")
46+
public ArrayList<SKBluetoothDeviceData> getBluetoothDevices() {
47+
return this.mBluetoothDevices;
48+
}
3749
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (c) 2015. Queen Mary University of London
3+
* Kleomenis Katevas, k.katevas@qmul.ac.uk
4+
*
5+
* This file is part of SensingKit-Android library.
6+
* For more information, please visit http://www.sensingkit.org
7+
*
8+
* SensingKit-Android is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Lesser General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* SensingKit-Android is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Lesser General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with SensingKit-Android. If not, see <http://www.gnu.org/licenses/>.
20+
*/
21+
22+
package org.sensingkit.sensingkitlib.data;
23+
24+
import java.util.Locale;
25+
26+
public class SKBluetoothDeviceData extends SKAbstractData {
27+
28+
@SuppressWarnings("unused")
29+
private static final String TAG = "SKBluetoothDeviceData";
30+
31+
protected final String name;
32+
protected final String address;
33+
protected final int rssi;
34+
35+
public SKBluetoothDeviceData(long timestamp, String name, String address, int rssi) {
36+
37+
super(timestamp);
38+
39+
this.name = name;
40+
this.address = address;
41+
this.rssi = rssi;
42+
}
43+
44+
@Override
45+
public String getDataInCSV() {
46+
return String.format(Locale.US, "%d,%s,%s,%d", this.timestamp, this.name, this.address, this.rssi);
47+
}
48+
49+
@SuppressWarnings("unused")
50+
public String getName() {
51+
return this.name;
52+
}
53+
54+
@SuppressWarnings("unused")
55+
public String getAddress() {
56+
return this.address;
57+
}
58+
59+
@SuppressWarnings("unused")
60+
public int getRssi() {
61+
return this.rssi;
62+
}
63+
}

SensingKitLib/src/main/java/org/sensingkit/sensingkitlib/modules/SKBluetooth.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,29 @@
2727
import android.content.Context;
2828
import android.content.Intent;
2929
import android.content.IntentFilter;
30-
import android.util.Log;
3130

3231
import org.sensingkit.sensingkitlib.SKException;
3332
import org.sensingkit.sensingkitlib.SKExceptionErrorCode;
3433
import org.sensingkit.sensingkitlib.SKSensorModuleType;
3534
import org.sensingkit.sensingkitlib.data.SKAbstractData;
3635
import org.sensingkit.sensingkitlib.data.SKBluetoothData;
36+
import org.sensingkit.sensingkitlib.data.SKBluetoothDeviceData;
37+
38+
import java.util.ArrayList;
3739

3840
public class SKBluetooth extends SKAbstractSensorModule {
3941

4042
@SuppressWarnings("unused")
4143
private static final String TAG = "SKBluetooth";
4244

43-
private BluetoothAdapter mBluetoothAdapter;
45+
private final BluetoothAdapter mBluetoothAdapter;
46+
private ArrayList<SKBluetoothDeviceData> mBluetoothDevices;
4447

4548
public SKBluetooth(Context context) throws SKException {
4649
super(context, SKSensorModuleType.BLUETOOTH);
4750

4851
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
52+
mBluetoothDevices = new ArrayList<>();
4953

5054
if (mBluetoothAdapter == null) {
5155
throw new SKException(TAG, "Bluetooth sensor module is not supported from the device.", SKExceptionErrorCode.UNKNOWN_ERROR);
@@ -71,14 +75,16 @@ public void onReceive(Context context, Intent intent) {
7175
int rssi = (int) intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE);
7276

7377
// Create SKBluetoothDevice and add to mBluetoothDevices array
74-
Log.i(TAG, "Device Found: " + name + ", " + address + ", " + rssi);
78+
SKBluetoothDeviceData deviceData = new SKBluetoothDeviceData(System.currentTimeMillis(), name, address, rssi);
79+
mBluetoothDevices.add(deviceData);
7580

7681
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { // Discovery Finished
7782

7883
// Build the data object
79-
SKAbstractData data = new SKBluetoothData(System.currentTimeMillis());
84+
SKAbstractData data = new SKBluetoothData(System.currentTimeMillis(), mBluetoothDevices);
8085

8186
// Clean the arrayList
87+
mBluetoothDevices = new ArrayList<>();
8288

8389
// Submit sensor data object
8490
submitSensorData(data);
@@ -127,6 +133,7 @@ public void stopSensing() {
127133
}
128134

129135
private void registerLocalBroadcastManager() {
136+
130137
// Register receivers for ACTION_FOUND and ACTION_DISCOVERY_FINISHED
131138
IntentFilter foundFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
132139
IntentFilter finishedFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);

0 commit comments

Comments
 (0)