You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

65 lines
2.4 KiB

package com.iflytop.profilometer;
import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.view.WindowManager;
import android.webkit.WebView;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import com.iflytop.profilometer.api.ws.BleWebsocketManager;
import com.iflytop.profilometer.api.ws.DeviceStateWebsocketManager;
import com.iflytop.profilometer.core.bluetooth.BleManager;
import com.iflytop.profilometer.server.HttpServer;
public class MainActivity extends AppCompatActivity {
private BleManager bleManager;
@SuppressLint({"SetJavaScriptEnabled", "MissingInflatedId"})
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);//长亮
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
// 启动 Ktor 内嵌服务器,绑定80端口,传入当前上下文
HttpServer.start(this, 8080);
// 初始化 WebView 并加载内嵌服务器提供的页面
WebView webView = findViewById(R.id.webView);
WebView.setWebContentsDebuggingEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setUseWideViewPort(true);
webView.loadUrl("http://127.0.0.1:8080/");
bleManager = BleManager.getInstance(this);
// 针对 Android 12 及以上版本,请求必要的运行时权限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
bleManager.checkAndRequestPermissions(this);
}
// 检查蓝牙是否开启(如果未开启,则提示用户开启)
bleManager.promptAndEnableBluetooth(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
// Activity 销毁时停止服务器
HttpServer.stop();
bleManager.stopScan();
}
}