How to get changing RSSI of BLE in Android - android

I am using Kontakt beacon device & testing ble on nexus 5. When I tested iPad as a beacon device I am able to find out the services & characteristics with the help of the code given on android developer site. But now I want to do some actions depending on the distance of the mobile from the beacon device. So I tried to detect how far is the mobile from the beacon device like iOS CLBeacon.Proximity (CLProximityFar, CLProximityNear, CLProximityImmideate & CLProximityUnknown). I searched but not able to find out like this in android. So decided to do some actions depending on the rssi. But problem is whatever rssi I am getting first time i.e at the time of mobile detect beacon device is not changing even if I move near to beacon device or went far away from it.
If any one had idea how to get the changing rssi in the android then please share.
Thanks.

I'm not sure how you checked the never changed rssi as you described, via a existed APP or writing codes by yourself?
but from the code, it's quite straight forward, I put a core code in Android 5.0,
//get your device's BT adapter
mBluetoothAdapter = ((BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter();
mLescanner = mBluetoothAdapter.getBluetoothLeScanner();
settings = new ScanSettings.Builder().setScanMode(scanSettings.SCAN_MODE_LOW_LATENCY).build();
mLescanner.startScan(this.mLeScanCallback);
And the callback is like this, and you can get the real time rssi in the function:
this.mLeScanCallback = new ScanCallback() {
#Override
public void onScanResult(int callbackType, ScanResult result) {
int realTimeRssiYouWant = result.getRssi();
}
}

Related

BLE: How to find exactly my device between scan results?

I'm a newbie on Android BLE programming.
So, I need a people who have a professional skills on a BLE concept :)
I made a app which connect between apple device and android phone.
apple device is slave, so apple send advertise message to android periodically.
But I have a little confused how to find my bluetooth device between scan results.
m_lescanner.startScan(filters, new ScanSettings.Builder().setScanMode(2).build(), m_scan_callback);
My app enroll broadcast receiver to listen bluetooth events.
If the events occurs, app start scanStart function to gathering BLE device near by android phone.
private ScanCallback m_scan_callback = new ScanCallback() {
#Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
After that, I got a many BLE device results including my BLE device(apple device) But I don't know what BLE results is mine....
all MAC address is different, and all UUID is null(I'm also confused why this flags is different and results is null).
Thank you for reading my question.
PLEASE HELP!!!

Using Bluetooth scanning for location accuracy in Marshmallow

With Android 4.3, Android implemented the idea of always-on WiFi where, even if you had Wi-Fi toggled off, the device and apps could still scan for WiFi networks to improve the location's accuracy. Along with using network triangulation, it's another way of getting your current position as quickly as possible without having to rely too much on GPS signals.
Android M is taking the idea further, adding Bluetooth scanning to the equation. Under the Location settings on M, you'll find a Scanning option in the menu, where both Wifi and Bluetooth scanning can be toggled on and off. When enabled, Bluetooth scanning will presumably look for BLE devices like beacons to get a quicker location fix.
Image resized. Click to view in full size
This may be very useful in the future inside malls, airports, and various indoor or underground locations where the reach and dispersion of Bluetooth beacons can outweigh a slow or impossible GPS signal lock. And the fact that it's always on, accessible whenever apps need a location fix, will make it even handier than if you had to remember to manually turn on Bluetooth.
Can anyone help in providing some insights or sample code for scanning for beacons with BLE without the main Bluetooth settings turned on?
I figured it out.
We have to write a system application and use the
BluetoothAdapter.enableBLE()
method.
This method is for special/system apps which use Bluetooth Low energy to scan for nearby devices which is mostly used for location accuracy.Even if the Bluetooth is turned off in the device settings.
Then we can use
BluetoothAdapter.LeScanCallback
callback to get the device details.
Sample:
for calling the method:
mBluetoothAdapter.enableBLE())
for callback:
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
#Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
if( device == null ){
System.out.println("-------onLeScan "+device);
}
runOnUiThread(new Runnable() {
#Override
public void run() {
mLeDeviceListAdapter.addDevice(device);
mLeDeviceListAdapter.notifyDataSetChanged();
}
});
}
};
Thanks

Does BLE support pinging?

Is BLE supporting pinging, i.e. sending a message to other devices and receiving their status?
I want to write an app on Android, which will ping each of the nearby devices and calculate the time between sending time and receiving answer, is it possible?
As you pointed out in your comment you like to use as time-of-arrival or time-difference-of-arrival algorithm to calculate the distance based on bluetooth.
To my knowledge this is currently more a theoretical approach in a bluetooth environment. As radio signals travel at light speed (~29.979 cm in one nanosecond) you will need a high sampling resolution to get a accurate result. Each nanosecound deviation will cause an error of roughly 30 cm.
With WiFi this is accomplished with a specalized chipset. To my knowledge this is currently not possibile with android as it would need a lot of low level support (Chipset and OS)
Hope this helps!
here are some sources
Android relative positioning, Wifi:Time of Arrival
Evaluation of indoor positioning based on
Bluetooth Smart technology - page 76
You can easily see the timestamp of discovery for each device in your onScanResult method:
#Override
public void onScanResult(int callbackType, ScanResult result){
Long lastSeen = result.getTimestampNanos();
//rest of your code
}
See the Android Documentation. You can use this timestamp and the time that you started your scan to get an approximate response time for each device.

how to get the rssi value for a bluetooth low energy device ?

I am new with android programming and trying to get rssi value form a BLE device for distance measurements.i can scan and get the name and mac address of the device but i've tried codes to get the rssi but can't get useful result,also i use the sample on the android developer site.
can someone give me the right code to do so??
Two solution for this.There is different approach one can have for 4.0 and 5.0 devices to search/scan BLE devices. You did not mentioned which one you are using, hence adding both the solution below.
1) for Android 4.4 + till 5.0, you have to start LE scanning via BluetoothAdapter's startLEScan method, which gives you below callback with RSSI value. see signature of method.
onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord)
// second param above is RSSI value.
2) For android 5.0+ devices you have to start scanning by BluetoothLeScanner class's startScan method, like below
getBluetoothLeScanner().startScan
which has callback onScanResult to notify for new scanned device, you can use below code to get rssi.
public void onScanResult(int callbackType, ScanResult result) {
final int rssi = result.getRssi(); //RSSI value

Detecting another nearby android device via Bluetooth

Alright, I've got a bit of a weird question here. I'm working on an Android game where I'd like to be able to have Android phones detect the presence of each other.
The device searching for other players will know the bluetooth mac addresses of the other players' devices (from a game DB), however the devices will not be paired and the devices will not be in discoverable mode. Also, there will only be a handful of devices that could possibly be found - so it's not a big deal to scan through mac addresses.
I don't need to connect to the devices, I just need to be able to answer one simple question: is this device with this mac address nearby?
It is permissible to have a pairing dialog appear on the other user's screen...I don't care what the outcome of their choice is...I just need to know if their device is there.
Any help would be greatly appreciated!
This use-case may be a good fit for the recently released Nearby API. See the Nearby Messages developer overview
Nearby has its own runtime permission saving you from adding BLUETOOTH_ADMIN or similar to your manifest. It works across iOS and Android by utilizing multiple technologies (Classic Bluetooth, BLE, ultrasound). There's an option to use only the ultrasonic modem which reduces the range to about 5 feet.
I've included a partial example below, you can find a more complete sample on github
// Call this when the user clicks "find players" or similar
// In the ResultCallback you'll want to trigger the permission
// dialog
Nearby.Messages.getPermissionStatus(client)
.setResultCallback(new ResultCallback<Status>() {
public void onResult(Status status) {
// Request Nearby runtime permission if missing
// ... see github sample for details
// If you already have the Nearby permission,
// call publishAndSubscribe()
}
});
void publishAndSubscribe() {
// You can put whatever you want in the message up to a modest
// size limit (currently 100KB). Smaller will be faster, though.
Message msg = "your device identifier/MAC/etc.".getBytes();
Nearby.Messages.publish(googleApiClient, msg)
.setResultCallback(...);
MessageListener listener = new MessageListener() {
public void onFound(Message msg) {
Log.i(TAG, "You found another device " + new String(msg));
}
});
Nearby.Messages.subscribe(googleApiClient, listener)
.setResultCallback(...);
}
Disclaimer I work on the Nearby API

Categories

Resources