Android 如何连续的测量蓝牙的RSSI

来源:互联网 发布:4399游戏刷盒币软件 编辑:程序博客网 时间:2024/05/18 01:25

 基于蓝牙的RSSI可以有很多应用,要获得蓝牙的RSSI无外乎两种方法,一种就是基于扫瞄的方法,优点是Android本身支持,缺点是scan的时间比较长,并且中间过程不受控制,为了连续的测量,需要不断的scan;第二种就是,基于连接的方法,前提是要建立两个蓝牙设备的连接后,再测量RSSI,优点是后期测量比较方便,间隔时间也较短。

  实现的方法,我就抛砖引玉了,有赶兴趣的再交流吧。

转自:http://techtitude.blogspot.com/2013/01/tutorial-to-continuously-measure.html

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.

Client:

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.

Server:

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

有TX问到,为什么RSSI=0,解释如下文,简单点就是这个值是设备相关的。

http://www.robomotic.com/android/bluetooth-rssi/


RSSI is an 8-bit signed integer that denotes whether the re-
ceived (RX) power level is within or above/below the Golden
Receiver Power Range (GRPR), which is regarded as the ideal
RX power range. Fig. 1 illustrates the relationship between
GRPR and RSSI, as defined in Bluetooth specification. A pos-
itive or negative RSSI (in dB) means the RX power level is
above or below the GRPR, respectively, while a zero implies
that it is ideal (i.e., within GRPR). The lower and upper thresh-
olds of GRPR are loosely bound, leaving them to be device-
specific. This, in turn, affects the RSSI, since it is merely a
relative parameter. In fact, its absolute accuracy is not man-
dated in the specification; the only requirement is to be able to
indicate whether it is within, above, or below the GRPR. The
RSSI status parameter of Bluetooth is particularly intended to
be used for power control purpose [6]. The receiver sends “in-
crease” or “decrease” TPL request to the transmitting side, de-
pending on whether the perceived RSSI at its side is negative
or positive, respectively.
原创粉丝点击