首页 > 解决方案 > NanoHTTPd 使用网络蓝牙 API

问题描述

我制作了一个简单的网络应用程序。这包含html和js。在网页上,您应该能够扫描蓝牙设备。这是通过 Web 蓝牙 API 实现的。

如果我使用应用程序“AWebServer”在 android 上托管 www 目录,然后使用 chrome 浏览器访问此页面“http://localhost:8080”,这可以正常工作。

但我想直接从一个单独的应用程序中获取所有内容。所以我制作了一个应用程序,它使用 NanoHTTPd 来托管 assets 文件夹中的网页。该网站到目前为止工作正常,只有连接蓝牙设备的按钮不再工作。

Web蓝牙的代码来自:https ://github.com/hakatashi/giiker

import GiiKER from 'giiker';

// Note: To use Web Bluetooth API trigger action such as button click is required
const button = document.querySelector('button#connect');
button.addEventListener('click', async () => {
    const giiker = await GiiKER.connect();
    giiker.on('move', (move) => {
        console.log(move.face); //=> "F"
        console.log(move.amount); //=> -1
        console.log(move.notation); //=> "F'"
    });
})

NanoHTTPd 的代码如下:

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private SimpleWebServer server = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startLocalServer(3000, "www", true, true );
    }


    public void startLocalServer(int port, String root, Boolean localhost, Boolean keepAlive) {
        try {
            String[] filePathList =(getAssets().list("www"));
            for (String s : filePathList) {
                System.out.println(s);
            }
            MainActivity.copyFolderFromAssets(this.getApplicationContext(), "www", getApplicationInfo().dataDir + '/' + root);
            File www_root = new File(getApplicationInfo().dataDir + '/' + root);
            System.out.println(www_root.listFiles());
            server = new WebServer("localhost", port, www_root.getCanonicalFile());
            server.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void copyFolderFromAssets(Context context, String rootDirFullPath, String targetDirFullPath) {
        System.out.println("copyFolderFromAssets " + "rootDirFullPath-" + rootDirFullPath + " targetDirFullPath-" + targetDirFullPath);
        File file = new File(targetDirFullPath);
        if (!file.exists()) {
            new File(targetDirFullPath).mkdirs();
        }
        try {
            String[] listFiles = context.getAssets().list(rootDirFullPath);
            for (String string : listFiles) {
                if (isFileByName(string)) {
                    copyFileFromAssets(context, rootDirFullPath + "/" + string, targetDirFullPath + "/" + string);
                } else {
                    String childRootDirFullPath = rootDirFullPath + "/" + string;
                    String childTargetDirFullPath = targetDirFullPath + "/" + string;
                    new File(childTargetDirFullPath).mkdirs();
                    copyFolderFromAssets(context, childRootDirFullPath, childTargetDirFullPath);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void copyFileFromAssets(Context context, String assetsFilePath, String targetFileFullPath) {
        InputStream assestsFileInputStream;
        try {
            assestsFileInputStream = context.getAssets().open(assetsFilePath);
            FileOutputStream fOS = new FileOutputStream(new File(targetFileFullPath));
            int length = -1;
            byte[] buf = new byte[1024];
            while ((length = assestsFileInputStream.read(buf)) != -1) {
                fOS.write(buf, 0, length);
            }
            fOS.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static boolean isFileByName(String str) {
        return str.contains(".");
    }
}

网络服务器.java

public class WebServer extends SimpleWebServer
{
    public WebServer(String localAddr, int port, File wwwroot) throws IOException {
        super(localAddr, port, wwwroot, true, "*");

        mimeTypes().put("xhtml", "application/xhtml+xml");
        mimeTypes().put("opf", "application/oebps-package+xml");
        mimeTypes().put("ncx", "application/xml");
        mimeTypes().put("epub", "application/epub+zip");
        mimeTypes().put("otf", "application/x-font-otf");
        mimeTypes().put("ttf", "application/x-font-ttf");
        mimeTypes().put("js", "application/javascript");
        mimeTypes().put("svg", "image/svg+xml");
    }

    @Override
    protected boolean useGzipWhenAccepted(Response r) {
        return super.useGzipWhenAccepted(r) && r.getStatus() != Response.Status.NOT_MODIFIED;
    }
}

标签: androidnanohttpdweb-bluetooth

解决方案


推荐阅读