Android BLE扫描与PendingIntent
创始人
2024-09-30 10:00:13
0

要进行Android BLE扫描并使用PendingIntent来处理扫描结果,可以按照以下步骤进行操作:

  1. 在AndroidManifest.xml文件中添加必要的权限和服务声明:





  1. 创建一个扫描服务ScanService类,该类继承自Service,并实现BLE扫描的逻辑:
public class ScanService extends Service {
    private BluetoothAdapter mBluetoothAdapter;
    private BluetoothLeScanner mBluetoothLeScanner;
    private PendingIntent mPendingIntent;

    private final BroadcastReceiver mScanReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) {
                int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF);
                if (state == BluetoothAdapter.STATE_ON) {
                    startScan();
                }
            }
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
        IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
        registerReceiver(mScanReceiver, filter);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        mPendingIntent = intent.getParcelableExtra("pendingIntent");
        if (mBluetoothAdapter.isEnabled()) {
            startScan();
        }
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mScanReceiver);
        stopScan();
    }

    private void startScan() {
        ScanSettings settings = new ScanSettings.Builder()
                .setScanMode(ScanSettings.SCAN_MODE_LOW_POWER)
                .build();
        ScanFilter filter = new ScanFilter.Builder()
                .build();
        List filters = new ArrayList<>();
        filters.add(filter);
        mBluetoothLeScanner.startScan(filters, settings, mScanCallback);
    }

    private void stopScan() {
        mBluetoothLeScanner.stopScan(mScanCallback);
    }

    private ScanCallback mScanCallback = new ScanCallback() {
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            super.onScanResult(callbackType, result);
            // 处理扫描结果
            if (mPendingIntent != null) {
                // 将扫描结果发送给PendingIntent
                try {
                    Intent intent = new Intent();
                    intent.putExtra("deviceName", result.getDevice().getName());
                    mPendingIntent.send(ScanService.this, 0, intent);
                } catch (PendingIntent.CanceledException e) {
                    e.printStackTrace();
                }
            }
        }
    };

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
  1. 在需要进行BLE扫描的地方,创建一个PendingIntent,并将其传递给ScanService启动扫描:
private void startBLEScan() {
    Intent intent = new Intent(this, ScanService.class);
    PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    intent.putExtra("pendingIntent", pendingIntent);
    startService(intent);
}
  1. 在接收扫描结果的地方,创建一个BroadcastReceiver来接收PendingIntent发送的结果:
private final BroadcastReceiver mScanResultReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String deviceName = intent.getStringExtra("deviceName");
        // 处理扫描结果
    }
};
  1. 在需要接收扫描结果的地方注册BroadcastReceiver,并在不需要时进行注销:
@Override
protected void onResume() {
    super.onResume();
    IntentFilter filter = new IntentFilter();
    filter.addAction(getPackageName() + ".SCAN_RESULT_ACTION");
    registerReceiver(mScanResultReceiver, filter);
}

@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver(mScanResultReceiver);
}

这样就可以通过PendingIntent来处理BLE扫描的结果了。当扫描到设备时,ScanService会将结果发送给PendingIntent,然后通过BroadcastReceiver接收结果并进行处理。

相关内容

热门资讯

六分钟辅助挂!黑桃k大厅辅助(... 六分钟辅助挂!黑桃k大厅辅助(透视)皮皮衡阳字牌黑科技(详细透视外开挂教程)是一款可以让一直输的玩家...
四分钟辅助挂!八闽掌上辅助(透... 四分钟辅助挂!八闽掌上辅助(透视)乐乐围棋入门辅助(详细透视外开挂教程)是一款可以让一直输的玩家,快...
第九分钟辅助挂!约局辅助透视开... 第九分钟辅助挂!约局辅助透视开挂有吗(透视)金州水鱼挂怎么购买(详细透视外开挂教程),您好,约局辅助...
1分钟辅助挂!打哈儿床将脚本(... 1分钟辅助挂!打哈儿床将脚本(透视)新518互游插件下载(详细透视外开挂教程);1分钟辅助挂!打哈儿...
第四分钟辅助挂!四川游戏家园破... 第四分钟辅助挂!四川游戏家园破解版(透视)瓜瓜丰城双剑旧版攻略(详细透视外开挂教程)1、这是跨平台的...
第八分钟辅助挂!同城游辅助器(... 第八分钟辅助挂!同城游辅助器(透视)金华佛手在线辅助(详细透视外开挂教程);无聊就玩这款同城游辅助器...
9分钟辅助挂!科乐天天踢填大坑... 9分钟辅助挂!科乐天天踢填大坑游戏攻略(透视)葫芦娃辅助软件(详细透视外开挂教程)1、快速入门:当你...
7分钟辅助挂!手游挂机辅助免费... 1、7分钟辅助挂!手游挂机辅助免费版(透视)杭州边锋网络游戏辅助(详细透视外开挂教程)。2、杭州边锋...
第3分钟辅助挂!安装胡乐辅助脚... 第3分钟辅助挂!安装胡乐辅助脚本(透视)顺欣茶楼辅助视频(详细透视外开挂教程);一、顺欣茶楼辅助视频...
1分钟辅助挂!四川途游小程序辅... 1分钟辅助挂!四川途游小程序辅助破解版(透视)杭州都莱辅助软件(详细透视外开挂教程)1、用户打开应用...