You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Firstly, thank-you for this library - it is very helpful and much easier than using Java Native Interface!
I am attempting to write a Java BLE library which calls out to the native C functions in https://github.com/OpenBluetoothToolbox/SimpleBLE. The specific function I am having trouble with is "simpleble_peripheral_write_command" which takes in a simpleble_peripheral_t handle, two simpleble_uuid_t pointers to the service and characteristic uuids, const uint8_t pointer to the data to write, and a size_t data_length.
I have implemented this function in my "libsimpleble" public interface:
public class simpleBLE {
// Interface to define what functions we would like to use
public interface libSimpleBLE {
// from adapter.h
// https://github.com/OpenBluetoothToolbox/SimpleBLE/blob/v0.7.1/simpleble/include/simpleble_c/adapter.h
boolean simpleble_adapter_is_bluetooth_enabled();
...
// from peripheral.h
// https://github.com/OpenBluetoothToolbox/SimpleBLE/blob/v0.7.1/simpleble/include/simpleble_c/peripheral.h
boolean simpleble_peripheral_write_command(Pointer handle, Pointer service, Pointer characteristic, Pointer data, int length);
}
and have defined the function further in the file:
public boolean writeCommand(Pointer peripheralHandle, Pointer serviceUuid, Pointer characteristicUuid, Pointer data, int length)
{
return libsimpleble.simpleble_peripheral_write_command(peripheralHandle, serviceUuid, characteristicUuid, data, length);
}
But the C program is crashing (on a memcpy call) whenever I try to call this function. It is currently working for all functions where I do not need to pass a user-defined pointer, which leads me to believe that there is something wrong with how I am passing my pointer parameters.
// Adding null bytes as Java strings are not null terminated
char[] service = addNullByte("65333333-a115-11e2-9e9a-0800200ca100");
char[] characteristic = addNullByte("65333333-a115-11e2-9e9a-0800200ca102");
// Data to send
ByteBuffer buffer = ByteBuffer.allocateDirect(3);
buffer.put(0x8a);
buffer.put(0x03);
buffer.put(0xff);
// Get the runtime in order to generate the custom structs
Runtime runtime = simpleBLE.libsimplebleRuntime();
simpleble_uuid_t service_uuid = new simpleble_uuid_t(runtime);
service_uuid.set(service);
Pointer service_p = Struct.getMemory(service_uuid);
simpleble_uuid_t characteristic_uuid = new simpleble_uuid_t(runtime);
characteristic_uuid.set(characteristic);
Pointer characteristic_p = Struct.getMemory(characteristic_uuid);
Pointer data_p = Pointer.wrap(runtime, buffer);
System.out.println("Data Pointer addr: " + data_p.address());
// Crashes on this call
simpleBLEwriteCommand(peripheral, service_p, characteristic_p, data_p, 3);
System.out.println("Connected!");
When I print out both the service_p.address() and characteristic_p.address these give me "0" which I think is my problem, however I have no idea how to allocate these structs in a memory address which the native library has access to? I have set the data_p using ByteBuffer.allocateDirect so I am hoping that this is okay.
Wondering if anyone has experience in passing user defined JNI-FFI Structs and pointers to data to native functions with success and could let me know where I am going wrong?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi all,
Firstly, thank-you for this library - it is very helpful and much easier than using Java Native Interface!
I am attempting to write a Java BLE library which calls out to the native C functions in https://github.com/OpenBluetoothToolbox/SimpleBLE. The specific function I am having trouble with is "simpleble_peripheral_write_command" which takes in a
simpleble_peripheral_t
handle, twosimpleble_uuid_t
pointers to the service and characteristic uuids,const uint8_t
pointer to the data to write, and asize_t
data_length.SIMPLEBLE_EXPORT simpleble_err_t simpleble_peripheral_write_command(simpleble_peripheral_t handle, simpleble_uuid_t service, simpleble_uuid_t characteristic, const uint8_t* data, size_t data_length);
I have implemented this function in my "libsimpleble" public interface:
and have defined the function further in the file:
But the C program is crashing (on a memcpy call) whenever I try to call this function. It is currently working for all functions where I do not need to pass a user-defined pointer, which leads me to believe that there is something wrong with how I am passing my pointer parameters.
When I print out both the
service_p.address()
andcharacteristic_p.address
these give me "0" which I think is my problem, however I have no idea how to allocate these structs in a memory address which the native library has access to? I have set the data_p usingByteBuffer.allocateDirect
so I am hoping that this is okay.Wondering if anyone has experience in passing user defined JNI-FFI Structs and pointers to data to native functions with success and could let me know where I am going wrong?
Thanks
Beta Was this translation helpful? Give feedback.
All reactions