本文记录一个不常用的扫描接口:
public int startScan (List<ScanFilter> filters,
ScanSettings settings,
PendingIntent callbackIntent)
官方描述:
Start Bluetooth LE scan using a PendingIntent. The scan results will be delivered via the PendingIntent. Use this method of scanning if your process is not always running and it should be started when scan results are available.
描述中说,如果你的进程并不是一直在运行中,可以使用此方法来接收广播数据,进程会被启动。
但是,实际操作中,前台或者后台运行会接收到intent;手动杀后台后,并没有接收到Intent。
public class BleBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
List<ScanResult> scanResults = intent.getParcelableArrayListExtra(EXTRA_LIST_SCAN_RESULT);
if (!scanResults.isEmpty()) {
Log.d("Receiver", "BleBroadcastReceiver.str: " + scanResults.get(0).toString());
}
}
}
Intent perIntent = new Intent(mContext, BleBroadcastReceiver.class);
PendingIntent callbackIntent = PendingIntent.getBroadcast(mContext, 1, perIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBleScanner.startScan(provideScanFilter(filters), provideScanSetting(settings), callbackIntent);
|