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.

64 lines
2.4 KiB

4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
  1. package com.iflytop.profilometer;
  2. import android.annotation.SuppressLint;
  3. import android.os.Build;
  4. import android.os.Bundle;
  5. import android.view.WindowManager;
  6. import android.webkit.WebView;
  7. import androidx.activity.EdgeToEdge;
  8. import androidx.appcompat.app.AppCompatActivity;
  9. import androidx.core.graphics.Insets;
  10. import androidx.core.view.ViewCompat;
  11. import androidx.core.view.WindowInsetsCompat;
  12. import com.iflytop.profilometer.api.ws.BleWebsocketManager;
  13. import com.iflytop.profilometer.api.ws.DeviceStateWebsocketManager;
  14. import com.iflytop.profilometer.core.bluetooth.BleManager;
  15. import com.iflytop.profilometer.server.HttpServer;
  16. public class MainActivity extends AppCompatActivity {
  17. private BleManager bleManager;
  18. @SuppressLint({"SetJavaScriptEnabled", "MissingInflatedId"})
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. EdgeToEdge.enable(this);
  23. setContentView(R.layout.activity_main);
  24. getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);//长亮
  25. ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
  26. Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
  27. v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
  28. return insets;
  29. });
  30. // 启动 Ktor 内嵌服务器,绑定80端口,传入当前上下文
  31. HttpServer.start(this, 8080);
  32. // 初始化 WebView 并加载内嵌服务器提供的页面
  33. WebView webView = findViewById(R.id.webView);
  34. WebView.setWebContentsDebuggingEnabled(true);
  35. webView.getSettings().setJavaScriptEnabled(true);
  36. webView.getSettings().setDomStorageEnabled(true);
  37. webView.getSettings().setUseWideViewPort(true);
  38. webView.loadUrl("http://127.0.0.1:8080/");
  39. bleManager = BleManager.getInstance(this);
  40. // 针对 Android 12 及以上版本,请求必要的运行时权限
  41. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
  42. bleManager.checkAndRequestPermissions(this);
  43. }
  44. // 检查蓝牙是否开启(如果未开启,则提示用户开启)
  45. bleManager.promptAndEnableBluetooth(this);
  46. }
  47. @Override
  48. protected void onDestroy() {
  49. super.onDestroy();
  50. // Activity 销毁时停止服务器
  51. HttpServer.stop();
  52. bleManager.stopScan();
  53. }
  54. }