【问题标题】:Android Getting continuously Bluetooth signal strength of paired devicesAndroid 不断获取配对设备的蓝牙信号强度
【发布时间】:2014-03-06 08:32:53
【问题描述】:

我列出了所有配对的设备,它运行良好,但现在想获取配对设备的蓝牙信号强度...我知道它将通过使用 rssi 获得,但无法在我的应用程序中连续获取它.. 请给我合适的代码作为我的代码...我的代码在这里...

public class Security extends Fragment implements OnClickListener{   
     private BluetoothAdapter BA;
     private Set<BluetoothDevice>pairedDevices;

     ArrayList<String> mylist = new ArrayList<String>();

     //private Object ImageView;

     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     View v = inflater.inflate(R.layout.security, null);

     BA = BluetoothAdapter.getDefaultAdapter();

     /* starting the bluetooth*/
     on(v);
     pairedDevices = BA.getBondedDevices();
     //length=4;
     // int j=1;

     for(BluetoothDevice bt : pairedDevices) {
        mylist.add(bt.getName());
        length=j;
        j++;

        bt.getBondState();
    }

     return v;
     }

     @Override
     public void onResume() {
      super.onResume();
    // Toast.makeText(getActivity(), "On resume", Toast.LENGTH_LONG).show(); 
     }

     /*************************Bluetooth function****************************/   

       public void on(View view){
          if (!BA.isEnabled()) {
             Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
             startActivityForResult(turnOn, 0);
             Toast.makeText(getActivity(),"Turned on" 
             ,Toast.LENGTH_LONG).show();
          } else{
            // Toast.makeText(getActivity(),"Already on",
           //  Toast.LENGTH_LONG).show();
             }
       }

       public void Discovery(View view) {
          if(BA.isDiscovering()) {
            BA.cancelDiscovery();   
           }
       }

       public void list(View view){
              Toast.makeText(getActivity(),"Showing Paired Devices",
              Toast.LENGTH_SHORT).show();
           }

    @Override
    public void onClick(View v) {
        for(int j=0;j<length;j++) {
        if(v.getId()==j)
        Toast.makeText(getActivity(), mylist.get(j), Toast.LENGTH_LONG).show();
        //hand.update(run,1000);
        }

    }

}

【问题讨论】:

  • 您只需要配对设备吗?因为您没有发现蓝牙设备。
  • 是的,只有活动的配对设备...
  • 如果您正在考虑使用活动设备,那么它将已经与您的设备配对。
  • 一切正常,但我无法连续获取活动配对设备的 rssi ......
  • 嗨 Dhiman,您有解决方案吗?我面临同样的问题。请帮帮我。

标签: android bluetooth


【解决方案1】:

您可以通过以下代码获取信号。

编码您的活动

@Override
public void onCreate(Bundle savedInstanceState) {
    .....
    // Registering Broadcast. this will fire when Bluetoothdevice Found
    registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED));
}

private final BroadcastReceiver BroadcastReceiver = new BroadcastReceiver(){
    @Override
    public void onReceive(Context context, Intent intent) {
        String mIntentAction = intent.getAction();
        if(BluetoothDevice.ACTION_ACL_CONNECTED.equals(mIntentAction)) {
            int RSSI = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
            String mDeviceName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
        }
    }
};

此广播将在您的设备连接到远程设备时执行。 还有其他几个动作可以触发这个广播。看看here(BluetoothDevice)

检查以下链接以获取 RSSI 的持续读数

Tutorial to continuously measure the Bluetooth RSSI of a connected Android device (Java)

链接输出:

【讨论】:

  • 我知道这一点,但它会在运行应用程序时检索一次 rssi...我希望在运行应用程序的时间片后连续...
  • 那么你需要不断地调用它。 TimerTask 中的意思
【解决方案2】:

然后如果你想连续做,你需要在服务或线程中运行它。这样您甚至可以添加时隙或睡眠(等待)来测量 RSSI。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-12
    • 2012-03-09
    • 1970-01-01
    • 2015-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多