BEIJING — Ancient Chinese doctrine has it that when heaven is unhappy with the emperor, it signals its displeasure by raining down disasters such as floods, plagues, and swarms of locusts. The philosopher Mencius said a ruler could lose his “mandate of heaven” if he neglected his responsibility to care for the ordinary people.

These beliefs, still widespread, should concern China’s modern-day emperor, Xi Jinping, as the country battles a coronavirus epidemic and braces for an invasion of locusts.

“He’s got an enemy that, for the first time, is absolutely uncontrollable, in a state that has always ruled by control,” said the Asia Society’s Orville Schell. “You can’t imprison a coronavirus or get it to undergo ‘thought reform.’ ”

Yet the recent introduction of coronavirus-related surveillance measures, many of them unlikely to disappear when the epidemic is over, has also given Xi an opening to assert even tighter control over society.

Drones with cameras chase grandmas down rural roads if they’re spotted not wearing a mask, calling out to them in a robotic voice. Apps enable users to pinpoint the nearest virus case to within 10 yards.

Since ascending to the top jobs in China’s political and military hierarchy in 2012, Xi has moved to make himself the strongest leader since Mao Zedong, the founder of this Communist state. He unleashed an anti-corruption campaign to purge rivals and presided over an Orwellian expansion of the surveillance state.

Two years ago he scrapped term limits, effectively enabling him to stay in power indefinitely, and this year, he has been styling himself as the “People’s Leader,” a term last applied to Mao.

But with China’s death toll from the coronavirus nearing 3,000, Xi faces an unprecedented challenge to his leadership.

“When things get out of kilter, heaven expresses its disfavor,” said Schell, citing the Chinese philosopher Mencius. “Xi has been stripped of his air of invincibility and that is calling into question his ‘mandate of heaven.’ ”

The party’s botched response to the virus has pierced Xi’s armor in multiple ways. His admission that he knew about the outbreak by Jan. 7, two weeks before China activated its emergency response, directly implicated Xi in the initial coverup.

The outbreak has derailed his political and economic agenda by forcing the party to postpone its showcase annual meetings, where cadres rubber-stamp Xi’s plans for the year. Economists say the shutdown of swaths of the country will hammer growth.

More concerning for Xi will be the criticism that has burst into the open, notably with the death of whistleblower doctor Li Wenliang.

Dissent on Chinese social media is quickly erased by the party’s censors, but Chinese-language discussion forums hosted overseas are full of comments about Xi being “gutless” for not going to Wuhan, displaying “tedious” leadership and being an “autocrat.”

Two prominent intellectuals who have criticized him have disappeared, apparently detained by security services.

Xu Zhiyong, a legal scholar and public intellectual, went into hiding before publishing an essay Feb. 4 in which he called Xi “clueless and hopeless” and urged him to step down. Still, he was detained shortly after.

Tsinghua University professor Xu Zhangrun is thought to be under house arrest after he wrote that the coronavirus epidemic “has revealed the rotten core of Chinese governance; the fragile and vacuous heart of the jittering edifice of state has thereby shown up as never before.”

Chinese journalists who have tried to do independent reporting have vanished, most recently Li Zehua, an anchor for the state broadcaster who resigned so he could be a “citizen reporter” in Wuhan.

The party has been battling to hide its missteps.

“With the failure of the central authority to provide even very basic public health information to the public, I think there’s now a lot more skepticism of the party,” said Victor Shih of the University of California at San Diego.

All of this has led to predictions — including from Council on Foreign Relations President Richard Haass — that the coronavirus could bring about a change in the way the party operates and may compel it to be more transparent and responsive to the population. Some columnists have characterized the epidemic as China’s “Chernobyl moment,” a tipping point that precipitates the end of Communist rule akin to the nuclear disaster that hastened the demise of the Soviet Union.

But the opposite is likely true, at least in the near term.

“The novel coronavirus has exposed flaws in Xi’s autocracy: the control on information, the absence of civil society and the lack of transparency have all had human costs,” said Natasha Kassam, a former Australian diplomat in China who is now at the Lowy Institute, a Sydney think tank.

“But rather than prompt a rethink in Beijing, Xi and China’s leaders are more likely to double down on the most repressive elements of the regime.”

With new cases dropping and the World Health Organization praising their response, China’s leaders are likely to feel vindicated, Kassam said.

In addition to effectively putting tens of millions of people under house arrest to contain the outbreak, China’s authorities have harnessed the tools of their techno-authoritarian surveillance state in the name of stopping the epidemic.

More than 200 cities across China have adopted “Alipay Health Code,” a QR code that runs through the payment app operated by tech giant Alibaba’s financial division. Alipay is now working, at the government’s behest, to take the service nationwide.

People are assigned a green, orange or red code, according to their risk of having the coronavirus, and must scan green codes before they can enter stores or restaurants, or take public transportation — an electronic passport for daily life.

The code syncs with payment and messaging apps but in Hangzhou, Alibaba’s hometown, the code can even be linked to a person’s social insurance and medical records, according to local reports. It will soon be required to enter office buildings, factory sites and technology parks.

The New York Times reported that the software’s code shares data, including the user’s location, with the police.

Some officials and experts are talking of keeping these new systems in place after the epidemic, adding them to the arsenal of tech tools at Chinese authorities’ disposal. The government has already promoted a “social credit” system aimed at monitoring and evaluating people’s activities in society, similar to a financial credit score.

Hangzhou’s Communist Party secretary, Zhou Jiangyong, has praised the technology and said the city should look at ways to use such tools more widely.

“We must forge ahead along this path with resolve,” Zhou said Monday, while hailing the development of the QR health code.

Continuing to require health-code scans will “increase the efficiency and lower costs for health-care services” after the outbreak, said Zhou Tao, a professor at the University of Electronic Science and Technology of China.

But human rights advocates outside China and ordinary people inside the country have voiced concerns. “I feel very uncomfortable about this kind of snooping,” one person wrote using a pseudonym on the Weibo microblogging site.

By deploying traditional measures of political control — including censorship, arresting dissidents and citizen journalists, nationalistic propaganda, and tighter surveillance — Xi has strengthened his hand, said Elizabeth Economy, director of Asian studies at the Council on Foreign Relations and author of a book about Xi.

“But this is a short-term victory,” she said. “Over the long term, these actions have the effect of corroding the Communist Party’s legitimacy among the Chinese people. At some point after the virus subsides, Chinese citizens and the rest of the world will likely demand a full accounting of what transpired.”

Still, the coronavirus crisis is unlikely to produce a fundamental change in the way that Xi does business, or to precipitate a return to a more collective style of leadership.

“His approach to governance is rooted in an extraordinary degree of personal and party control,” Economy said, “and he has little interest in opening space for civil society to act independently.”