我为我的手机写了一个MP3播放器。我希望它在某些蓝牙设备连接时自动开始播放 - 例如在我的车里。当他们断开连接时停止播放。它适用于我的旧三星 Galaxy S9(Android 版本 10),但不适用于我的新三星 Galaxy S24(Android 版本 14)。我假设操作系统在版本 10 和版本 14 之间发生了一些变化。我在 android 文档中找不到任何关于此的内容。
清单如下所示:
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH" />
...
<application
...
tools:targetApi="31">
...
<activity
android:name=".MainActivity"
android:exported="true"
在MainActivity中,它请求蓝牙权限并获得它:
ActivityCompat.requestPermissions(getMainActivity(),
new String[]{Manifest.permission.BLUETOOTH},
MY_BLUETOOTH_PERMISSION);
MainActivity 中的代码如下所示:
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
checkPermission(android.Manifest.permission.BLUETOOTH_CONNECT,BLUETOOTH_PERMISSION);
Toast.makeText(mainActivity, "Bluetooth device " + device.getName(), Toast.LENGTH_LONG).show();
addBluetoothDevice(device);
} else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
bluetoothConnected = addBluetoothDevice(device);
if (bluetoothConnected != null && bluetoothConnected.isPlayOnConnect()) {
mediaService.resume();
}
Toast.makeText(mainActivity, "Bluetooth device connected " + device.getName(), Toast.LENGTH_LONG).show();
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
addBluetoothDevice(device);
Toast.makeText(mainActivity, "Bluetooth device discovery finished " + device.getName(), Toast.LENGTH_LONG).show();
} else if (BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) {
addBluetoothDevice(device);
Toast.makeText(mainActivity, "Bluetooth device disconnect requested " + device.getName(), Toast.LENGTH_LONG).show();
} else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
bluetoothDisconnected = addBluetoothDevice(device);
if (bluetoothDisconnected != null && bluetoothDisconnected.isPauseOnDisconnect()) {
mediaService.pause();
}
Toast.makeText(mainActivity, "Bluetooth device disconnected " + device.getName(), Toast.LENGTH_LONG).show();
} else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
switch (state) {
case BluetoothAdapter.STATE_OFF:
System.out.println("STATE_OFF");
break;
case BluetoothAdapter.STATE_TURNING_OFF:
break;
case BluetoothAdapter.STATE_ON:
break;
case BluetoothAdapter.STATE_TURNING_ON:
break;
}
}
}
};
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);type here
int receiverFlags = 0;
this.registerReceiver(mReceiver, filter, receiverFlags);
我也尝试监听 ACTION_STATE_CHANGED (参见上面的代码),尽管我不需要它。然而,当我打开和关闭蓝牙时,我确实在 S24 上收到了 ACTION_STATE_CHANGED 的广播。我需要的是 ACTION_ACL_CONNECTED 和 ACTION_ACL_DISCONNECTED,当 S24 手机连接到蓝牙扬声器时我没有得到这些。再说一次,它确实可以在我的 S9 手机上使用。显然 Android 版本 10 和 14 之间发生了一些变化。有人知道它是什么吗?
您需要在清单中和运行时请求
BLUETOOTH_CONNECT
权限,以便根据 文档接收这些广播。
显然 Android 版本 10 和 14 之间发生了一些变化。
Android 11 中为面向 Android 11 或更高版本的应用添加了权限要求。