1

我正在尝试监听蓝牙连接/断开事件以确定是否连接了蓝牙设备。我正在尝试这样做,以便我的应用程序可以在较旧的 android 版本上运行。

我在清单中注册了接收器,如下所示:

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

        </intent-filter>
</receiver>

在我的 BluetoothReceiver 课程中,我有

public class BluetoothReceiver extends BroadcastReceiver{
public final static String TAG = "BluetoothReciever";

public void onReceive(Context context, Intent intent)
{

    Log.d(TAG, "Bluetooth Intent Recieved");

    final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    String action = intent.getAction();

    if (BluetoothDevice.ACTION_ACL_CONNECTED.equalsIgnoreCase(action))
    {
        Log.d(TAG, "Connected to " + device.getName());
    }

    if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equalsIgnoreCase(action))
    {
        Log.d(TAG, "Disconnected from " + device.getName());
    }
}}

但是当连接和断开蓝牙时,什么也没有发生。我究竟做错了什么?

4

0 回答 0