蓝牙锁 app 安卓项目 需要把web的dist目录 复制到项目内 再打包apk
he wei
2024-12-20 fb6363025ed78a29aee33553c6d6fd1480d6ca36
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.whyc.lock;
 
import com.getcapacitor.BridgeActivity;
// public class MainActivity extends BridgeActivity {}
 
import android.os.Bundle; // 添加这个导入语句
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
 
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
  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;
}