Tutorial to continuously measure the Bluetooth RSSI of a connected Android device (Java)
Bluetooth RSSI - Nexus7 & Nokia Xpress Music |
At the time of this writing, there is no Android API available to continuously retrieve the RSSI of an existing bluetooth connection even though API exists for getting WiFi RSSI. The current API will get the Bluetooth RSSI only during the initial connection setup process. In this article we will find out how to continuously get the Bluetooth RSSI of an Android device and a Nokia Mobile Phone from a computer running Linux.
The RSSI of any device connected to the computer can be determined by using the hcitool in Linux. But this may not be possible with commercial Android devices as root access is required in order to call any functions from the Bluetooth HCI layer using the Android NDK. For experimental purposes, in order to exploit the bluetooth equipment onboard commercial handheld devices, we shall measure the RSSI of these devices by connecting them to a computer or a laptop.
In this experiment the RSSI is being measured continuously in motion from an Android Device (Nexus 7 Tablet) and a Nokia Mobile Phone (Xpress Music) from a computer based on Ubuntu. The source code uses the Bluecove bluetooth library to extract the RSSI information from these connected devices. The Android device and the Nokia device acts like a server and the computer acts like a client.
At the computer, we need to write the client code that will continuously poll the RSSI from our known devices. In order to do this we need to first checkout the bluecove bluetooth libraries from here (http://bluecove.org/source-repository.html). Then we can make use of the BluetoothRSSIPollingClient.java to get the RSSI readings. We can filter out the other discovered devices using the Bluetooth MAC address of our known devices. We can obtain the Bluetooth MAC address of a device from the Preferences Tab of the Bluetooth Menu in Ubuntu after connecting the device with the computer.
public void PollRSSI()
{
try {
while(true)
{
try {
System.out.println();
if(Android_Device != null)
System.out.println("Android RSSI = " + RemoteDeviceHelper.readRSSI(Android_Device));
} catch (Exception e) { System.out.println("Android RSSI = Connection Error"); }
try {
if(Nokia != null)
System.out.println("Nokia RSSI = " + RemoteDeviceHelper.readRSSI(Nokia));
} catch (Exception e) { System.out.println("Nokia RSSI = Connection Error"); }
Thread.sleep(2000);
}
} catch (Exception e){ e.printStackTrace(); }
}
For the Android device we need to write our own server code in order to overcome the [13] Permission denied error. We might need to run more than one server thread (AcceptThread.java) on the Android device so the incoming connection request will be finally accepted after an initial permission denied error. We will also specify the RFCOMM UUID and a Service name which the client can search and connect to. The entire server has to be implemented as a Service in Android (BluetoothRSSIService.java) so that the connection is not lost if the display screen is timed out.
public AcceptThread()
{
BluetoothServerSocket tmp = null;
mBluetooth = BluetoothAdapter.getDefaultAdapter();
mUuid = UUID.fromString("00000003-0000-1000-8000-00805F9B34FB");
try {
tmp = mBluetooth.listenUsingInsecureRfcommWithServiceRecord("BluetoothCustomService", mUuid);
} catch (IOException e) { }
myServerSocket = tmp;
}
public void onCreate()
{
super.onCreate();
thread1 = new Thread(new AcceptThread());
thread1.start(); //First thread will often be denied
thread2 = new Thread(new AcceptThread());
thread2.start(); //Most probably be accepted
}
For the Nokia device there is no explicit server necessary and we can simply connect using the Bluetooth Serial Port Profile connection url. Once the connection is establish we can continuously poll the RSSI from both the devices periodically.
Note: RSSI of Bluetooth may not be an efficient and reliable parameter for applications such as indoor positioning
Source Code:
BluetoothRSSIPollingClient.java AcceptThread.java BluetoothRSSIService.java
References:
"A Bluetooth Based Supermarket Navigation System" - Pearl Manoharan, Vignesh Subramanian & Anusha Vutukuri - Course Project - Mobile Systems 16:332:559:02 F12 (Rutgers Fall 2012) -->
http://developer.android.com/guide/topics/connectivity/bluetooth.html
http://stackoverflow.com/questions/12251785/android-bluetooth-read-rssi-signal-strength
http://bluecove.org/bluecove-examples/bluecove-tester/index.html
6 comments:
I love this post! It's really helpfull to me ;)
But, I have some questions:
-The sample is about an Andorid device and a Linux Machine. Could this work for measuring the distance between two Android devices?
-Could you publish the eclipse project? Its is very easy to import than java files...
Thanks in advance
Thanks Josep. In order to measure the distance between two android devices you need to implement the Bluecove client on your android device but this requires sudo access to the device which may not be directly available on phones or tablets we buy from regular brands.
I remember seeing some android specific modules in the bluecove distribution but android recently converted to the Broadcomm stack for bluetooth and I'm not sure if it works.
is there any coding only for server. pc as server and sending advertisement list which is in 2d barcodes to clients mobile. there is no coding for clients mobile. only server has coding. is that possible?
Hello there,
this post was very helpful to me..
I'm trying to get data from my pc to android mobile through bluetooth..
Please send me your project folder in eclipse..
please mail me at krunal_717(at)yahoo.co.in
Thanks & regards,
Krunal
Thanks for info. Good Post.
Thank You!
Post a Comment