android - 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;
}
}
解决方案
推荐阅读
- asp.net - ASP.NET Json 无法反序列化当前的 JSON 数组
- bash - 比较来自不同文件的 2 列打印匹配列
- powerbi - 如何在power bi中使用dax查找时间段?
- javascript - 使用 React Hooks 卸载组件时如何访问状态?
- java - 如果架构与数据库结构不同,如何将 graphql 与 jpa 一起使用
- javascript - 阻止来自潜在可信来源的混合内容 (127.0.0.0/8)
- reactjs - TypeError:无法读取 null 的属性“显示”
- python - 从 Faker 获取支持的国家列表
- h3 - 对于使用 H3 进行不精确的地理查询,我们可以考虑五边形等于六边形吗?
- html - 如何在我的登录页面上修复此设计错误?