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