Android 7.0设备蓝牙扫描无结果求助(同代码适配4.4.4正常)
Hey there! I’ve run into this exact issue before—your Android 7.0 device failing to scan for Bluetooth devices while the 4.4.4 one works with the same code almost always boils down to permission changes and API deprecation introduced in Android 6.0 (API 23). Let’s walk through the fixes step by step:
1. Fix Location Permissions (Critical for Android 6.0+)
Android 6.0 and later require dynamic location permissions for Bluetooth scanning (the system uses location data to improve scan accuracy). Your 4.4.4 device only needs static permissions declared in the manifest, but 7.0 needs both:
Step 1: Add Permissions to AndroidManifest.xml
<!-- Basic Bluetooth permissions --> <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <!-- Required for Bluetooth scanning on Android 6.0+ --> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Step 2: Request Permissions Dynamically in MainActivity
Before calling your BluetoothService’s scan function, check if you have the location permission. If not, request it:
private static final int REQUEST_LOCATION_PERMISSION = 1001; // Call this before starting the scan private void checkLocationPermission() { if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION_PERMISSION); } else { // Permission granted, start scan via BluetoothService mBluetoothService.startScan(); } } // Handle permission request result @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == REQUEST_LOCATION_PERMISSION) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { mBluetoothService.startScan(); } else { // User denied permission—show a message explaining it's needed for scanning Toast.makeText(this, "Location permission is required to scan Bluetooth devices", Toast.LENGTH_LONG).show(); } } }
2. Replace Deprecated Scan API
Android 5.0 (API 21) introduced BluetoothLeScanner, and the old BluetoothAdapter.startLeScan() method was deprecated in Android 6.0. If your code uses the old API, it might not work reliably on 7.0. Update your BluetoothService to use the modern scanner:
// In your BluetoothService private BluetoothLeScanner mBluetoothLeScanner; private ScanCallback mScanCallback; // Initialize scanner and callback when service starts private void initScanner() { BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); mBluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner(); mScanCallback = new ScanCallback() { @Override public void onScanResult(int callbackType, ScanResult result) { super.onScanResult(callbackType, result); // Process the found device: result.getDevice() gives you the BluetoothDevice BluetoothDevice device = result.getDevice(); // Send this back to MainActivity via broadcast or interface } @Override public void onScanFailed(int errorCode) { super.onScanFailed(errorCode); // Handle scan failure—log the error code for debugging Log.e("BluetoothService", "Scan failed with error code: " + errorCode); } }; } // Start scan method public void startScan() { if (mBluetoothLeScanner != null && mScanCallback != null) { ScanSettings scanSettings = new ScanSettings.Builder() .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) // Adjust mode based on your needs .build(); mBluetoothLeScanner.startScan(null, scanSettings, mScanCallback); } } // Don't forget to stop the scan when done! public void stopScan() { if (mBluetoothLeScanner != null && mScanCallback != null) { mBluetoothLeScanner.stopScan(mScanCallback); } }
3. Ensure Location Services Are Enabled
Even with permissions, Android 6.0+ blocks Bluetooth scans if the system’s Location Services are turned off. Add a check in your MainActivity:
private void checkLocationServices() { LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); boolean isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); if (!isGpsEnabled && !isNetworkEnabled) { // Prompt user to enable location services Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivity(intent); } }
4. Verify Bluetooth is Enabled
Double-check that Bluetooth is turned on before starting the scan (this should work on both versions, but it’s good to confirm):
private static final int REQUEST_ENABLE_BT = 1002; private void checkBluetoothEnabled() { BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (bluetoothAdapter == null) { // Device doesn't support Bluetooth Toast.makeText(this, "Bluetooth is not supported on this device", Toast.LENGTH_LONG).show(); return; } if (!bluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } }
Final Notes
Put it all together: in your MainActivity, first check Bluetooth is enabled, then check location permissions, then check location services, and finally trigger the scan via your updated BluetoothService. This should resolve the "no devices found" issue on your Android 7.0 device.
内容的提问来源于stack exchange,提问作者조한나

