Skip to content

Commit

Permalink
Merge branch 'tincore-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
hypfvieh committed Jun 29, 2020
2 parents 50c1798 + 7615c63 commit 22bc2ba
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@

/**
* Bluetooth device ObjectType identifier.
* @author hypfvieh
*
* @author hypfvieh
*/
public enum BluetoothDeviceType {
NONE, ADAPTER, DEVICE, GATT_SERVICE, GATT_CHARACTERISTIC, GATT_DESCRIPTOR, BATTERY
NONE,
ADAPTER,
DEVICE,
GATT_SERVICE,
GATT_CHARACTERISTIC,
GATT_DESCRIPTOR,
BATTERY,
PROFILE,
PROFILE_MANAGER;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.github.hypfvieh.bluetooth.wrapper;

import java.util.Map;

import org.freedesktop.dbus.FileDescriptor;
import org.freedesktop.dbus.types.Variant;

public interface ProfileChangeListener {
void onProfileConnection(String _dbusPath, FileDescriptor _fd, Map<String, Variant<?>> _fdProperties);

void onProfileDisconnectRequest(String _path);

void onProfileRelease();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.github.hypfvieh.bluetooth.wrapper;

import java.util.Map;

import org.bluez.Profile1;
import org.bluez.exceptions.BluezCanceledException;
import org.bluez.exceptions.BluezRejectedException;
import org.freedesktop.dbus.DBusPath;
import org.freedesktop.dbus.FileDescriptor;
import org.freedesktop.dbus.types.Variant;

public final class ProfileHandler implements Profile1 {
private final String objectPath;
private final ProfileChangeListener profileChangeListener;

public ProfileHandler(String _objectPath, ProfileChangeListener _profileChangeListener) {
this.objectPath = _objectPath;
this.profileChangeListener = _profileChangeListener;
}

@Override
public void NewConnection(DBusPath _device, FileDescriptor _fd, Map<String, Variant<?>> _fdProperties) throws BluezRejectedException, BluezCanceledException {
profileChangeListener.onProfileConnection(_device.getPath(), _fd, _fdProperties);
}

@Override
public void Release() {
profileChangeListener.onProfileRelease();
}

@Override
public void RequestDisconnection(DBusPath _device) throws BluezRejectedException, BluezCanceledException {
profileChangeListener.onProfileDisconnectRequest(_device.getPath());
}

@Override
public String getObjectPath() {
return objectPath;
}

@Override
public boolean isRemote() {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.github.hypfvieh.bluetooth.wrapper;

import java.util.Map;
import java.util.UUID;

import org.bluez.ProfileManager1;
import org.bluez.exceptions.BluezAlreadyExistsException;
import org.bluez.exceptions.BluezDoesNotExistException;
import org.bluez.exceptions.BluezInvalidArgumentsException;
import org.freedesktop.dbus.DBusPath;
import org.freedesktop.dbus.connections.impl.DBusConnection;
import org.freedesktop.dbus.interfaces.DBusInterface;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.hypfvieh.DbusHelper;

public class ProfileManager extends AbstractBluetoothObject {
private final Logger logger = LoggerFactory.getLogger(getClass());

private final ProfileManager1 rawProfileManager;

public ProfileManager(DBusConnection _dbusConnection) {
super(BluetoothDeviceType.PROFILE_MANAGER, _dbusConnection, "/org/bluez");
rawProfileManager = DbusHelper.getRemoteObject(_dbusConnection, getDbusPath(), ProfileManager1.class);

}

@Override
protected Class<? extends DBusInterface> getInterfaceClass() {
return ProfileManager1.class;
}

public boolean registerProfile(String _path, String _uuid, Map<String, Object> _options) {
try {
rawProfileManager.RegisterProfile(new DBusPath(_path), _uuid, optionsToVariantMap(_options));
return true;
} catch (BluezAlreadyExistsException e) {
logger.debug("Profile already exists (UUID: {}, Path: {}).", _uuid, _path, e);
return true;
} catch (BluezInvalidArgumentsException e) {
logger.error("Error while registering Profile (UUID: {}, Path: {}).", _uuid, _path, e);
return false;
}
}

public boolean unregisterProfile(UUID _uuid, String _path) {
try {
rawProfileManager.UnregisterProfile(new DBusPath(_path));
return true;
} catch (BluezDoesNotExistException e) {
logger.trace("Profile does not exist (UUID: {}, Path: {}).", _uuid, _path, e);
return false;
}
}

}

0 comments on commit 22bc2ba

Please sign in to comment.