NanoHTTPd 使用网络蓝牙 API

问题描述

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

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

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

网络蓝牙的代码来自: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(),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,length);
            }
            fOS.flush();
        } catch (IOException e) {
            e.printstacktrace();
        }
    }

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

WebServer.java

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

        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;
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...