summaryrefslogtreecommitdiffstats
path: root/examples/java/Hellotinyb.java
blob: 3597a63ebbef388437008ef3f57bb36ef67e4917 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import tinyb.*;
import java.util.*;

public class Hellotinyb {
    private static final float SCALE_LSB = 0.03125f;

    public static void main(String[] args) throws InterruptedException {
        BluetoothManager manager = BluetoothManager.getBluetoothManager();

        if (args.length < 1) {
            System.err.println("Run with <device_address> argument");
            System.exit(-1);
        }

        boolean discoveryStarted = manager.startDiscovery();

        System.out.println("The discovery started: " + (discoveryStarted ? "true" : "false"));
        BluetoothDevice sensor = null;
        for (int i = 0; i < 15; ++i) {
            List<BluetoothDevice> list = manager.getDevices();

            for (BluetoothDevice device : list) {
                System.out.println("Address = " + device.getAddress());
                System.out.println("Name = " + device.getName());
                System.out.println("Connected = " + device.getConnected());

                if (device.getAddress().equals(args[0])) {
                    sensor = device;
                }
            }
            if (sensor != null) {
                break;
            }
            System.out.println("");
            Thread.sleep(4000);
        }

        Thread.sleep(4000);
        manager.stopDiscovery();

        if (sensor == null) {
            System.out.println("No sensor found with the provided address.");
            System.exit(-1);
        }

        Thread.sleep(4000);
        sensor.connect();
        System.out.println("Sensor with the provided address connected");

        List<BluetoothGattService> bluetoothServices = null;

        do {
            Thread.sleep(4000);
            bluetoothServices = sensor.getServices();

            for (BluetoothGattService service : bluetoothServices) {
                System.out.println("UUID: " + service.getUuid());
            }
        } while (bluetoothServices.isEmpty());
    }
}