1

I have been trying to alter the RedBearLab Chat program https://github.com/RedBearLab/Android/tree/master/Examples/Android%20Studio%20Examples/Chat in Android Studio, to send my own data to the BLE shield I have on my Arduino Uno.

While the RedBearLab app from the appstore shows the Rssi value of the devices the Bluetooth low enegery finds, the Chat example, which is only a part of the whole app you can get on the appstore, only shows the device name and the address.

After looking at a couple of different solutions to get the rssi value, I found that there are two ways of getting these values:
- while scanning for devices with he lescan callback function;
- after a connection has been established between the phone and the device.

Now for what I would like to know is how to get the rssi value out of the lescan callback function. I have found different solutions whereby the value was send with the device name to another function to (usually) a different class where it is attached to a textfield.

When this value is known to me, I'd like to use it to narrow the devices that the app finds by only adding devices above a certain rssi value (let's say lower than -70 is too far away).

private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {

private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {

    @Override
    public void onLeScan(final BluetoothDevice device, final int rssi,
            byte[] scanRecord) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (device != null) {
                    if (mDevices.indexOf(device) == -1)
                        mDevices.add(device);
                }
            }
        });
    }
};

The above code is in the Main.java.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.device_list);

    setTitle("Device");

    listView = (ListView) findViewById(R.id.listView);

    devices = (ArrayList<BluetoothDevice>) Main.mDevices;
    for (BluetoothDevice device : devices) {
        map = new HashMap<String, String>();
        map.put(DEVICE_NAME, device.getName());
        map.put(DEVICE_ADDRESS, device.getAddress());
        listItems.add(map);
    }

    adapter = new SimpleAdapter(getApplicationContext(), listItems,
            R.layout.list_item, new String[] { "name", "address" },
            new int[] { R.id.deviceName, R.id.deviceAddr });
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(this);
}

The above code is in the Device.java.

I'm sure there is someone here who knows how to handle this! Thanks for looking into my post, for any who have read my post.

Edit:

I found a tutorial on youtube, he posted his code on Github, who uses the same BluetoothAdapter.LeScanCallback, but it's a little different. https://github.com/kaviles/BLE_Tutorials/blob/master/Android/BLE_Tutorial_Final/app/src/main/java/android/kaviles/bletutorial/Scanner_BTLE.java

// Device scan callback.
private BluetoothAdapter.LeScanCallback mLeScanCallback =
        new BluetoothAdapter.LeScanCallback() {

            @Override
            public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {

                final int new_rssi = rssi;
                if (rssi > signalStrength) {
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            ma.addDevice(device, new_rssi);
                        }
                    });
                }
            }
        };

So I tried implementing this into the Chat code from RedBearLab, but it doesn't work and I don't get how it works in the youtube tutorial.

gjthegrave
  • 11
  • 3

1 Answers1

0

Look on stackoverflow, very probably the question has already been answered. how to get the rssi value for a bluetooth low energy device ?

Community
  • 1
  • 1
R. Zagórski
  • 20,020
  • 5
  • 65
  • 90
  • Yes, so this code uses the first solutions, but only gets out the device name (and address too, somehow i don't fully understand). Now I get that the second value is the rssi value I want, what I don't get is how to get it out and send it with the device name to the Device,java class where it is put in the arraylist. – gjthegrave Jun 28 '16 at 07:38
  • How to get RSSI value? Use `getRssi()` function from `ScanResult` object. An how to send it back? Define an interface, whose function will be invoked, when the result it found and send it to `Device.java` class. – R. Zagórski Jun 28 '16 at 07:39
  • Isn't getRssi() from the second solution, which probably won't work when already using the first solution to get the device name? I tried putting in the onScanResults but my code doesn't know the method getRssi(). – gjthegrave Jun 28 '16 at 07:53
  • After further diving into Android BluetoothLE API, I found that there are to ways. In API >= 17 the callback, when the device is found, is `public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord)`. You can get device name by calling `device.getName()` and RSSI value is provided as the argument of function. On API >= 21 the callback is different `public void onScanResult(int callbackType, ScanResult result)`. To get a name call `result.getScanRecord().getDeviceName()` and RSSI value `result.getRssi()`. – R. Zagórski Jun 28 '16 at 08:05
  • The Chat code is using the API >= 17 callback, it seems. What I found was https://developer.android.com/reference/android/bluetooth/BluetoothDevice.html where you'll find the callback explained a little. When you search for rssi (ctrl+f) EXTRA_RSSI is mentioned and ACTION_FOUND. I'm not sure if this is a clue as how to solve it, as I have no idea how to implement it. – gjthegrave Jun 28 '16 at 08:22
  • I give you the link to the official google sample of how to implement bluetoothLE: https://android.googlesource.com/platform/developers/samples/android/+/marshmallow-release/connectivity/bluetooth/BluetoothLeGatt/ Maybe this will help you. – R. Zagórski Jun 28 '16 at 08:27
  • Ah yes, BluetoothLeGatt proved to be an okay example when I used it a few weeks ago. It doesn't, however do anything with rssi. With a bit of searching I found a modified version of BluetoothLeGatt where rssi was implemented and I got it working. Then I couldn't send data which was when I discovered the RedBearLab chat app, which was what I wanted to use (as it has pretty much all the functions I needed in the original app). But that brough me back to the issue I have now. Also in order to use BluetoothLeGatt I needed to implement some permission code (for android 6 update). – gjthegrave Jun 28 '16 at 08:58