要将Android蓝牙可发现时间设置为120秒,可以使用以下代码示例:
import android.bluetooth.BluetoothAdapter;
// 获取默认的蓝牙适配器
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// 设置可发现时间为120秒
int discoverableDuration = 120;
bluetoothAdapter.enable(); // 确保蓝牙已启用
bluetoothAdapter.setDiscoverableTimeout(discoverableDuration);
此代码使用BluetoothAdapter
类来获取默认的蓝牙适配器,并使用setDiscoverableTimeout
方法来设置可发现时间为120秒。请确保在调用此方法之前已启用蓝牙,可以使用enable
方法进行启用。
请注意,这段代码需要添加适当的权限到你的AndroidManifest.xml文件中:
这些权限是用于访问蓝牙功能。