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

我试图通过点击按钮获得连接的蓝牙设备的当前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();

}};

有人可以帮我吗?

解决方法:

这不是你使用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()内部调用BluetoothAdapter上的startDiscovery(),您可以按需执行此操作.这应该为它发现的每个设备触发ACTION_FOUND.

标签:android,onclick,bluetooth,rssi,signal-strength

来源: https://codeday.me/bug/20190723/1515282.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值