protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_bluetooth_list); btAdapter = BluetoothAdapter.getDefaultAdapter(); if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED || checkSelfPermission(Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED || checkSelfPermission(Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[]{ Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.BLUETOOTH_SCAN, Manifest.permission.BLUETOOTH_CONNECT }, 1); Log.d(TAG, "onCreate: reuqest permission"); }
时间: 2024-02-14 16:35:39 浏览: 119
android-web-browser.zip_Androidweαr_Creating_android browser_web
这段代码实现了蓝牙设备列表的显示,并且进行了权限检查和请求。在onCreate()方法中,首先通过setContentView()方法加载了布局文件activity_bluetooth_list.xml,然后获取了默认的蓝牙适配器BluetoothAdapter。接着,通过调用checkSelfPermission()方法对三个权限ACCESS_FINE_LOCATION、BLUETOOTH_SCAN、BLUETOOTH_CONNECT进行检查,如果其中任何一个权限未被授予,则通过调用requestPermissions()方法请求这三个权限,并在Logcat中输出相应的日志信息。
阅读全文