bglib
bglib copied to clipboard
Help for using bglib
Hello,
I'm trying to use your library to connect to a BLE device, but I can't figure how to read attributes.
I could connect to my port com and scan for devices, and I think I can connect to the device I want, but I can not read attributes.
I use bgapi.send_gap_connect_direct(sender, 1, 0x20, 0x3C, 0x64, 0);
to connect to my device, and that leads to receive_gap_connect_direct
callback. The problem is that this callback does not give a connection number and the callback receive_connection_status
that seems to be the only callback that give the connection number is never called.
And then, which method should I call to read an attribute by uuid?
Could you tell me what I do wrong and help me, please?
Thank you
@damien-raemy Hi, I am trying to integrate a BLE device in a Java application. Did you find a way to do it? Or any other help is also appreciated.
Thanks!
Hi, sorry, I see your question only now.
First, be sure you use a BLED112 device, otherwise the library won't work.
Then, my problem was, if I remember correctly, that I did not pass corrects arguments to send_gap_connect_direct
. You get the values in the receive_gap_scan_response
callback. The intervals are available in the data. I made an object to get the data:
AdvertisingData(byte[] data) {
for (int i = 0; i < data.length; /*increment made in loop*/) {
byte length = data[i];
byte type = data[i + 1];
switch (type) {
case DEVICE_NAME:
byte[] deviceNameBytes = Arrays.copyOfRange(data,
i + 2, i + length + 1);
deviceName = new String(deviceNameBytes);
break;
case SLAVE_CONNECTION_INTERVAL_RANGE:
// Intervals seems to be in little endian
connectionIntervalMin = (((int) data[i + 3]) << 8)
+ ((int) data[i + 2]);
connectionIntervalMax = (((int) data[i + 5]) << 8)
+ data[i + 4];
break;
}
i += length + 1; // +1 because length field is not part of length
}
}
For the two last arguments, I hard-coded 700 and 0, I don't remember why. Finally, here is how I call the method: bgapi.send_gap_connect_direct(address, addressType, advertisingData.getConnectionIntervalMin(), advertisingData.getConnectionIntervalMax(), 700, 0);
Then, I receive the connection number by the receive_connection_status
method.
I forgot to include the constants :
/**
* Type that indicate that the data is the device name
*/
private static final int DEVICE_NAME = 0x09;
/**
* Type that indicate that the section is the connection interval range
*/
private static final int SLAVE_CONNECTION_INTERVAL_RANGE = 0x12;