tinyb
tinyb copied to clipboard
"Find" method by locking the application
Hello,
I have a problem. When I run the code below, the code in the method: BluetoothGattService tempService = sensor.find("0000FFE0-0000-1000-8000-00805F9B34FB");
Before that, I can connect to the At-09 module normally. Could you tell me what the problem is? I'm running the application below in eclipse. import tinyb.; import java.util.; import java.time.; import java.util.concurrent.locks.;
public class Main { private static final float SCALE_LSB = 0.03125f; static boolean running = true;
static void printDevice(BluetoothDevice device) {
System.out.print("Address = " + device.getAddress());
System.out.print(" Name = " + device.getName());
System.out.print(" Connected = " + device.getConnected());
System.out.println();
}
public static void main(String[] args) throws InterruptedException {
//while(true) {}
BluetoothManager manager = BluetoothManager.getBluetoothManager();
boolean discoveryStarted = manager.startDiscovery();
System.out.println("The discovery started: " + (discoveryStarted ? "true" : "false"));
BluetoothDevice sensor = getDevice("00:15:83:00:73:35");
/*
* After we find the device we can stop looking for other devices.
*/
try {
manager.stopDiscovery();
System.out.println("Achou");
} catch (BluetoothException e) {
System.err.println("Discovery could not be stopped.");
}
if (sensor == null) {
System.err.println("No sensor found with the provided address.");
System.exit(-1);
}
System.out.print("Found device: ");
printDevice(sensor);
if (sensor.connect())
System.out.println("Sensor with the provided address connected");
else {
System.out.println("Could not connect device.");
System.exit(-1);
}
Lock lock = new ReentrantLock();
Condition cv = lock.newCondition();
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
running = false;
lock.lock();
try {
cv.signalAll();
} finally {
lock.unlock();
}
}
});
//System.out.println(sensor.getUUIDs());
BluetoothGattService tempService = sensor.find("0000FFE0-0000-1000-8000-00805F9B34FB");
System.out.println("locking");
BluetoothGattCharacteristic tempValue = tempService.find("0000FFE1-0000-1000-8000-00805F9B34FB");
}
static BluetoothDevice getDevice(String address) throws InterruptedException {
BluetoothManager manager = BluetoothManager.getBluetoothManager();
BluetoothDevice sensor = null;
for (int i = 0; (i < 15) && running; ++i) {
List<BluetoothDevice> list = manager.getDevices();
for (BluetoothDevice device : list) {
printDevice(device);
/*
* Here we check if the address matches.
*/
if (device.getAddress().equals(address))
sensor = device;
}
if (sensor != null) {
return sensor;
}
Thread.sleep(4000);
}
return null;
}
}