2013-03-09 137 views
27

我想它连接到我的电话的另一设备的蓝牙信号强度,获取蓝牙信号强度

我怎样才能获得蓝牙信号强度?

我试图搜索很多谷歌,并没有找到任何答案。

有人知道我该如何实现它吗?

这是myActivity:

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_FOUND)); 


    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    private final BroadcastReceiver receiver = new BroadcastReceiver(){ 
     @Override 
     public void onReceive(Context context, Intent intent) { 

      String action = intent.getAction(); 
      if(BluetoothDevice.ACTION_FOUND.equals(action)) { 
       int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE); 
       Toast.makeText(getApplicationContext()," RSSI: " + rssi + "dBm", Toast.LENGTH_SHORT).show(); 
      } 
     } 
    }; 

} 

我也有我的清单文件蓝牙权限。

+1

http://stackoverflow.com/questions/3020407/how-to-find-signal-strength-of-connected-bluetooth-devices – 2013-03-09 16:13:18

+1

我已经看到它,但是没有任何如何找到信号的例子力量..可能你能帮助我吗? – 2013-03-09 16:14:57

+0

我认为你的代码是好的。为了查看Toast,您需要先执行Discover。 – Memochipan 2013-08-04 22:40:03

回答

26

要获取信号,您可以检查蓝牙RSSI,您可以读取连接设备的RSSI,或执行蓝牙发现以检查附近任何设备的RSSI。

蓝牙发现基本上是广播范围内的所有站点回应。当每个设备回应时,Android会触发ACTION_FOUND的意图。在此意图内,您可以获得EXTRA EXTRA_RSSI以获取RSSI。

请注意,并非所有的蓝牙硬件都支持RSSI。

也关系:Android IRC Office Hours Question About Android Bluetooth RSSI 我这里是

private final BroadcastReceiver receiver = new BroadcastReceiver(){ 
    @Override 
    public void onReceive(Context context, Intent intent) { 

     String action = intent.getAction(); 
     if(BluetoothDevice.ACTION_FOUND.equals(action)) { 
      int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE); 
      Toast.makeText(getApplicationContext()," RSSI: " + rssi + "dBm", Toast.LENGTH_SHORT).show(); 
     } 
    } 
}; 
+0

你能举个例子吗,我怎么可以把RSSI和蓝牙的强度相提并论呢? – 2013-03-09 16:36:56

+0

非常感谢,我写了这段代码,但是当我与设备配对时,它并没有显示我在吐司的信号强度,你有另一种方式吗?我还将我的活动添加到主线程 – 2013-03-09 17:28:11

+1

EXTRA_RSSI值是一个简短类型而不是int更改它,然后检查 – Arvind 2013-03-09 17:40:19

23

我觉得你的代码是好的,但你需要才能看到结果,以实现startDiscovery()

确实是BluetoothDevice.EXTRA_RSSI仅适用于发现设备,当您连接到其中一个设备时,您无法再获取其RSSI。

在这里我开发了一个非常简单的活动示例,可以让您看到靠近您的设备的RSSI。首先需要在布局中添加一个TextView和一个按钮,然后启用蓝牙适配器,然后点击按钮。

package com.in2apps.rssi; 

import android.os.Bundle; 
import android.app.Activity; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 

public class RSSIActivity extends Activity { 

    private BluetoothAdapter BTAdapter = BluetoothAdapter.getDefaultAdapter(); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_rssi); 
     registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_FOUND)); 

     Button boton = (Button) findViewById(R.id.button1); 
     boton.setOnClickListener(new OnClickListener(){ 
      public void onClick(View v) { 
       BTAdapter.startDiscovery(); 
      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_rssi, menu); 
     return true; 
    } 

    private final BroadcastReceiver receiver = new BroadcastReceiver(){ 
     @Override 
     public void onReceive(Context context, Intent intent) { 

      String action = intent.getAction(); 
      if(BluetoothDevice.ACTION_FOUND.equals(action)) { 
       int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE); 
       String name = intent.getStringExtra(BluetoothDevice.EXTRA_NAME); 
       TextView rssi_msg = (TextView) findViewById(R.id.textView1); 
       rssi_msg.setText(rssi_msg.getText() + name + " => " + rssi + "dBm\n"); 
      } 
     } 
    }; 
} 

它看起来是这样的:

RSSI Detection - Android Example

+0

你能检查这[问题](http://stackoverflow.com/questions/31761416/connect-to-bluetooth-programmatically)吗? – 2015-08-04 15:37:51