蓝牙锁 app 安卓项目 需要把web的dist目录 复制到项目内 再打包apk
he wei
2025-03-14 8eb7f251a6394840f60fe56079097f5f5c0492b1
android/app/src/main/java/com/whyc/lock/MainActivity.java
@@ -1,34 +1,19 @@
package com.whyc.lock;
import android.os.Build;
import android.os.Bundle;
import com.getcapacitor.BridgeActivity;
// public class MainActivity extends BridgeActivity {}
import android.os.Bundle; // 添加这个导入语句
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.webkit.WebView;
public class MainActivity extends BridgeActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // 打开安卓系统蓝牙
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (bluetoothAdapter != null && !bluetoothAdapter.isEnabled()) {
      Intent enableBluetoothIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
      startActivityForResult(enableBluetoothIntent, REQUEST_ENABLE_BT);
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 检查 Android 版本是否为 4.4(KitKat)及以上
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // 开启 WebView 的调试功能
            WebView.setWebContentsDebuggingEnabled(true);
        }
    }
  }
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_ENABLE_BT && resultCode == RESULT_OK) {
      // 安卓系统蓝牙已打开
    } else if (requestCode == REQUEST_ENABLE_BT && resultCode == RESULT_CANCELED) {
      // 用户取消打开安卓系统蓝牙
    }
  }
  private static final int REQUEST_ENABLE_BT = 1;
}
}