Connect Metamask App from Android to an Android app created project using Javascript only and CDNs - Stack Overflow

admin2025-04-16  5

I have the following code, it is not working

// Handle connect button click
connectButton.addEventListener('click', async () => {
    try {
        // Initialize WalletConnect
        const walletConnectProvider = new WalletConnect.default({
            bridge: ";,  // Optional: WalletConnect bridge URL
        });

        // Enable the provider to establish the connection
        await walletConnectProvider.enable();

        // Initialize Web3 with WalletConnect provider
        provider = new Web3(walletConnectProvider);

        // Get the accounts
        accounts = await provider.eth.getAccounts();
        const address = accounts[0];

        walletAddressElement.innerText = "Connected: " + address;

        // Toggle button visibility
        connectButton.style.display = 'none';
        disconnectButton.style.display = 'inline-block';
    } catch (error) {
        console.error("Error during connection:", error);
        alert("Connection failed. Please try again.");
    }
});

I tried using the also, but this only opens the metamask application on android, it doesn't request for sign in the request, and approve the connection to the app, the idea is the user connect their metamask wallet, with a button, when clicking the button it redirects to the metamask application (if installed), then approve the request, and go back to the android project application and see their wallet address, also there will be a disconnection button. I have this other code with the metamask.app.link but only opens the application, not receiving the sign request to approve the android app.

document.addEventListener('deviceready', function () {
    console.log("Device is ready!");

    const connectButton = document.getElementById('connectButton');
    const walletAddressElement = document.getElementById('walletAddress');

    function openMetaMask() {
        const metamaskUrl = "/wc"; // Opens MetaMask Mobile
        console.log("Opening MetaMask:", metamaskUrl);
        window.open(metamaskUrl, '_system'); // Opens MetaMask app
    }

    async function connectWallet() {
        try {
            if (!window.ethereum) {
                alert("MetaMask is not installed. Please install it first.");
                return;
            }

            const accounts = await ethereum.request({ method: 'eth_requestAccounts' });
            const account = accounts[0];

            console.log("Connected account:", account);
            walletAddressElement.innerText = "Wallet: " + account;

            // Now, request a signature
            const message = "Sign this message to authenticate with My Cordova App";
            const signature = await ethereum.request({
                method: 'personal_sign',
                params: [message, account]
            });

            console.log("Signature:", signature);
            alert("Signed successfully! Returning to app...");
        } catch (error) {
            console.error("Error:", error);
            alert("Connection or signing failed.");
        }
    }

    connectButton.addEventListener('click', function () {
        openMetaMask();
        setTimeout(connectWallet, 5000); // Wait for MetaMask to open before requesting connection
    });

Do you have any other approach, I am only using cdns and want to be in that way, is there any alternative without using Infura API or reown project id? The only thing I need is to connect, no anything else, no transactions, nothing only sign the approval and then go back to the application.

转载请注明原文地址:http://anycun.com/QandA/1744810121a87931.html