2012-10-30 177 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