Android 蓝牙 ACTION_DISCOVERY_FINISHED 不起作用
我已经编写了我的第一个 Android 应用程序,一切都运行良好,除了......在下面的例程中,ACTION_DISCOVERY_FINISHED 似乎从未被调用(或者广播或接收或其他)。不管怎样,“else if”中的代码块都不起作用。
我只在我的 摩托罗拉 Atrix 上进行了测试,所以我想知道这是否是问题所在。由于我正在测试蓝牙功能,因此我认为我无法使用Android模拟器进行有效测试。
想法?
private BluetoothAdapter mBtAdapter;
mBtAdapter.startDiscovery();
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
//do something
}
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
//do something else
}
}
}
I have written my first Android app and everything is working really well, except...in the routine, below, the ACTION_DISCOVERY_FINISHED never seems to get called (or broadcast or received or whatever). No matter what the block of code in that "else if" is not working.
I have only tested on my Motorola Atrix, so I am wondering if that is the issue. Since I am testing bluetooth functionality, I don't think I can use the Android emulator for effective testing.
Thoughts?
private BluetoothAdapter mBtAdapter;
mBtAdapter.startDiscovery();
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
//do something
}
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
//do something else
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
2 种可能的解决方案:
从您的活动/服务中动态注册它,如下所示:
我不确定从活动/服务注册它时是否必须取消注册它(我知道从应用程序的上下文注册时必须取消注册) )所以检查一下。
2 possibles solutions:
Instead of creating an annonymous receiver, subclass BroadcastReceiver with just the same implementation, then declare it in your project manifest (Remember to declare that your receiver receives these actions you want).
Dynamically register it from your activity/service, this way:
I'm not sure if you have to unregister it when registering it from an activity/service (I know you have to when registering from app's context) so check it out.
您需要将该行添加
到您的清单中
You need to add the line
to your manifest