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();
}};
有人可以帮我吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是使用 Intent 的方式。您得到 -32768,因为 RSSI 不在您刚刚创建的 Intent 中,并且您指定的默认结果是 Short.MIN_VALUE (-32768)。
您需要子类化BroadcastReceiver,并创建一个IntentFilter(或使用清单)才能接收BluetoothDevice.ACTION_FOUND意图。
您将无法“通过单击按钮”来完成此操作。只有当 Android 生成 ACTION_FOUND 时你才会得到它。
这是一些接近的东西。自己没跑过。
在 onCreate() 中:
其他地方:
编辑:实际上,如果您从 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():
Elsewhere:
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.