2012-10-30 175 views
2

我想抓到一个蓝牙设备断开连接意图过滤器。 我向onReceive添加了一个日志,但它永远不会到达它,并且不会显示在logcat中。 我怀疑问题是与我的manifest.xml配置:安卓蓝牙 - 检测设备断开连接

清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.company" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="10" 
     android:targetSdkVersion="16" /> 

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

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <receiver android:name="com.company.MyReceiver" android:enabled="true" android:exported="true"> 
      <intent-filter> 
       <category android:name="android.intent.category.DEFAULT" /> 
       <action android:name="android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED" /> 
       <action android:name="android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED" /> 
       <action android:name="android.bluetooth.adapter.action.DISCOVERY_STARTED" /> 
      </intent-filter> 
     </receiver> 

     <activity 
      android:name=".BTActivity" 
      android:label="BTActivity" > 
     </activity> 
    </application> 

</manifest> 

MyReceiver扩展广播接收器:

@Override 
    public void onReceive(Context context, Intent intent) { 
     Log.i("got-in", "got-in-"); 
     // String action = intent.getAction(); 
     BluetoothDevice device = intent 
       .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 

     Log.i("disconnect", device.getName()); 
     Intent i = new Intent(context, BTActivity.class); 
     Bundle b = new Bundle(); 
     b.putString("deviceName", device.getName()); 
     intent.putExtras(b); // Put your id to your next Intent 
     context.startActivity(i); 
     // finish(); 

    } 

回答

-1

尝试在android.bluetooth.device.action.ACL_DISCONNECTED行动意图过滤器。它应该解决你的问题。

+0

都能跟得上。它仍然是一样的。我的猜测是我的清单不正确。但我不确定问题是什么。 – user1528794

+0

Infact即使是“android.bluetooth.adapter.action.DISCOVERY_STARTED”也不起作用.... – user1528794

0

尝试从意图过滤器中删除<category .../>,然后重试。

+0

试过这个,但是它又没有进入接受回调函数。我错过了什么?请帮忙! – user1528794

4

使用此代码用于接收断开(Bluetooth时仍然开启):

<intent-filter> <action android:name="android.bluetooth.device.action.ACL_CONNECTED" /> 
    <action android:name="android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED" /> 
    <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" /> 
</intent-filter> 

你也需要注册一个服务,然后注册了蓝牙状态变化的广播接收器,让你的应用程序知道当蓝牙已关闭,因此会丢弃所有活动的设备,因为当蓝牙关闭时您将不会收到ACL_DISCONNECT。

@Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 

    IntentFilter filter = new IntentFilter(); 
    filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); 
    registerReceiver(bluetoothTurnedOnOff, filter); 
    return START_STICKY; 
} 

private final BroadcastReceiver bluetoothTurnedOnOff = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_OFF) { 
[...] 
+0

为什么我需要创建一项服务?在我的清单xml文件中声明一个接收器并且扩展了BroadcastReceiver的类是不够的?这不够吗? – user1528794

+1

您必须创建服务,因为Intent BluetoothAdapter.ACTION_STATE_CHANGED无法通过清单注册,因此您必须创建一个服务来注册自定义Receiver – Force

1

对于新用户。它会起作用。清单文件看起来

<receiver android:name="(package name).BluetoothReceiver" > 
      <intent-filter> 
       <action android:name="android.bluetooth.device.action.ACL_CONNECTED" /> 
       <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" /> 
      </intent-filter> 
     </receiver> 

和接收器看起来像

else if (intent.getAction().equals(`enter code here` 
    BluetoothDevice.ACTION_ACL_DISCONNECTED)) { 
    // connection lost