簡體   English   中英

重新啟動后接收藍牙連接更改

[英]Receive bluetooth connection changes after reboot

我正在嘗試與接收器一起檢查與不同設備的藍牙連接,然后將其記錄在logcat中。 它適用於一般情況,但是在我重新啟動后會失敗。

這是正常的工作流程:

  1. 開啟電話
  2. 開啟/關閉藍牙
  3. 重新啟動手機
  4. 重啟后設備再次重新連接

我可以在logcat中看到2,因此正常情況下可以正常工作。 但是我錯過了4,並且重啟后沒有任何事件,所以我不知道我們是否再次連接。

我的清單有這個:

    <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>

這些許可:

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

我的藍牙接收器如下所示:

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());
}

}

有沒有更簡單的方法可以做到這一點? 如果重啟后我沒有收到第一個藍牙連接事件,那么一切都一文不值。 還是在重啟后以某種方式啟動接收機?

你可以抓住

<action android:name="android.intent.action.BOOT_COMPLETED" /> 

在您的BluetoothReceiver並手動檢查藍牙連接狀態。

PS不要忘記

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

例如,檢查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