I'm trying to work with a receiver to check bluetooth connection with different devices and then log it in logcat. It works for normal cases but fail when I reboot.
Here is a normal workflow:
I can see 2 in my logcats so normal cases works fine. But I miss 4 and don't get a single event after reboot so I don't know if we're connected again.
My manifest have this:
<receiver android:name=".settings.BluetoothReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED" />
</intent-filter>
</receiver>
And these permission:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
And my bluetooth reciver look like this:
public class BluetoothReceiver extends 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)) {
Log.e("BluetoothReceiver", "ActionFound");
}
JSONObject status = new JSONObject();
try {
status.put("status", action);
status.put("name", device.getName());
status.put("address", device.getAddress());
Calendar c = Calendar.getInstance();
status.put("time", c.getTime());
} catch (JSONException e) {
e.printStackTrace();
}
Log.e("BluetoothReceiver", status.toString());
}
}
Is there any easier way to do this? Everything is pretty worthless if I don't receive the first bluetooth connection event after reboot. Or do I have kickstart the receiver after the reboot in some way?
You can catch
<action android:name="android.intent.action.BOOT_COMPLETED" />
in your BluetoothReceiver
and check bluetooth connection state manually.
PS Don't forget
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Check http://www.jjoe64.com/2011/06/autostart-service-on-device-boot.html for example
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.