Search in sources :

Example 1 with BluetoothDiscoveryParticipant

use of org.eclipse.smarthome.binding.bluetooth.discovery.BluetoothDiscoveryParticipant in project smarthome by eclipse.

the class BluetoothDiscoveryService method deviceDiscovered.

private void deviceDiscovered(BluetoothAdapter adapter, BluetoothDevice device) {
    for (BluetoothDiscoveryParticipant participant : participants) {
        try {
            DiscoveryResult result = participant.createResult(device);
            if (result != null) {
                thingDiscovered(result);
                return;
            }
        } catch (Exception e) {
            logger.error("Participant '{}' threw an exception", participant.getClass().getName(), e);
        }
    }
    // We did not find a thing type for this device, so let's treat it as a generic one
    String label = device.getName();
    if (label == null || label.length() == 0 || label.equals(device.getAddress().toString().replace(':', '-'))) {
        label = "Bluetooth Device";
    }
    Map<String, Object> properties = new HashMap<>();
    properties.put(BluetoothBindingConstants.CONFIGURATION_ADDRESS, device.getAddress().toString());
    Integer txPower = device.getTxPower();
    if (txPower != null && txPower > 0) {
        properties.put(BluetoothBindingConstants.PROPERTY_TXPOWER, Integer.toString(txPower));
    }
    String manufacturer = BluetoothCompanyIdentifiers.get(device.getManufacturerId());
    if (manufacturer != null) {
        properties.put(Thing.PROPERTY_VENDOR, manufacturer);
        label += " (" + manufacturer + ")";
    }
    ThingUID thingUID = new ThingUID(BluetoothBindingConstants.THING_TYPE_BEACON, adapter.getUID(), device.getAddress().toString().toLowerCase().replace(":", ""));
    // Create the discovery result and add to the inbox
    DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties).withRepresentationProperty(BluetoothBindingConstants.CONFIGURATION_ADDRESS).withBridge(adapter.getUID()).withLabel(label).build();
    thingDiscovered(discoveryResult);
}
Also used : DiscoveryResult(org.eclipse.smarthome.config.discovery.DiscoveryResult) BluetoothDiscoveryParticipant(org.eclipse.smarthome.binding.bluetooth.discovery.BluetoothDiscoveryParticipant) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ThingUID(org.eclipse.smarthome.core.thing.ThingUID)

Aggregations

HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 BluetoothDiscoveryParticipant (org.eclipse.smarthome.binding.bluetooth.discovery.BluetoothDiscoveryParticipant)1 DiscoveryResult (org.eclipse.smarthome.config.discovery.DiscoveryResult)1 ThingUID (org.eclipse.smarthome.core.thing.ThingUID)1