2014-11-24 64 views
0

我试图与接收器一起检查与不同设备的蓝牙连接,然后将其记录在logcat中。它适用于正常情况,但在重新启动时会失败。重新启动后接收蓝牙连接更改

这是一个正常的工作流程:

  1. 手机上
  2. 切换蓝牙开/关
  3. 重启手机
  4. 重新连接设备,重新启动

我可以看到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" /> 

我的蓝牙reciver是这样的:

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

}

有什么项目EATCHIP如何做到这一点?如果我在重新启动后没有收到第一个蓝牙连接事件,那么一切都是毫无价值的。或者在重启后以某种方式启动接收器?

回答

0

你能赶上

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

和手动检查蓝牙连接状态。

P.S.不要忘记

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

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

+0

感谢的答案,但我会如何手动检查呢?我的意思是在我的onReceive我有这条线走出设备: BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 但我想我必须将其更改为启动完成的其他内容?我是否必须获得蓝牙适配器并检查与所有设备的连接?我使用接收器的原因是因为我认为你无法检查连接,而是必须等待更改。 – user1776562 2014-11-24 13:37:25

+0

您可以使用'BluetoothAdapter'类。在这里查看示例http://www.programcreek.com/java-api-examples/index.php?api=android.bluetooth.BluetoothProfile – 2014-11-24 13:48:54