<!-- Android 12以下才需要定位权限, Android 9以下官方建议申请ACCESS_COARSE_LOCATION --><uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /><uses-permission android:name="android.permission.BLUETOOTH" /><uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /><!-- Android 12在不申请定位权限时,必须加上android:usesPermissionFlags="neverForLocation",否则搜不到设备 --><uses-permissionandroid:name="android.permission.BLUETOOTH_SCAN"android:usesPermissionFlags="neverForLocation"tools:targetApi="s" /><uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" /><uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
Andoird蓝牙java:
@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 请求权限requestPermissions();// settingBlueTooth();
// scanLeDevice(true);// 初始化蓝牙适配器bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();// 开始搜索蓝牙设备if (bluetoothAdapter != null && bluetoothAdapter.isEnabled()) {if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {return;}bluetoothAdapter.startDiscovery();// 注册广播接收器来接收蓝牙搜索结果IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);bluetoothReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();if (BluetoothDevice.ACTION_FOUND.equals(action)) {// 从Intent中获取蓝牙设备对象BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);// 打印蓝牙设备的MAC地址if (device != null) {Log.i(TAG, "Found device: " + device.getAddress());// 如果设备的MAC A4开头,就是我们的设备if (device.getAddress().startsWith("A4")) {Log.i(TAG, "Found our device: " + device.getAddress());}}}}};registerReceiver(bluetoothReceiver, filter);} else {Log.e(TAG, "Bluetooth is not available");}EdgeToEdge.enable(this);setContentView(R.layout.activity_main);ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);return insets;});}@Overrideprotected void onDestroy() {super.onDestroy();// 停止蓝牙设备搜索并注销广播接收器if (bluetoothAdapter != null) {if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {return;}bluetoothAdapter.cancelDiscovery();}unregisterReceiver(bluetoothReceiver);}
请求权限requestPermissions
private void requestPermissions() {if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH) != PackageManager.PERMISSION_GRANTED|| ContextCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_ADMIN) != PackageManager.PERMISSION_GRANTED|| ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED|| ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {// 如果没有获取到权限,请求权限ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.BLUETOOTH,android.Manifest.permission.BLUETOOTH_ADMIN,android.Manifest.permission.ACCESS_FINE_LOCATION,android.Manifest.permission.WRITE_EXTERNAL_STORAGE,}, PERMISSION_REQUEST_CODE);}} else {if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED|| ContextCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_ADVERTISE) != PackageManager.PERMISSION_GRANTED|| ContextCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED|| ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED|| ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {// 如果没有获取到权限,请求权限ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.BLUETOOTH_SCAN,android.Manifest.permission.BLUETOOTH_ADVERTISE,android.Manifest.permission.BLUETOOTH_CONNECT,android.Manifest.permission.ACCESS_FINE_LOCATION,android.Manifest.permission.WRITE_EXTERNAL_STORAGE,}, PERMISSION_REQUEST_CODE);}}// 如果权限已经被授予,直接进行蓝牙功能检查和定位功能检查}@Overrideprotected void onDestroy() {super.onDestroy();// 停止蓝牙设备搜索并注销广播接收器if (bluetoothAdapter != null) {if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {return;}bluetoothAdapter.cancelDiscovery();}unregisterReceiver(bluetoothReceiver);}