The Supreme Court ruled Tuesday that police may not drag out a routine traffic stop in order to buy time for a dog to search the vehicle for drugs.

“A police stop exceeding the time needed to handle the matter for which the stop was made violates the Constitution’s shield against unreasonable seizures,” Justice Ruth Bader Ginsburg wrote, adding that authority for stopping the vehicle “ends when tasks tied to the traffic infraction are — or reasonably should have been — completed.”

The court’s decision was 6 to 3, with Chief Justice John G. Roberts Jr. and Justices Antonin Scalia, Stephen G. Breyer, Sonia Sotomayor and Elena Kagan joining Ginsburg.

The case began in 2012, when Nebraska police officer Morgan Struble saw a Mercury Mountaineer veer onto the shoulder of the highway and quickly jerk back onto the road. Struble was accompanied by his police dog, Floyd.

Struble questioned the two men in the car, driver Dennys Rodriguez and his passenger, Scott Pollman. After the routine of a traffic stop — asking for license, registration and proof of insurance — Struble ran a records check and then wrote Rodriguez a warning.

Struble said he was suspicious of the two men, though, and asked to search the car. Rodriguez said no. Struble told Rodriguez to exit the car and, after a second officer arrived, retrieved Floyd. The dog circled the vehicle twice before alerting to the presence of drugs.

“All told, seven or eight minutes had elapsed from the time Struble issued the written warning until the dog indicated the presence of drugs,” Ginsburg wrote. It was a “large bag of methamphetamine.”

The Supreme Court in 2005 said that a dog sniff conducted during a lawful traffic stop does not violate the Fourth Amendment, a case in which Ginsburg dissented. But she said the “line drawn” in that decision was that the dog sniff could not prolong the traffic stop.

Justices Anthony M. Kennedy, Clarence Thomas and Samuel A. Alito Jr. dissented.

Thomas said the majority’s rule would produce arbitrary results. “Under its reasoning, a traffic stop made by a rookie could be executed in a reasonable manner, whereas the same traffic stop made by a knowledgeable, veteran officer in precisely the same circumstances might not, if in fact his knowledge and experience made him capable of completing the stop faster,” Thomas wrote.

Alito said the rule would simply lead to police workarounds, such as conducting the sniff while running a records check and before issuing a ticket or warning for the original infraction.

He wrote: “Most officers will learn the prescribed sequence of events even if they cannot fathom the reason for that requirement. (I would love to be the proverbial fly on the wall when police instructors teach this rule to officers who make traffic stops.)”

Rodriguez, who was sentenced to five years in prison, is not in the clear. The justices returned the case to lower courts to decide whether Struble had reasonable suspicion to conduct the search. The U.S. Court of Appeals for the 8th Circuit did not rule on that question.

The case is Rodriguez v. United States.