Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Further cleanup of Android code. #361

Merged
merged 3 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/simpleble-android/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[versions]
agp = "8.3.1"
agp = "8.7.1"
kotlin = "1.9.23"
coreKtx = "1.12.0"
appcompat = "1.6.1"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Sat Apr 27 08:22:16 PDT 2024
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
28 changes: 9 additions & 19 deletions simpleble/src/backends/android/PeripheralBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
#include "CommonUtils.h"
#include "LoggingInternal.h"

#include <android/log.h>

using namespace SimpleBLE;
using namespace std::chrono_literals;

Expand All @@ -21,7 +19,7 @@ PeripheralBase::PeripheralBase(Android::ScanResult scan_result) : _device(scan_r
_gatt.discoverServices();
} else {
// TODO: Whatever cleanup is necessary when disconnected.
__android_log_write(ANDROID_LOG_INFO, "SimpleBLE", "Disconnected from device");
SIMPLEBLE_LOG_INFO("Disconnected from device");
SAFE_CALLBACK_CALL(callback_on_disconnected_);
}
});
Expand Down Expand Up @@ -100,8 +98,7 @@ std::vector<Service> PeripheralBase::advertised_services() { return std::vector<
std::map<uint16_t, ByteArray> PeripheralBase::manufacturer_data() { return std::map<uint16_t, ByteArray>(); }

ByteArray PeripheralBase::read(BluetoothUUID const& service, BluetoothUUID const& characteristic) {
auto msg = "Reading characteristic " + characteristic;
__android_log_write(ANDROID_LOG_INFO, "SimpleBLE", msg.c_str());
SIMPLEBLE_LOG_INFO("Reading characteristic " + characteristic);

auto characteristic_obj = _fetch_characteristic(service, characteristic);

Expand All @@ -116,8 +113,7 @@ ByteArray PeripheralBase::read(BluetoothUUID const& service, BluetoothUUID const

void PeripheralBase::write_request(BluetoothUUID const& service, BluetoothUUID const& characteristic,
ByteArray const& data) {
auto msg = "Writing request to characteristic " + characteristic;
__android_log_write(ANDROID_LOG_INFO, "SimpleBLE", msg.c_str());
SIMPLEBLE_LOG_INFO("Writing request to characteristic " + characteristic);

auto characteristic_obj = _fetch_characteristic(service, characteristic);

Expand All @@ -133,8 +129,7 @@ void PeripheralBase::write_request(BluetoothUUID const& service, BluetoothUUID c

void PeripheralBase::write_command(BluetoothUUID const& service, BluetoothUUID const& characteristic,
ByteArray const& data) {
auto msg = "Writing command to characteristic " + characteristic;
__android_log_write(ANDROID_LOG_INFO, "SimpleBLE", msg.c_str());
SIMPLEBLE_LOG_INFO("Writing command to characteristic " + characteristic);

auto characteristic_obj = _fetch_characteristic(service, characteristic);

Expand All @@ -150,8 +145,7 @@ void PeripheralBase::write_command(BluetoothUUID const& service, BluetoothUUID c

void PeripheralBase::notify(BluetoothUUID const& service, BluetoothUUID const& characteristic,
std::function<void(ByteArray payload)> callback) {
auto msg = "Subscribing to characteristic " + characteristic;
__android_log_write(ANDROID_LOG_INFO, "SimpleBLE", msg.c_str());
SIMPLEBLE_LOG_INFO("Subscribing to characteristic " + characteristic);

auto characteristic_obj = _fetch_characteristic(service, characteristic);
auto descriptor_obj = _fetch_descriptor(service, characteristic,
Expand All @@ -177,8 +171,7 @@ void PeripheralBase::notify(BluetoothUUID const& service, BluetoothUUID const& c

void PeripheralBase::indicate(BluetoothUUID const& service, BluetoothUUID const& characteristic,
std::function<void(ByteArray payload)> callback) {
auto msg = "Subscribing to characteristic " + characteristic;
__android_log_write(ANDROID_LOG_INFO, "SimpleBLE", msg.c_str());
SIMPLEBLE_LOG_INFO("Subscribing to characteristic " + characteristic);

auto characteristic_obj = _fetch_characteristic(service, characteristic);
auto descriptor_obj = _fetch_descriptor(service, characteristic,
Expand All @@ -203,8 +196,7 @@ void PeripheralBase::indicate(BluetoothUUID const& service, BluetoothUUID const&
}

void PeripheralBase::unsubscribe(BluetoothUUID const& service, BluetoothUUID const& characteristic) {
auto msg = "Unsubscribing from characteristic " + characteristic;
__android_log_write(ANDROID_LOG_INFO, "SimpleBLE", msg.c_str());
SIMPLEBLE_LOG_INFO("Unsubscribing from characteristic " + characteristic);

auto characteristic_obj = _fetch_characteristic(service, characteristic);
auto descriptor_obj = _fetch_descriptor(service, characteristic,
Expand All @@ -226,8 +218,7 @@ void PeripheralBase::unsubscribe(BluetoothUUID const& service, BluetoothUUID con

ByteArray PeripheralBase::read(BluetoothUUID const& service, BluetoothUUID const& characteristic,
BluetoothUUID const& descriptor) {
auto msg = "Reading descriptor " + descriptor;
__android_log_write(ANDROID_LOG_INFO, "SimpleBLE", msg.c_str());
SIMPLEBLE_LOG_INFO("Reading descriptor " + descriptor);

auto descriptor_obj = _fetch_descriptor(service, characteristic, descriptor);

Expand All @@ -242,8 +233,7 @@ ByteArray PeripheralBase::read(BluetoothUUID const& service, BluetoothUUID const

void PeripheralBase::write(BluetoothUUID const& service, BluetoothUUID const& characteristic,
BluetoothUUID const& descriptor, ByteArray const& data) {
auto msg = "Writing descriptor " + descriptor;
__android_log_write(ANDROID_LOG_INFO, "SimpleBLE", msg.c_str());
SIMPLEBLE_LOG_INFO("Writing descriptor " + descriptor);

auto descriptor_obj = _fetch_descriptor(service, characteristic, descriptor);

Expand Down
15 changes: 13 additions & 2 deletions simpleble/src/backends/android/android/BluetoothDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,22 @@ BluetoothDevice::BluetoothDevice(JNI::Object obj) : _obj(obj) {
initialize();
};

std::string BluetoothDevice::getAddress() { return _obj.call_string_method(_method_getAddress); }
void BluetoothDevice::check_initialized() const {
if (!_obj) throw std::runtime_error("BluetoothDevice is not initialized");
}

std::string BluetoothDevice::getAddress() {
check_initialized();
return _obj.call_string_method(_method_getAddress);
}

std::string BluetoothDevice::getName() { return _obj.call_string_method(_method_getName); }
std::string BluetoothDevice::getName() {
check_initialized();
return _obj.call_string_method(_method_getName);
}

BluetoothGatt BluetoothDevice::connectGatt(bool autoConnect, Bridge::BluetoothGattCallback& callback) {
check_initialized();
return BluetoothGatt(_obj.call_object_method(_method_connectGatt, nullptr, autoConnect, callback.get()));
}

Expand Down
2 changes: 1 addition & 1 deletion simpleble/src/backends/android/android/BluetoothDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class BluetoothDevice {
static jmethodID _method_connectGatt;

static void initialize();

void check_initialized() const;
JNI::Object _obj;

};
Expand Down
25 changes: 15 additions & 10 deletions simpleble/src/backends/android/android/BluetoothGatt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,32 +69,37 @@ BluetoothGatt::BluetoothGatt() { initialize(); }

BluetoothGatt::BluetoothGatt(JNI::Object obj) : BluetoothGatt() { _obj = obj; }


void BluetoothGatt::check_initialized() const {
if (!_obj) throw std::runtime_error("BluetoothGatt is not initialized");
}

void BluetoothGatt::close() {
if (!_obj) return;
check_initialized();

_obj.call_void_method(_method_close);
}

bool BluetoothGatt::connect() {
if (!_obj) return false;
check_initialized();

return _obj.call_boolean_method(_method_connect);
}

void BluetoothGatt::disconnect() {
if (!_obj) return;
check_initialized();

_obj.call_void_method(_method_disconnect);
}

bool BluetoothGatt::discoverServices() {
if (!_obj) return false;
check_initialized();

return _obj.call_boolean_method(_method_discoverServices);
}

std::vector<BluetoothGattService> BluetoothGatt::getServices() {
if (!_obj) return std::vector<BluetoothGattService>();
check_initialized();

JNI::Object services = _obj.call_object_method("getServices", "()Ljava/util/List;");
if (!services) return std::vector<BluetoothGattService>();
Expand All @@ -113,31 +118,31 @@ std::vector<BluetoothGattService> BluetoothGatt::getServices() {
}

bool BluetoothGatt::readCharacteristic(BluetoothGattCharacteristic characteristic) {
if (!_obj) return false;
check_initialized();

return _obj.call_boolean_method(_method_readCharacteristic, characteristic.getObject().get());
}

bool BluetoothGatt::readDescriptor(BluetoothGattDescriptor descriptor) {
if (!_obj) return false;
check_initialized();

return _obj.call_boolean_method(_method_readDescriptor, descriptor.getObject().get());
}

bool BluetoothGatt::setCharacteristicNotification(BluetoothGattCharacteristic characteristic, bool enable) {
if (!_obj) return false;
check_initialized();

return _obj.call_boolean_method(_method_setCharacteristicNotification, characteristic.getObject().get(), enable);
}

bool BluetoothGatt::writeCharacteristic(BluetoothGattCharacteristic characteristic) {
if (!_obj) return false;
check_initialized();

return _obj.call_boolean_method(_method_writeCharacteristic, characteristic.getObject().get());
}

bool BluetoothGatt::writeDescriptor(BluetoothGattDescriptor descriptor) {
if (!_obj) return false;
check_initialized();

return _obj.call_boolean_method(_method_writeDescriptor, descriptor.getObject().get());
}
Expand Down
2 changes: 1 addition & 1 deletion simpleble/src/backends/android/android/BluetoothGatt.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class BluetoothGatt {
static jmethodID _method_writeDescriptor;

static void initialize();

void check_initialized() const;
JNI::Object _obj;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ void BluetoothGattCharacteristic::initialize() {
}
}

void BluetoothGattCharacteristic::check_initialized() const {
if (!_obj) throw std::runtime_error("BluetoothGattCharacteristic is not initialized");
}

BluetoothGattCharacteristic::BluetoothGattCharacteristic() { initialize(); }

BluetoothGattCharacteristic::BluetoothGattCharacteristic(JNI::Object obj) : BluetoothGattCharacteristic() {
Expand All @@ -93,10 +97,10 @@ BluetoothGattCharacteristic::BluetoothGattCharacteristic(JNI::Object obj) : Blue
// }
//
std::vector<BluetoothGattDescriptor> BluetoothGattCharacteristic::getDescriptors() {
if (!_obj) return std::vector<BluetoothGattDescriptor>();
check_initialized();

JNI::Object descriptors = _obj.call_object_method(_method_getDescriptors);
if (!descriptors) return std::vector<BluetoothGattDescriptor>();
if (!descriptors) throw std::runtime_error("Failed to get descriptors");

std::vector<BluetoothGattDescriptor> result;
JNI::Object iterator = descriptors.call_object_method("iterator", "()Ljava/util/Iterator;");
Expand All @@ -110,28 +114,43 @@ std::vector<BluetoothGattDescriptor> BluetoothGattCharacteristic::getDescriptors
return result;
}

int BluetoothGattCharacteristic::getInstanceId() { return _obj.call_int_method(_method_getInstanceId); }
int BluetoothGattCharacteristic::getInstanceId() {
check_initialized();
return _obj.call_int_method(_method_getInstanceId);
}

int BluetoothGattCharacteristic::getPermissions() { return _obj.call_int_method(_method_getPermissions); }
int BluetoothGattCharacteristic::getPermissions() {
check_initialized();
return _obj.call_int_method(_method_getPermissions);
}

int BluetoothGattCharacteristic::getProperties() { return _obj.call_int_method(_method_getProperties); }
int BluetoothGattCharacteristic::getProperties() {
check_initialized();
return _obj.call_int_method(_method_getProperties);
}

std::string BluetoothGattCharacteristic::getUuid() {
if (!_obj) return "";
check_initialized();

JNI::Object uuidObj = _obj.call_object_method(_method_getUuid);
if (!uuidObj) return "";
if (!uuidObj) throw std::runtime_error("Failed to get UUID");

return UUID(uuidObj).toString();
}

int BluetoothGattCharacteristic::getWriteType() { return _obj.call_int_method(_method_getWriteType); }
int BluetoothGattCharacteristic::getWriteType() {
check_initialized();
return _obj.call_int_method(_method_getWriteType);
}

void BluetoothGattCharacteristic::setWriteType(int writeType) {
check_initialized();
_obj.call_void_method(_method_setWriteType, writeType);
}

bool BluetoothGattCharacteristic::setValue(const std::vector<uint8_t>& value) {
check_initialized();

JNI::Env env;
jbyteArray array = JNI::Types::toJByteArray(value);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@ namespace SimpleBLE {
namespace Android {

class BluetoothGattCharacteristic {
// See: https://developer.android.com/reference/android/bluetooth/BluetoothGattCharacteristic
public:
BluetoothGattCharacteristic();
BluetoothGattCharacteristic(JNI::Object obj);

// bool addDescriptor(BluetoothGattDescriptor descriptor);
// BluetoothGattDescriptor getDescriptor(std::string uuid);
std::vector<BluetoothGattDescriptor> getDescriptors();

int getInstanceId();
int getPermissions();
int getProperties();
std::string getUuid();
int getWriteType();
void setWriteType(int writeType);

bool setValue(const std::vector<uint8_t>& value);

JNI::Object getObject() const { return _obj; }
Expand Down Expand Up @@ -50,7 +53,8 @@ class BluetoothGattCharacteristic {
static jmethodID _method_setWriteType;
static jmethodID _method_setValue;

void initialize();
static void initialize();
void check_initialized() const;
};

} // namespace Android
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,31 @@ void BluetoothGattDescriptor::initialize() {
}
}

void BluetoothGattDescriptor::check_initialized() const {
if (!_obj) throw std::runtime_error("BluetoothGattDescriptor is not initialized");
}

BluetoothGattDescriptor::BluetoothGattDescriptor() { initialize(); }

BluetoothGattDescriptor::BluetoothGattDescriptor(JNI::Object obj) : BluetoothGattDescriptor() { _obj = obj; }

std::string BluetoothGattDescriptor::getUuid() {
if (!_obj) return "";
check_initialized();

JNI::Object uuidObj = _obj.call_object_method(_method_getUuid);
if (!uuidObj) return "";
if (!uuidObj) throw std::runtime_error("Failed to get UUID");

return UUID(uuidObj).toString();
}

std::vector<uint8_t> BluetoothGattDescriptor::getValue() {
if (!_obj) return {};
check_initialized();

return _obj.call_byte_array_method(_method_getValue);
}

bool BluetoothGattDescriptor::setValue(const std::vector<uint8_t> &value) {
if (!_obj) return false;
check_initialized();

JNI::Env env;
jbyteArray jbyteArray_obj = env->NewByteArray(value.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace SimpleBLE {
namespace Android {

class BluetoothGattDescriptor {
// See: https://developer.android.com/reference/android/bluetooth/BluetoothGattDescriptor
public:
BluetoothGattDescriptor();
BluetoothGattDescriptor(JNI::Object obj);
Expand All @@ -32,7 +33,8 @@ class BluetoothGattDescriptor {
static jmethodID _method_getValue;
static jmethodID _method_setValue;

void initialize();
static void initialize();
void check_initialized() const;
};

} // namespace Android
Expand Down
Loading
Loading