在 Android 上使用 BLE(蓝牙低功耗)的应用程序需要检查以下权限:android.permission.BLUETOOTH、android.permission.BLUETOOTH_ADMIN和android.permission.ACCESS_COARSE_LOCATION。为了检查这些权限,您可以编写以下代码:
//检查是否可以启用蓝牙 final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter(); if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); }
//检查是否有所需权限 if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_LOCATION_PERMISSION); }
//处理权限请求结果 @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { if (requestCode == REQUEST_LOCATION_PERMISSION) { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { //已授予权限,执行您的操作 } else { //拒绝授予权限,无法执行操作 } } }
通过上述代码,在Android应用中检查BLE所需的权限并请求所需的权限,然后处理请求的结果。