A Brentwood man who tried to rob an off-duty NYPD officer while he was walking home Friday night in Patchogue faces robbery and assault charges, Suffolk police said.

The attack resulted in the officer's gun discharging, police said, and both the victim and the suspect ended up in the hospital with minor injuries, though neither was hit by gunfire.

Police said the off-duty officer was walking home on Jennings Avenue near the fire department at about 10:15 p.m. when he was attacked from behind by Timothy Frederique, 21, who tried to steal the officer's backpack.

The victim identified himself as a police officer and reached for his weapon, police said. At the same time, Frederique grabbed for the officer's gun. A scuffle ensued, and during the struggle the gun discharged, though no one was hit, police said.

Frederique was eventually subdued by a passerby, according to police.

Both Frederique and the victim were taken to Brookhaven Memorial Hospital Medical Center to be treated for minor injuries.

Frederique faces charges of second-degree robbery and second-degree assault. Arraignment information was not immediately available Saturday.