summaryrefslogtreecommitdiffstats
path: root/examples/java
diff options
context:
space:
mode:
authorPetre Eftime <[email protected]>2016-07-04 17:28:33 +0300
committerPetre Eftime <[email protected]>2016-07-05 15:26:14 +0300
commit3e629a23b367dd2c6a4b0c5d6ec970f6278805c1 (patch)
treed56a8893bd0f824df9672becf5bebf9d243e1bb7 /examples/java
parent2ff15f0bd3fd55f458b5de9d7be25186d39dae4d (diff)
examples: java: Improve interrupt handling in Java examples
Signed-off-by: Petre Eftime <[email protected]>
Diffstat (limited to 'examples/java')
-rw-r--r--examples/java/AsyncTinyB.java22
-rw-r--r--examples/java/HelloTinyB.java21
-rw-r--r--examples/java/Notification.java36
3 files changed, 59 insertions, 20 deletions
diff --git a/examples/java/AsyncTinyB.java b/examples/java/AsyncTinyB.java
index 2f1a0254..b566b795 100644
--- a/examples/java/AsyncTinyB.java
+++ b/examples/java/AsyncTinyB.java
@@ -1,6 +1,8 @@
import tinyb.*;
import java.util.*;
import java.time.*;
+import java.util.concurrent.locks.*;
+import java.util.concurrent.TimeUnit;
public class AsyncTinyB {
private static final float SCALE_LSB = 0.03125f;
@@ -57,7 +59,7 @@ public class AsyncTinyB {
/*
* After we find the device we can stop looking for other devices.
*/
- manager.stopDiscovery();
+ //manager.stopDiscovery();
if (sensor == null) {
System.err.println("No sensor found with the provided address.");
@@ -74,11 +76,18 @@ public class AsyncTinyB {
System.exit(-1);
}
- final Thread mainThread = Thread.currentThread();
+ Lock lock = new ReentrantLock();
+ Condition cv = lock.newCondition();
+
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
running = false;
- sensor.disconnect();
+ lock.lock();
+ try {
+ cv.signalAll();
+ } finally {
+ lock.unlock();
+ }
}
});
@@ -142,7 +151,12 @@ public class AsyncTinyB {
System.out.println(
String.format(" Temp: Object = %fC, Ambient = %fC", objectTempCelsius, ambientTempCelsius));
- Thread.sleep(1000);
+ lock.lock();
+ try {
+ cv.await(1, TimeUnit.SECONDS);
+ } finally {
+ lock.unlock();
+ }
}
sensor.disconnect();
diff --git a/examples/java/HelloTinyB.java b/examples/java/HelloTinyB.java
index 840bf79c..64725b9d 100644
--- a/examples/java/HelloTinyB.java
+++ b/examples/java/HelloTinyB.java
@@ -1,5 +1,7 @@
import tinyb.*;
import java.util.*;
+import java.util.concurrent.locks.*;
+import java.util.concurrent.TimeUnit;
public class HelloTinyB {
private static final float SCALE_LSB = 0.03125f;
@@ -135,11 +137,19 @@ public class HelloTinyB {
System.exit(-1);
}
- final Thread mainThread = Thread.currentThread();
+ Lock lock = new ReentrantLock();
+ Condition cv = lock.newCondition();
+
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
running = false;
- sensor.disconnect();
+ lock.lock();
+ try {
+ cv.signalAll();
+ } finally {
+ lock.unlock();
+ }
+
}
});
@@ -198,7 +208,12 @@ public class HelloTinyB {
System.out.println(
String.format(" Temp: Object = %fC, Ambient = %fC", objectTempCelsius, ambientTempCelsius));
- Thread.sleep(1000);
+ lock.lock();
+ try {
+ cv.await(1, TimeUnit.SECONDS);
+ } finally {
+ lock.unlock();
+ }
}
sensor.disconnect();
diff --git a/examples/java/Notification.java b/examples/java/Notification.java
index e869ad8a..66f2e5de 100644
--- a/examples/java/Notification.java
+++ b/examples/java/Notification.java
@@ -1,6 +1,7 @@
import tinyb.*;
import java.util.*;
import java.time.*;
+import java.util.concurrent.locks.*;
class ValueNotification implements BluetoothNotification<byte[]> {
@@ -89,18 +90,14 @@ public class Notification {
* through the manager's find method.
*/
BluetoothDevice sensor = manager.find(null, args[0], null, Duration.ofSeconds(10));
- sensor.enableConnectedNotifications(new ConnectedNotification());
-
- /*
- * After we find the device we can stop looking for other devices.
- */
- //manager.stopDiscovery();
if (sensor == null) {
System.err.println("No sensor found with the provided address.");
System.exit(-1);
}
+ sensor.enableConnectedNotifications(new ConnectedNotification());
+
System.out.print("Found device: ");
printDevice(sensor);
@@ -111,11 +108,23 @@ public class Notification {
System.exit(-1);
}
- final Thread mainThread = Thread.currentThread();
+ /*
+ * After we find the device we can stop looking for other devices.
+ */
+ //manager.stopDiscovery();
+
+ Lock lock = new ReentrantLock();
+ Condition cv = lock.newCondition();
+
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
running = false;
- sensor.disconnect();
+ lock.lock();
+ try {
+ cv.signalAll();
+ } finally {
+ lock.unlock();
+ }
}
});
@@ -159,11 +168,12 @@ public class Notification {
tempValue.enableValueNotifications(new ValueNotification());
- /*
- * Each second read the value characteristic and display it in a human readable format.
- */
- while (running) {
- Thread.sleep(1000);
+ lock.lock();
+ try {
+ while(running)
+ cv.await();
+ } finally {
+ lock.unlock();
}
sensor.disconnect();