2013-03-09 144 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