Android Récepteur de Radiodiffusion bluetooth événements de capture

Je suis en train d'essayer d'attraper bluetooth changements d'état avec Récepteur de Radiodiffusion.

Mon manifeste:

<uses-permission android:name="android.permission.BLUETOOTH" />
<application>
     <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name=".BluetoothBroadcastReceiver"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />
            <action android:name="android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED" />
            <action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
            <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
        </intent-filter>
    </receiver>
</application>

Récepteur onReceive méthode:

public void onReceive(Context context, Intent intent) {

    String action = intent.getAction();
    Log.d("BroadcastActions", "Action "+action+"received");
    int state;
    BluetoothDevice bluetoothDevice;

    switch(action)
    {
        case BluetoothAdapter.ACTION_STATE_CHANGED:
            state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);
            if (state == BluetoothAdapter.STATE_OFF)
            {
                Toast.makeText(context, "Bluetooth is off", Toast.LENGTH_SHORT).show();
                Log.d("BroadcastActions", "Bluetooth is off");
            }
            else if (state == BluetoothAdapter.STATE_TURNING_OFF)
            {
                Toast.makeText(context, "Bluetooth is turning off", Toast.LENGTH_SHORT).show();
                Log.d("BroadcastActions", "Bluetooth is turning off");
            }
            else if(state == BluetoothAdapter.STATE_ON)
            {
                Log.d("BroadcastActions", "Bluetooth is on");
            }
            break;

        case BluetoothDevice.ACTION_ACL_CONNECTED:
            bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            Toast.makeText(context, "Connected to "+bluetoothDevice.getName(),
                    Toast.LENGTH_SHORT).show();
            Log.d("BroadcastActions", "Connected to "+bluetoothDevice.getName());
            break;

        case BluetoothDevice.ACTION_ACL_DISCONNECTED:
            bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            Toast.makeText(context, "Disconnected from "+bluetoothDevice.getName(),
                    Toast.LENGTH_SHORT).show();
            break;
    }
}

J'ai lancer l'application puis réduisez-le en appuyant sur la touche Home. Aller dans les paramètres et activer le bluetooth, mais rien ne se passe. Mais j'attends de pain grillé et de logcat messages. Quel est le problème ici?

Pas sûr, il peut être à cause de la façon dont la diffusion enregistré. Je voudrais essayer de s'inscrire au service, et non au moment de l'exécution et de voir si cela change quoi que ce soit.
Yep, j'ai essayé d'ajouter le service où je m'inscrire récepteur. Il ressemblait à ce IntentFilter fltr = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED); fltr.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED); fltr.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED); fltr.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); registerReceiver(brecv, fltr);brecv est un objet de CustomBluetoothReceiver. Il n'y avait pas toutes les modifications.
Ne vous lancez le Service dans un certain endroit ?
Merci. C'était un émulateur problème. Sur le périphérique réel, il fonctionne très bien.

OriginalL'auteur Long Smith | 2015-05-13