![](/img/trans.png)
[英]How to receive data using bluetooth after connection (in listening mode) in Delphi android
[英]Receive bluetooth connection changes after reboot
我正在尝试与接收器一起检查与不同设备的蓝牙连接,然后将其记录在logcat中。 它适用于一般情况,但是在我重新启动后会失败。
这是正常的工作流程:
我可以在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.