Java Question How to share a Custom Type between B4A and java library?

TurboCCC

Member
Licensed User
I created a customer type charStruct in my Java library and would like to use it in B4A.

Java:
public class charStruct {
        String uuid;
        int properties;
        int permissions;
    }

B4X:
Type charStruct (uuid As String, properties As Int, permissions As Int)

Compiler complains it can't match both types but it knows they are related.

Compiling generated Java code. Error
B4A line: 31
peripheral.Start2(\
src\b4a\bbb\ble_peripheral.java:230: error: incompatible types: _charstruct cannot be converted to BlePeripheral3.charStruct
__ref._peripheral /*anywheresoftware.b4a.objects.BlePeripheral3*/ .Start2("RunCline",(android.bluetooth.le.AdvertiseSettings)(__ref._createadvertisesettings /*Object*/ (null)),(anywheresoftware.b4a.objects.BlePeripheral3.charStruct)(_charinfo));


What gives?
Thanks.
 
Last edited:

TurboCCC

Member
Licensed User
Wow! I made this work by adding a shortname in front of it.

Java:
@ShortName("charStruct")
    public static class charStruct {
        public String uuid;
        public int properties;
        public int permissions;
    }

There is still an error in eclipse saying No ShortName annotation found for class: BlePeripheral3.charStruct but it does work. I believe I need to understand more the usage of the @ShortName.
 
Last edited:

TurboCCC

Member
Licensed User
While the above worked, I'd like to pass an array of charStruct. The same trick does not seem to work. Code looks like this:

B4X:
...
    Dim charInfo(2) As charStruct
    charInfo(0).uuid = "1002"
    charInfo(0).permissions = peripheral.PERMISSION_WRITE
    charInfo(0).properties = peripheral.PROPERTY_WRITE
    charInfo(1).uuid = "1009"
    charInfo(1).permissions = peripheral.PERMISSION_WRITE
    charInfo(1).properties = peripheral.PROPERTY_WRITE
    peripheral.Start2("RunCline", CreateAdvertiseSettings, charInfo)
...

Java:
@ShortName("charStruct")
    public static class charStruct {
        public String uuid;
        public int properties;
        public int permissions;
    }

public void Start2(String Name, AdvertiseSettings Settings, charStruct charInfo[]) {
...
}
 

TurboCCC

Member
Licensed User
It should work. Which error do you get?
Thanks Erel. You are correct. Sleeping helped. It worked on the first attempt this morning. Still, I tried this in multiple forms last night. I can't believe I missed. Possibly forgot a library update somewhere and it sent me on the wrong track.
 

TurboCCC

Member
Licensed User
Not sure if this is nice or not but decided to go another way. I prefer it to a big structure initialization.

B4X:
peripheral.AddServiceCharacteristic("1001", "1001", peripheral.PERMISSION_READ, Bit.Or(peripheral.PROPERTY_READ,peripheral.PROPERTY_NOTIFY))
peripheral.AddServiceCharacteristic("1001", "1002", peripheral.PERMISSION_WRITE, peripheral.PROPERTY_WRITE)
peripheral.AddServiceCharacteristic("1001", "1003", peripheral.PERMISSION_WRITE, peripheral.PROPERTY_WRITE)
peripheral.AddServiceCharacteristic("1002", "1004", peripheral.PERMISSION_WRITE, peripheral.PROPERTY_WRITE)
peripheral.Start2("RunCline", CreateAdvertiseSettings)

This works well for now.
 

TurboCCC

Member
Licensed User
I will. I am currently investigating an issue with the existing library: whatever you write to characteristic 0002 (write) is always read on characteristic 0001 (read, notify). It seems the characteristic.getValue() in onCharacteristicReadRequest(...) is always returning the last thing that was written to any characteristic. It has no impact on the BLE Example but it is important that it does not do that.

Java:
@Override
public void onCharacteristicReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattCharacteristic characteristic) {
    gattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, 0, characteristic.getValue() == null ? new byte[8] : characteristic.getValue());
}
 
Last edited:

TurboCCC

Member
Licensed User
LOLOLOL! That's the normal behavior of the BLE_Peripheral.BA4 code that echos what was written to in Peripheral_NewData. I was so focused on the java section I forgot to check what the B4A code was doing. 🤦‍♂️ I lost so much time on this.
 
Last edited:
Top