public void startScan() {
final List<MyBluetoothDevice> arrayOfFoundBTDevices = new ArrayList<>();
// start looking for bluetooth devices
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
boolean scanStatus = mBluetoothAdapter.startDiscovery();
Timber.d("SCANNING_STATUS : " + scanStatus);
// Discover new devices
// Create a BroadcastReceiver for ACTION_FOUND
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Timber.d("onReceiveSignal");
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the bluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
Timber.d("Discovery Finished ");
AppUtils.showToast(context, "Scanning restart");
mBluetoothAdapter.startDiscovery();
}
}
};
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
context.registerReceiver(mReceiver, filter);
}
Я пытаюсь сканировать близлежащие устройства Bluetooth. Но метод BluetoothAdapter.getDefaultAdapter (). StartDiscovery () возвращает false на уровне API 29, но работает на уровне API 26
------------- Разрешение определено в AndroidManifest. xml ----------- Не могу найти решение для Android 10
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bluetoothexample">
<uses-feature
android:name="android.hardware.bluetooth"
android:required="true" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".FoundBTDevices" />
</application>
</manifest>