2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.bluetooth.bluez.internal.events;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.bluetooth.BluetoothAddress;
20 * The {@link BlueZEvent} class represents an event from dbus due to
21 * changes in the properties of a bluetooth device.
23 * @author Benjamin Lafois - Initial Contribution
27 public abstract class BlueZEvent {
29 private String dbusPath;
31 private @Nullable BluetoothAddress device;
32 private @Nullable String adapterName;
34 public BlueZEvent(String dbusPath) {
35 this.dbusPath = dbusPath;
37 // the rest of the code should be equivalent to parsing with the following regex:
38 // "/org/bluez/(?<adapterName>[^/]+)(/dev_(?<deviceMac>[^/]+).*)?"
39 if (!dbusPath.startsWith("/org/bluez/")) {
42 int start = dbusPath.indexOf('/', 11);
44 this.adapterName = dbusPath.substring(11);
47 this.adapterName = dbusPath.substring(11, start);
50 int end = dbusPath.indexOf('/', start);
53 mac = dbusPath.substring(start);
55 mac = dbusPath.substring(start, end);
57 if (!mac.startsWith("dev_")) {
60 mac = mac.substring(4); // trim off the "dev_" prefix
62 this.device = new BluetoothAddress(mac.replace('_', ':').toUpperCase());
66 public String getDbusPath() {
70 public @Nullable BluetoothAddress getDevice() {
74 public @Nullable String getAdapterName() {
78 public abstract void dispatch(BlueZEventListener listener);
81 public String toString() {
82 return getClass().getSimpleName() + ": " + dbusPath;