Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何判断蓝牙设备是否已连接?我的代码不是';行不通_Android_Android Intent - Fatal编程技术网

Android 如何判断蓝牙设备是否已连接?我的代码不是';行不通

Android 如何判断蓝牙设备是否已连接?我的代码不是';行不通,android,android-intent,Android,Android Intent,我正在尝试侦听蓝牙连接/断开事件,以确定是否连接了蓝牙设备。我正试图这样做,这样我的应用程序就可以在旧的android版本上运行 我在舱单中登记了接收人: <receiver android:name=".BluetoothReceiver"> <intent-filter> <action android:name="android.bluetooth.device.action.ACL_CONNECTED"

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

我在舱单中登记了接收人:

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

        </intent-filter>
</receiver>

但当连接和断开蓝牙时,什么也不会发生。我做错了什么?

如果是蓝牙耳机,请参阅
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());
    }
}}