【发布时间】:2013-12-27 08:51:56
【问题描述】:
我写了有关android蓝牙编程的代码。我正在连接到HC06模块。我使用了socket,我使用了Runnable方法。如果连接已经结束,我关闭socket
当我断开蓝牙电缆时,我可以看到连接失败的消息。但是我退出蓝牙连接范围时,我看不到连接失败的消息。
我无法退出蓝牙设备连接范围。即使我离开了。
我必须做什么?
【问题讨论】:
标签: android sockets bluetooth connection range
我写了有关android蓝牙编程的代码。我正在连接到HC06模块。我使用了socket,我使用了Runnable方法。如果连接已经结束,我关闭socket
当我断开蓝牙电缆时,我可以看到连接失败的消息。但是我退出蓝牙连接范围时,我看不到连接失败的消息。
我无法退出蓝牙设备连接范围。即使我离开了。
我必须做什么?
【问题讨论】:
标签: android sockets bluetooth connection range
使用意图过滤器来监听 ACTION_ACL_CONNECTED、ACTION_ACL_DISCONNECT_REQUESTED 和 ACTION_ACL_DISCONNECTED 广播:
public void onCreate() {
...
IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
IntentFilter filter2 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
IntentFilter filter3 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
this.registerReceiver(mReceiver, filter1);
this.registerReceiver(mReceiver, filter2);
this.registerReceiver(mReceiver, filter3);
}
//The BroadcastReceiver that listens for bluetooth broadcasts
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
... //Device found
}
else if (BluetoothAdapter.ACTION_ACL_CONNECTED.equals(action)) {
... //Device is now connected
}
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
... //Done searching
}
else if (BluetoothAdapter.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) {
... //Device is about to disconnect
}
else if (BluetoothAdapter.ACTION_ACL_DISCONNECTED.equals(action)) {
... //Device has disconnected
}
}
};
【讨论】: