我有BroadcastReceiver服务 注册行动:
在与Galaxy S3和Fly iq280上的其他设备连接/断开连接后,调用onReceived(),但在谷歌Nexus 7上没有。
也许我做错了什么?
我的代码:
private void registerConnectionBroadcast() {
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);
IntentFilter filter4 = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mReceiver, filter1);
registerReceiver(mReceiver, filter2);
registerReceiver(mReceiver, filter3);
registerReceiver(mReceiver, filter4);
}
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String deviceAddress = device != null ? device.getAddress() : "";
Log.d(TAG, deviceAddress);
if (!isConnected()) {
SettingsSupport.setDeviceAddress(deviceAddress, ReceiverService.this);
}
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
Log.d("ConnectionBroadcastReceiver", "Device found");//Device found
}
else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
Log.d("ConnectionBroadcastReceiver", "Device is now connected");//Device is now connected
}
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
Log.d("ConnectionBroadcastReceiver", "Done searching");//Done searching
}
else if (BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) {
Log.d("ConnectionBroadcastReceiver", "Device is about to disconnect");//Device is about to disconnect
}
else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
Log.d("ConnectionBroadcastReceiver", "Device has disconnected");//Device has disconnected
}
else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
Log.d(TAG, "Bluetooth state is changed to " + state);
}
}
};
答案 0 :(得分:0)
就我而言,它是在Nexus 4上调用的,但不是在三星S3上。 我不认为它会有任何区别,但您可以尝试在清单xml中注册接收器。