蓝牙An​​droid的RSSI值返回-32768始终? [英] Android RSSI value of Bluetooth returns -32768 always?

查看:442
本文介绍了蓝牙An​​droid的RSSI值返回-32768始终?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在点击按钮来获得一个连接的蓝牙设备的当前RSSI值。然而,它仅返回-32768永远!不知道什么是错的!不过,我能得到正确的RSSI,第一次它得到了连接。

I am trying to get the current RSSI value of a connected bluetooth device at the click of a button. However it returns only -32768 always! Don't know what is wrong! However I was able to get the correct RSSI, the first time it got connected.

private Button.OnClickListener buttonRSSIOnClickListener = new Button.OnClickListener(){
  @Override
  public void onClick(View arg0) {
   // TODO Auto-generated method stub
   Intent intent = new Intent(BluetoothDevice.ACTION_FOUND);
   short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
   Toast.makeText(getApplicationContext(),"  RSSI: " + rssi + "dBm", Toast.LENGTH_SHORT).show();

  }};

谁能帮我吗?

推荐答案

这是不是你如何使用意向。你得到-32768因为RSSI是不是在您刚刚创建的意图,并且已指定默认的结果是Short.MIN_VALUE(-32768)。

This isn't how you use an Intent. You are getting -32768 because the RSSI isn't in that Intent that you just created, and the default result you have specified is Short.MIN_VALUE (-32768).

您需要继承广播接收器,并创建一个IntentFilter的(或使用清单),以使您获得BluetoothDevice.ACTION_FOUND意图。

You need to subclass BroadcastReceiver, and create an IntentFilter (or use the manifest) to so that you receive the BluetoothDevice.ACTION_FOUND intent.

您将无法做到这一点在点击按钮。你只把它当Android的生成ACTION_FOUND。

You won't be able to do this "at the click of a button." You'll only get it when Android generates the ACTION_FOUND.

下面是一些接近。没有运行它自己。

Here is something close. Haven't run it myself.

在的onCreate():

In onCreate():

registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));

在别处:

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

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

编辑:其实,如果你在你的BluetoothAdapter从内部的onClick调用startDiscovery()你也许可以做到这一点点播()。这应该引起ACTION_FOUND每个它发现的设备。

Actually you might be able to do it on-demand if you call startDiscovery() on your BluetoothAdapter from within onClick(). That should trigger ACTION_FOUND for each device it discovers.

这篇关于蓝牙An​​droid的RSSI值返回-32768始终?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆