简体   繁体   English

重新启动后接收蓝牙连接更改

[英]Receive bluetooth connection changes after reboot

I'm trying to work with a receiver to check bluetooth connection with different devices and then log it in logcat. 我正在尝试与接收器一起检查与不同设备的蓝牙连接,然后将其记录在logcat中。 It works for normal cases but fail when I reboot. 它适用于一般情况,但是在我重新启动后会失败。

Here is a normal workflow: 这是正常的工作流程:

  1. Phone on 开启电话
  2. Toggle bluetooth on/off 开启/关闭蓝牙
  3. Reboot phone 重新启动手机
  4. Device reconnect again after reboot 重启后设备再次重新连接

I can see 2 in my logcats so normal cases works fine. 我可以在logcat中看到2,因此正常情况下可以正常工作。 But I miss 4 and don't get a single event after reboot so I don't know if we're connected again. 但是我错过了4,并且重启后没有任何事件,所以我不知道我们是否再次连接。

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. 在您的BluetoothReceiver并手动检查蓝牙连接状态。

PS Don't forget PS不要忘记

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

Check http://www.jjoe64.com/2011/06/autostart-service-on-device-boot.html for example 例如,检查http://www.jjoe64.com/2011/06/autostart-service-on-device-boot.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM