示例
private static final int REQUEST_DISCOVERABLE_BT = 2; // 唯一请求码
private static final int DISCOVERABLE_DURATION = 120; // 可发现的持续时间(以秒为单位)
// 0表示始终可发现
// 最大值是3600
// ...
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, DISCOVERABLE_DURATION);
startActivityForResult(discoverableIntent, REQUEST_DISCOVERABLE_BT);
// ...
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_DISCOVERABLE_BT) {
if (resultCode == RESULT_OK) {
// 设备可发现
} else if (resultCode == RESULT_CANCELED) {
// 无法发现设备
}
}
}