Had he not informed them that he would shoot at them, they would have left him alone. Telling them that he was going to fire at them left them no choice but to deal with him before the other belligerent force arrived to attack them.
An officer is responsible for the defense of his position.
Anderson did not fire on Confederate forces until the fort was fired on.