Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void theDeviceOffersItsExperimentAndItLoads() throws Exception {
experiment.experiment.loaded);

assertFalse("the experiment must be left for the host to start, not started here",
experiment.measuring);
experiment.isMeasuring());

//Instrumentation runs in the app's process: returning kills the remote server the host needs.
if (holdForHost)
Expand Down
539 changes: 534 additions & 5 deletions app/src/androidTest/java/de/rwth_aachen/phyphox/LifecycleTest.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public void aNanIsReplacedByTheDefault() throws Exception {
assertBuffer("dropdown_nan", 2);
assertBuffer("edit_nan", 12);
assertBuffer("slider_nan", 3);
assertTrue("replacing a NaN started the experiment", !activity.measuring);
assertTrue("replacing a NaN started the experiment", !activity.isMeasuring());
} finally {
FixtureExperiment.close(activity);
}
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.ACCESS_LOCAL_NETWORK" />
<!-- Measuring in the background (MeasurementService): the foreground service, one permission per declared type, and its notification -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MICROPHONE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_CONNECTED_DEVICE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

<uses-feature android:glEsVersion="0x00020000" android:required="true" />
<uses-feature android:name="android.hardware.camera" android:required="false" />
Expand Down Expand Up @@ -121,6 +128,15 @@
android:name="autoStoreLocales"
android:value="true" />
</service>
<!-- Declares every type a measurement might need; the service only requests the ones the loaded experiment uses -->
<service
android:name="de.rwth_aachen.phyphox.MeasurementService"
android:exported="false"
android:foregroundServiceType="specialUse|location|microphone|connectedDevice">
<property
android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
android:value="Continue a running sensor measurement of a physics experiment while the app is in the background" />
</service>
</application>
<queries>
<!-- Opening web links -->
Expand Down
18 changes: 15 additions & 3 deletions app/src/main/java/de/rwth_aachen/phyphox/Bluetooth/Bluetooth.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import java.io.Serializable
import java.lang.ref.WeakReference
import java.nio.ByteBuffer
import java.util.UUID
import java.util.Vector
Expand Down Expand Up @@ -154,6 +155,17 @@ open class Bluetooth(

private val connectedDeviceInformation = ConnectedDeviceInfo()

//The experiment screen showing this device's battery and signal strength. Weak: a run may outlive its screen.
@Transient
@JvmField
var connectedDeviceDelegate: WeakReference<UpdateConnectedDeviceDelegate>? = null

//The experiment screen that carries out this device's commands, the one showing its run (not the activity that
// loaded it, which may be gone). Weak for the same reason.
@Transient
@JvmField
var commandDelegate: WeakReference<BluetoothCommandDelegate>? = null

@Transient
private var lastToastShown = 0L

Expand Down Expand Up @@ -552,7 +564,7 @@ open class Bluetooth(
private fun updateConnectedDeviceInfo(rssi: Int) {
connectedDeviceInformation.signalStrength = rssi
mainHandler.post {
Experiment.updateConnectedDeviceDelegate?.updateConnectedDevice(arrayListOf(connectedDeviceInformation))
connectedDeviceDelegate?.get()?.updateConnectedDevice(arrayListOf(connectedDeviceInformation))
}
}

Expand Down Expand Up @@ -609,15 +621,15 @@ open class Bluetooth(
Log.w(TAG, "Could not subscribe to the command characteristic")
}

//A notification on the command characteristic. The activity carries the command out like a tap on
//A notification on the command characteristic. The screen carries the command out like a tap on
// the corresponding button; an unknown command is ignored by contract so newer devices stay harmless.
internal fun handleCommand(data: ByteArray) {
val command = BluetoothCommand.decode(data)
if (command == null) {
Log.d(TAG, "Ignoring unknown command from $deviceName: " + (data.firstOrNull()?.let { "0x%02x".format(it.toInt() and 0xff) } ?: "empty"))
return
}
(activity as? BluetoothCommandDelegate)?.onBluetoothCommand(command, this)
commandDelegate?.get()?.onBluetoothCommand(command, this)
}

/** Attributes of a characteristic as defined in the phyphox file */
Expand Down
Loading
Loading