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.