Marijuana and a pipe used to smoke it are displayed in New York in June 2018. Peter Morgan/Associated Press/File/Associated Press

If you’re acting loopy during a traffic stop and your car smells like pot, police can arrest you for driving under the influence of marijuana.

That was a finding contained in a ruling Monday from the state Supreme Judicial Court, amid the backdrop of a booming retail marijuana industry that launched in Massachusetts in November.

The ruling concerned a defendant, Mark J. Davis, who was arrested for alleged impaired driving in Brighton in 2015, after State Police pulled him over for speeding and operating erratically on the Mass. Pike.

Davis was acquitted of the drugged driving charge at trial, but he challenged the lawfulness of the arrest on appeal, because troopers had impounded and searched his vehicle. They found cocaine, oxycodone, and a handgun inside the car, leading to additional drug and gun possession charges, records show.

A jury convicted Davis of drug possession but acquitted him of possessing the gun.

As for the initial arrest for drugged driving, the SJC said it passed constitutional muster, based on the arresting trooper’s observations at the scene.

The trooper, the ruling said, “detected the odor of burnt and unburnt marijuana emanating from the vehicle, and the odor of burnt marijuana coming from the defendant’s person. Among other things, the defendant had red and glassy eyes, he was struggling to keep his eyes open and his head upright, ‘his coordination was slow,’ he had difficulty ‘focusing,’ and he also had difficulty in following the officer’s ‘simple directions.’ ”

The ruling continued, “The defendant told the officer that he had smoked marijuana earlier that day, before he left to drive to Somerville. Given this, the judge was warranted in finding that police had probable cause to believe that the defendant had operated a motor vehicle while impaired.”

In addition, the court ruled that the later warrantless search of Davis’s vehicle at the State Police barracks was justified, finding that troopers “had authority to search the vehicle, pursuant to the automobile [warrant] exception, for evidence pertaining to the offense of operating a motor vehicle while under the influence.”

That search turned up oxycodone and cocaine in a locked glove box, as well as a loaded Smith and Wesson .380 handgun in the trunk.

Jurors convicted Davis of drug possession after his trial lawyer told them, “Go ahead and find him guilty of the drugs in the glove box. Those are his,” the ruling said.

But the jury bought the lawyer’s argument that impaired driving and gun possession charges couldn’t stick.

Davis’s attorney argued, “ ‘[I]t is simply insufficient for the police to have found something in the trunk of the car where there were three people inside and where two people, after [the defendant] was removed, went in and took their property out,’ ” the ruling said. “ ‘So compare that to what they found in the glove box. . . . He possesses the things in the glove box. That does not prove anything about the gun.’ ”

Jurors agreed.

But Monday’s SJC ruling means the conviction for drug possession stands.

The opinion provided clarity on factors police can cite when arresting someone for driving under the influence of pot.

“We acknowledge that it is often difficult to detect marijuana impairment, because the effects of marijuana consumption ‘vary greatly amongst individuals,’ ” the opinion said.

Quoting from prior rulings on the subject, the SJC wrote, “ ‘A police officer makes numerous relevant observations in the course of an encounter with a possibly impaired driver. There is no doubt that an officer may testify to his or her observations of, for example, any erratic driving or moving violations that led to the initial stop; the driver’s appearance and demeanor; the odor of fresh or burnt marijuana; and the driver’s behavior on getting out of the vehicle.’ ”