Несколько дней назад мне удалось успешно заставить Android сканировать устройства BLE, используя PendingIntents
.Сегодня у меня возникают проблемы с получением PendingIntent для получения любых данных обратного вызова, и я пытаюсь понять, почему.
Я использую следующую функцию, как определено в документации для Android:
из https://developer.android.com/reference/android/bluetooth/le/BluetoothLeScanner:
startScan(List<ScanFilter> filters, ScanSettings settings, PendingIntent callbackIntent)
Я добавил следующие разрешения в мой манифест:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Я также объявил, что использую этот получатель как:
<receiver android:name="com.myapp.PendingIntentScanReceiver" >
<intent-filter>
<action android:name="BluetoothDevice.ACTION_FOUND" />
<action android:name="BluetoothDevice.EXTRA_UUID" />
<action android:name="BluetoothDevice.EXTRA_RSSI" />
</intent-filter>
</receiver>
и реализация этого PendingIntentScanReceiver
:
public class PendingIntentScanReceiver extends BroadcastReceiver {
@TargetApi(Build.VERSION_CODES.O)
@Override
public void onReceive(Context context, Intent intent) {
Log.d("TAG", "Hello there.");
}
}
Я использую Samsung Galaxy S8 и читаю здесь , что я не долженпередать null
для моих фильтров сканирования (потому что это не работает на устройствах Samsung, я полагаю?).
Я также пытался переместить вызов startScan
из фоновой службы в MainApplication в MainActivity, нони один из них, кажется, не улучшает результат.
Кстати, мой код startScan
выглядит так:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
ScanSettings settings = (new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_POWER)).build();
List<ScanFilter> listOfFiters = new ArrayList<>();
ScanFilter.Builder builder = new ScanFilter.Builder();
ScanFilter filter = builder.build();
listOfFiters.add(filter);
BluetoothManager bluetoothManager =
(BluetoothManager) getApplicationContext().getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
Intent intent = new Intent(getApplicationContext(), PendingIntentScanReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 42, intent, 0);
int scanRes = bluetoothAdapter.getBluetoothLeScanner().startScan(listOfFiters, settings, pendingIntent);
// (scanRes is always 0 during my tests)
}
Мой вопрос: почему «Привет!».никогда не печатается?Есть что-то, что я забыл?