summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2022-05-09 02:39:02 +0200
committerSven Gothel <[email protected]>2022-05-09 02:39:02 +0200
commit1bf597ac6b51977bc27d22fc57b0bdaa722df86d (patch)
tree903e796b4e18f1c545197e892010a572a4a5b99e
parent03a6ebf6b9d197d7ae4788c4f9d8c4c3ed74fcf5 (diff)
Remove AdapterStatusListener::matchDevice(), moved to impl::StatusListenerPair for BTDevice::addStatusListener() only -> Simplifying API
-rw-r--r--api/direct_bt/BTAdapter.hpp25
-rw-r--r--java/jni/direct_bt/DBTAdapter.cxx23
-rw-r--r--src/direct_bt/BTAdapter.cpp18
3 files changed, 22 insertions, 44 deletions
diff --git a/api/direct_bt/BTAdapter.hpp b/api/direct_bt/BTAdapter.hpp
index ef06d53a..ee16f881 100644
--- a/api/direct_bt/BTAdapter.hpp
+++ b/api/direct_bt/BTAdapter.hpp
@@ -119,22 +119,6 @@ namespace direct_bt {
class AdapterStatusListener : public jau::JavaUplink {
public:
/**
- * Custom filter for all 'device*' notification methods,
- * which will not be called if this method returns false.
- * <p>
- * User may override this method to test whether the 'device*' methods shall be called
- * for the given device.
- * </p>
- * <p>
- * Defaults to true;
- * </p>
- */
- virtual bool matchDevice(const BTDevice & device) {
- (void)device;
- return true;
- }
-
- /**
* BTAdapter setting(s) changed.
* @param adapter the adapter which settings have changed.
* @param oldmask the previous settings mask. AdapterSetting::NONE indicates the initial setting notification, see BTAdapter::addStatusListener().
@@ -314,6 +298,15 @@ namespace direct_bt {
AdapterStatusListenerRef listener;
/** The optional weak device reference. Weak, b/c it shall not block destruction */
std::weak_ptr<BTDevice> wbr_device;
+
+ bool match(const BTDeviceRef& device) const noexcept {
+ BTDeviceRef sda = wbr_device.lock();
+ if( nullptr != sda && nullptr != device ) {
+ return *sda == *device;
+ } else {
+ return true;
+ }
+ }
};
}
diff --git a/java/jni/direct_bt/DBTAdapter.cxx b/java/jni/direct_bt/DBTAdapter.cxx
index 51023378..7ea95178 100644
--- a/java/jni/direct_bt/DBTAdapter.cxx
+++ b/java/jni/direct_bt/DBTAdapter.cxx
@@ -66,8 +66,6 @@ class JNIAdapterStatusListener : public AdapterStatusListener {
private:
/**
public abstract class AdapterStatusListener {
- private long nativeInstance;
-
public void adapterSettingsChanged(final BluetoothAdapter adapter,
final AdapterSettings oldmask, final AdapterSettings newmask,
final AdapterSettings changedmask, final long timestamp) { }
@@ -84,7 +82,6 @@ class JNIAdapterStatusListener : public AdapterStatusListener {
*/
static std::atomic<int> iname_next;
int const iname;
- BTDeviceRef deviceMatchRef;
jau::JNIGlobalRef adapterSettingsClazzRef;
jmethodID adapterSettingsClazzCtor;
@@ -118,16 +115,15 @@ class JNIAdapterStatusListener : public AdapterStatusListener {
public:
std::string toString() const noexcept override {
- const std::string devMatchAddr = nullptr != deviceMatchRef ? deviceMatchRef->getAddressAndType().toString() : "nil";
- return "JNIAdapterStatusListener[this "+jau::to_hexstring(this)+", iname "+std::to_string(iname)+", devMatchAddr "+devMatchAddr+"]";
+ return "JNIAdapterStatusListener[this "+jau::to_hexstring(this)+", iname "+std::to_string(iname)+"]";
}
~JNIAdapterStatusListener() override {
// listenerObjRef dtor will call notifyDelete and clears the nativeInstance handle
}
- JNIAdapterStatusListener(JNIEnv *env, jobject statusListenerObj, const BTDeviceRef& _deviceMatchRef)
- : iname(iname_next.fetch_add(1)), deviceMatchRef(_deviceMatchRef)
+ JNIAdapterStatusListener(JNIEnv *env, jobject statusListenerObj)
+ : iname(iname_next.fetch_add(1))
{
jclass listenerClazz = jau::search_class(env, statusListenerObj);
@@ -209,17 +205,6 @@ class JNIAdapterStatusListener : public AdapterStatusListener {
mDeviceDisconnected = jau::search_method(env, listenerClazz, "deviceDisconnected", _deviceDisconnectedMethodArgs.c_str(), false);
}
- void setDeviceMatchRef(const BTDeviceRef& _deviceMatchRef) {
- deviceMatchRef = _deviceMatchRef;
- }
-
- bool matchDevice(const BTDevice & device) override {
- if( nullptr == deviceMatchRef ) {
- return true;
- }
- return device == *deviceMatchRef;
- }
-
void adapterSettingsChanged(BTAdapter &a, const AdapterSetting oldmask, const AdapterSetting newmask,
const AdapterSetting changedmask, const uint64_t timestamp) override {
JNIEnv *env = *jau::jni_env;
@@ -449,7 +434,7 @@ std::atomic<int> JNIAdapterStatusListener::iname_next(0);
jlong Java_org_direct_1bt_AdapterStatusListener_ctorImpl(JNIEnv *env, jobject obj) {
try {
// new instance
- jau::shared_ptr_ref<JNIAdapterStatusListener> ref( new JNIAdapterStatusListener(env, obj, nullptr) );
+ jau::shared_ptr_ref<JNIAdapterStatusListener> ref( new JNIAdapterStatusListener(env, obj) );
return ref.release_to_jlong();
} catch(...) {
diff --git a/src/direct_bt/BTAdapter.cpp b/src/direct_bt/BTAdapter.cpp
index 8725c684..0603b2ed 100644
--- a/src/direct_bt/BTAdapter.cpp
+++ b/src/direct_bt/BTAdapter.cpp
@@ -1615,7 +1615,7 @@ void BTAdapter::sendDeviceUpdated(std::string cause, BTDeviceRef device, uint64_
int i=0;
jau::for_each_fidelity(statusListenerList, [&](impl::StatusListenerPair &p) {
try {
- if( p.listener->matchDevice(*device) ) {
+ if( p.match(device) ) {
p.listener->deviceUpdated(device, updateMask, timestamp);
}
} catch (std::exception &e) {
@@ -1998,7 +1998,7 @@ bool BTAdapter::mgmtEvDeviceConnectedHCI(const MgmtEvent& e) noexcept {
int i=0;
jau::for_each_fidelity(statusListenerList, [&](impl::StatusListenerPair &p) {
try {
- if( p.listener->matchDevice(*device) ) {
+ if( p.match(device) ) {
if( EIRDataType::NONE != updateMask ) {
p.listener->deviceUpdated(device, updateMask, ad_report.getTimestamp());
}
@@ -2048,7 +2048,7 @@ bool BTAdapter::mgmtEvConnectFailedHCI(const MgmtEvent& e) noexcept {
int i=0;
jau::for_each_fidelity(statusListenerList, [&](impl::StatusListenerPair &p) {
try {
- if( p.listener->matchDevice(*device) ) {
+ if( p.match(device) ) {
p.listener->deviceDisconnected(device, event.getHCIStatus(), handle, event.getTimestamp());
}
} catch (std::exception &except) {
@@ -2148,7 +2148,7 @@ bool BTAdapter::mgmtEvDeviceDisconnectedHCI(const MgmtEvent& e) noexcept {
int i=0;
jau::for_each_fidelity(statusListenerList, [&](impl::StatusListenerPair &p) {
try {
- if( p.listener->matchDevice(*device) ) {
+ if( p.match(device) ) {
p.listener->deviceDisconnected(device, event.getHCIReason(), event.getHCIHandle(), event.getTimestamp());
}
} catch (std::exception &except) {
@@ -2395,7 +2395,7 @@ bool BTAdapter::mgmtEvDeviceFoundHCI(const MgmtEvent& e) noexcept {
bool device_used = false;
jau::for_each_fidelity(statusListenerList, [&](impl::StatusListenerPair &p) {
try {
- if( p.listener->matchDevice(*dev_shared) ) {
+ if( p.match(dev_shared) ) {
device_used = p.listener->deviceFound(dev_shared, eir->getTimestamp()) || device_used;
}
} catch (std::exception &except) {
@@ -2435,7 +2435,7 @@ bool BTAdapter::mgmtEvDeviceFoundHCI(const MgmtEvent& e) noexcept {
bool device_used = false;
jau::for_each_fidelity(statusListenerList, [&](impl::StatusListenerPair &p) {
try {
- if( p.listener->matchDevice(*dev_shared) ) {
+ if( p.match(dev_shared) ) {
device_used = p.listener->deviceFound(dev_shared, eir->getTimestamp()) || device_used;
}
} catch (std::exception &except) {
@@ -2474,7 +2474,7 @@ bool BTAdapter::mgmtEvDeviceFoundHCI(const MgmtEvent& e) noexcept {
bool device_used = false;
jau::for_each_fidelity(statusListenerList, [&](impl::StatusListenerPair &p) {
try {
- if( p.listener->matchDevice(*dev_discovered) ) {
+ if( p.match(dev_discovered) ) {
device_used = p.listener->deviceFound(dev_discovered, eir->getTimestamp()) || device_used;
}
} catch (std::exception &except) {
@@ -2617,7 +2617,7 @@ void BTAdapter::sendDevicePairingState(BTDeviceRef device, const SMPPairingState
int i=0;
jau::for_each_fidelity(statusListenerList, [&](impl::StatusListenerPair &p) {
try {
- if( p.listener->matchDevice(*device) ) {
+ if( p.match(device) ) {
p.listener->devicePairingState(device, state, mode, timestamp);
}
} catch (std::exception &except) {
@@ -2650,7 +2650,7 @@ void BTAdapter::sendDeviceReady(BTDeviceRef device, uint64_t timestamp) noexcept
try {
// Only issue if valid && received connected confirmation (HCI) && not have called disconnect yet.
if( device->isValidInstance() && device->getConnected() && device->allowDisconnect ) {
- if( p.listener->matchDevice(*device) ) {
+ if( p.match(device) ) {
p.listener->deviceReady(device, timestamp);
}
}