Android蓝牙RSSI值总是返回-32768?

发布于 2024-12-01 08:34:05 字数 550 浏览 2 评论 0原文

我试图通过单击按钮获取已连接蓝牙设备的当前 RSSI 值。然而它总是只返回-32768!不知道出了什么问题!不过,我在第一次连接时就能够获得正确的 RSSI。

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();

  }};

有人可以帮我吗?

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();

  }};

Can anyone help me please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟沫凡尘 2024-12-08 08:34:05

这不是使用 Intent 的方式。您得到 -32768,因为 RSSI 不在您刚刚创建的 Intent 中,并且您指定的默认结果是 Short.MIN_VALUE (-32768)。

您需要子类化BroadcastReceiver,并创建一个IntentFilter(或使用清单)才能接收BluetoothDevice.ACTION_FOUND意图。

您将无法“通过单击按钮”来完成此操作。只有当 Android 生成 ACTION_FOUND 时你才会得到它。

这是一些接近的东西。自己没跑过。

在 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();
        }
    }
};

编辑:实际上,如果您从 onClick() 内调用蓝牙适配器上的 startDiscovery() ,您可能可以按需执行此操作。这应该为它发现的每个设备触发 ACTION_FOUND 。

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).

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

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.

In onCreate():

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

Elsewhere:

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();
        }
    }
};

EDIT: 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文