I managed, through the Nordic library for Android thingylib
(github) , to connect to thingy52 and receive in the callback listener ThingyListener
the information about the device (e.g. onGravityVectorChangedEvent
, onAccelerometerValueChangedEvent
, onGyroscopeValueChangedEvent
, and so on...).
What I can't do (and I can't find how to do) is how I can get the information about the connection with the device, exactly the RSSI values.
Using the BluetoothLeScannerCompat, inside the onBatchScanResults
scan callback, I detect the thingy52 bluetooth device, and their RSSI value, but if I set a high scan frequency, for example .setReportDelay(10)
, it almost always does not detect devices.
How I made scan:
private void startBLEScan() {
// set scan
ScanSettings scanSettings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
.setReportDelay(10) // set frequency
.setUseHardwareBatchingIfSupported(false)
.build();
// filter scan by uuid
List<no.nordicsemi.android.support.v18.scanner.ScanFilter> filters = new ArrayList<>();
filters.add(new ScanFilter.Builder().setServiceUuid(new ParcelUuid(ThingyUtils.THINGY_BASE_UUID)).build());
// start scan, this will trigger the scanCallback
BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
scanner.startScan(filters, scanSettings, scanCallback);
}
private final ScanCallback scanCallback = new ScanCallback() {
@Override
public void onBatchScanResults(@NonNull List<ScanResult> results) {
super.onBatchScanResults(results);
for(ScanResult result : results) {
result.getRssi(); // read RSSI values
}
}
};
My need is to get at a frequency of 10Hz
, or 100Hz
the RSSI value of the nordic thingy52, which I can't get from the ThingyListener
events.
How can I do it? Thank you in advance.
Example of my ThingyListener:
private ThingyListener mThingyListener = new ThingyListener() {
@Override
public void onGravityVectorChangedEvent(BluetoothDevice bluetoothDevice, float x, float y, float z) {
// Here I got the data from device... but no RSSI value
};
I finally solved in this way:
I don't use the ScannerCompact
, but the BluetoothGATT
:
when i link the nordic to the ThingyLibSdkManager
i also link it to a GATT
:
ThingyListenerHelper.registerThingyListener(getApplicationContext(), mThingyListener, device);
mBluetoothGatt = device.connectGatt(getApplicationContext(), true, gattCallback);
with that, inside the mThingylistener
now i can request the rssi
:
@Override
public void onGyroscopeValueChangedEvent(BluetoothDevice bluetoothDevice, float x, float y, float z) {
collectedData.put("onGyroscopeValueChangedEvent_x", x + "");
collectedData.put("onGyroscopeValueChangedEvent_y", y + "");
collectedData.put("onGyroscopeValueChangedEvent_z", z + "");
// request rssi from gatt
mBluetoothGatt.readRemoteRssi();
}
now, in async way, you will recieve the callback inside mBluetoothGatt
:
@Override
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
// do some stuff
}