【发布时间】:2012-03-02 17:38:25
【问题描述】:
我想知道远程物理设备中的哪些事件会触发监听设备中的ACTION_ACL_CONNECTED 和ACTION_ACL_DISCONNECTED。我的测试结果毫无意义。我收集了几个彼此相距几分米的设备:
- 运行 Android 3.1 的 Galaxy Tab P7500
- 运行 Android 2.2 的 i5500 手机
- 带有蓝牙 USB 加密狗的 WinXP 电脑
- 两个带开/关按钮的耳机
首先,我手动与选项卡中的所有设备配对。 PC 和手机均未与 Tab 以外的任何其他设备配对。 (其中一个耳机永远无法通过选项卡以任何方式找到,但可以通过手动和编程方式轻松地从手机中找到)。然后我有一个简单的应用程序来开始发现并侦听并显示 ACL 广播。这就是发生的事情(每次都是同样的事情,它的疯狂是一致的):
-
startDiscovery()来自启用所有设备的选项卡:- PC 是唯一找到的设备 - 在 PC 上禁用蓝牙:- 选项卡上没有反应
- 在 PC 上启用蓝牙:- 选项卡上没有反应
- 第一次打开耳机:-标签上的
ACTION_ACL_CONNECTED - 关闭耳机:- 选项卡上没有反应
- 再次打开耳机电源:- 在选项卡上快速连续地
ACTION_ACL_DISCONNECTED和ACTION_ACL_CONNECTED
- 在选项卡上禁用蓝牙:- 在选项卡上没有反应
- 在选项卡上启用蓝牙:- 在选项卡上启用耳机
ACTION_ACL_CONNECTED
-
startDiscovery()来自电话:- PC 是唯一被 手机,虽然手机只与 Tab 配对,而不与 个人电脑。否则,手机只会对 Tab 的耳机做出反应 从不反应。
如何解决这个混乱?不能依赖导致ACTION_ACL_CONNECT 的设备,即使它已配对并在范围内通电?
以下是 BroadcastReceiver 和活动 onCreate 的方法,但我不认为这段代码中的细节很重要:
BroadcastReceiver intentReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (device != null) {
name = device.getName();
Log.v(TAG, "Device=" + device.getName());
}
else {
name = "None";
}
if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
text1.setText(name + " connected " + (checkCounter++));
Log.v(TAG, "connected: " + device);
}
else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
text2.setText(name + " disconnected " + (checkCounter++));
Log.v(TAG, "disconnected: " + device);
}
else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
text3.setText( name + " found " + (checkCounter++));
Log.v(TAG, "found: " + device + "");
}
else if (blueAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
text4.setText("Started " + (checkCounter++));
Log.v(TAG, "Discovery started");
}
else if (blueAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
text4.setText("Finished " + (checkCounter++));
Log.v(TAG, "Discovery finished");
}
}
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bluetoothlayout);
text1 = (TextView)findViewById(R.id.textView1);
text2 = (TextView)findViewById(R.id.textView2);
text3 = (TextView)findViewById(R.id.textView3);
text4 = (TextView)findViewById(R.id.textView4);
BluetoothDevice mw600 = blueAdapter.getRemoteDevice("58:17:0C:EB:C5:08");
BluetoothDevice bt500 = blueAdapter.getRemoteDevice("00:1D:43:00:C4:54");
BluetoothDevice galaxyTab = blueAdapter.getRemoteDevice("00:07:AB:6A:96:7D");
BluetoothDevice pcDongle = blueAdapter.getRemoteDevice("00:15:83:4D:8B:57");
intentFilter = new IntentFilter();
intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
intentFilter.addAction(blueAdapter.ACTION_DISCOVERY_STARTED);
intentFilter.addAction(blueAdapter.ACTION_DISCOVERY_FINISHED);
if (!isReceiverRegistered) {
registerReceiver(intentReceiver, intentFilter);
isReceiverRegistered = true;
}
if (!blueAdapter.isEnabled()) {
blueAdapter.enable();
}
blueAdapter.startDiscovery();
}
【问题讨论】:
标签: android bluetooth broadcastreceiver device