Search in sources :

Example 1 with DiscoveryAgent

use of javax.bluetooth.DiscoveryAgent in project openhab1-addons by openhab.

the class BTDeviceDiscoveryService method run.

@Override
public void run() {
    try {
        LocalDevice localDevice = LocalDevice.getLocalDevice();
        logger.debug("Initializing local bluetooth device ({}, {})", localDevice.getBluetoothAddress(), localDevice.getFriendlyName());
        DiscoveryAgent agent = localDevice.getDiscoveryAgent();
        final Object inquiryCompletedEvent = new Object();
        // this is the call back for the bluetooth driver
        DiscoveryListener listener = new DiscoveryListener() {

            @Override
            public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
                if (!newDevices.contains(btDevice)) {
                    newDevices.add(btDevice);
                    if (!oldDevices.contains(btDevice)) {
                        BluetoothDevice device = toBluetoothDevice(btDevice);
                        logger.debug("Device discovered: {}", device.toString());
                        for (BluetoothEventHandler handler : eventHandler) {
                            handler.handleDeviceInRange(device);
                        }
                    }
                }
            }

            @Override
            public void inquiryCompleted(int discType) {
                // check if any device has disappeared
                for (RemoteDevice btDevice : oldDevices) {
                    if (newDevices.contains(btDevice)) {
                        continue;
                    }
                    BluetoothDevice device = toBluetoothDevice(btDevice);
                    logger.debug("Device out of range: {}", device.toString());
                    for (BluetoothEventHandler handler : eventHandler) {
                        handler.handleDeviceOutOfRange(device);
                    }
                }
                oldDevices = new HashSet<RemoteDevice>(newDevices);
                // we now pass the list of all devices in range to the event handlers
                Iterable<BluetoothDevice> devices = Iterables.transform(newDevices, new Function<RemoteDevice, BluetoothDevice>() {

                    @Override
                    public BluetoothDevice apply(RemoteDevice from) {
                        return toBluetoothDevice(from);
                    }
                });
                for (BluetoothEventHandler handler : eventHandler) {
                    handler.handleAllDevicesInRange(devices);
                }
                newDevices.clear();
                synchronized (inquiryCompletedEvent) {
                    // tell the main thread that we are done
                    inquiryCompletedEvent.notifyAll();
                }
            }

            @Override
            public void serviceSearchCompleted(int transID, int respCode) {
            }

            @Override
            public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
            }
        };
        // this is the main loop, which will run as long as the thread is not marked as interrupted
        while (!interrupted) {
            long starttime = new Date().getTime();
            // check if we at all need to run the bluetooth discovery
            boolean runDiscovery = false;
            for (BluetoothEventHandler handler : eventHandler) {
                if (handler.isActive()) {
                    runDiscovery = true;
                    break;
                }
            }
            if (runDiscovery) {
                synchronized (inquiryCompletedEvent) {
                    try {
                        logger.debug("Launching bluetooth device discovery...");
                        boolean started = agent.startInquiry(DiscoveryAgent.GIAC, listener);
                        if (started) {
                            inquiryCompletedEvent.wait();
                        }
                    } catch (BluetoothStateException e) {
                        logger.error("Error while starting the bluetooth agent", e);
                    } catch (InterruptedException e) {
                        interrupted = true;
                    }
                }
            }
            if (!interrupted) {
                // let this thread sleep until the next discovery should be done
                long sleeptime = refreshRate * 1000L - (new Date().getTime() - starttime);
                if (sleeptime > 0) {
                    logger.debug("Sleeping for {} s...", sleeptime / 1000.0);
                    try {
                        Thread.sleep(sleeptime);
                    } catch (InterruptedException e) {
                        interrupted = true;
                    }
                }
            }
        }
    } catch (BluetoothStateException e) {
        logger.error("Error while initializing local bluetooth device.", e);
    }
}
Also used : DeviceClass(javax.bluetooth.DeviceClass) BluetoothStateException(javax.bluetooth.BluetoothStateException) Date(java.util.Date) BluetoothEventHandler(org.openhab.binding.bluetooth.BluetoothEventHandler) DiscoveryAgent(javax.bluetooth.DiscoveryAgent) LocalDevice(javax.bluetooth.LocalDevice) RemoteDevice(javax.bluetooth.RemoteDevice) DiscoveryListener(javax.bluetooth.DiscoveryListener)

Aggregations

Date (java.util.Date)1 BluetoothStateException (javax.bluetooth.BluetoothStateException)1 DeviceClass (javax.bluetooth.DeviceClass)1 DiscoveryAgent (javax.bluetooth.DiscoveryAgent)1 DiscoveryListener (javax.bluetooth.DiscoveryListener)1 LocalDevice (javax.bluetooth.LocalDevice)1 RemoteDevice (javax.bluetooth.RemoteDevice)1 BluetoothEventHandler (org.openhab.binding.bluetooth.BluetoothEventHandler)1