安卓中扫描特定类型的外围BLE蓝牙设备获取列表完整详细的用例

时间: 2024-03-12 15:44:07 浏览: 15
好的,您可以参考以下的安卓代码,它演示了如何扫描特定类型的外围BLE蓝牙设备并获取列表的完整用例。 ```java import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGattCallback; import android.bluetooth.BluetoothGattCharacteristic; import android.bluetooth.BluetoothGattDescriptor; import android.bluetooth.BluetoothGattService; import android.bluetooth.BluetoothManager; import android.bluetooth.le.BluetoothLeScanner; import android.bluetooth.le.ScanCallback; import android.bluetooth.le.ScanResult; import android.bluetooth.le.ScanSettings; import android.content.Context; import android.os.Handler; import java.util.ArrayList; import java.util.List; import java.util.UUID; public class BleScanner { private static final String TAG = "BleScanner"; private static final long SCAN_PERIOD = 10000; // 扫描时间,单位毫秒 private BluetoothAdapter mBluetoothAdapter; private BluetoothLeScanner mBluetoothLeScanner; private ScanCallback mScanCallback; private Handler mHandler; private boolean mScanning; private List<BluetoothDevice> mDeviceList; private Context mContext; public BleScanner(Context context) { mContext = context; mHandler = new Handler(); BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE); mBluetoothAdapter = bluetoothManager.getAdapter(); mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner(); mDeviceList = new ArrayList<>(); } public void startScan(UUID serviceUuid) { if (mScanning) { return; } ScanSettings settings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build(); mScanCallback = new ScanCallback() { @Override public void onScanResult(int callbackType, ScanResult result) { BluetoothDevice device = result.getDevice(); if (device != null && device.getName() != null && device.getName().contains("MY_DEVICE_NAME")) { BluetoothGatt gatt = device.connectGatt(mContext, false, mGattCallback); gatt.discoverServices(); } } }; mBluetoothLeScanner.startScan(null, settings, mScanCallback); mScanning = true; mHandler.postDelayed(() -> stopScan(), SCAN_PERIOD); } public void stopScan() { if (mScanning && mBluetoothAdapter != null && mBluetoothLeScanner != null) { mBluetoothLeScanner.stopScan(mScanCallback); mScanCallback = null; mScanning = false; } } private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { if (newState == BluetoothGatt.STATE_CONNECTED) { gatt.discoverServices(); } else if (newState == BluetoothGatt.STATE_DISCONNECTED) { gatt.close(); } } @Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { for (BluetoothGattService service : gatt.getServices()) { if (service.getUuid().equals(UUID.fromString("MY_SERVICE_UUID"))) { mDeviceList.add(gatt.getDevice()); } } gatt.disconnect(); } } }; public List<BluetoothDevice> getDeviceList() { return mDeviceList; } } ``` 在上面的代码中,我们首先创建了一个 `BleScanner` 类来扫描外围BLE蓝牙设备。`startScan()` 方法接受一个 `serviceUuid` 作为参数,用于筛选特定类型的设备。在此方法中,我们使用 BluetoothLeScanner 开始扫描,并在发现符合条件的设备时连接设备并发现服务。如果符合条件的服务被发现,则将设备添加到设备列表中。之后,我们使用 `stopScan()` 方法停止扫描。 在扫描结束后,您可以使用 `getDeviceList()` 方法获取扫描到的设备列表。请注意,这个列表只包含筛选后符合条件的设备。 希望这个例子对您有所帮助!

相关推荐

最新推荐

recommend-type

在BLE蓝牙中一次写入超过20字节数据包的方法和技巧.docx

本文比较详细地描述了在低功耗蓝牙模块中如何一次免分包发送超过20字节数据包的方法和技巧,共享了关键方法和代码,该方法和技巧通过作者在真机上运行测试证实完全可行。
recommend-type

BLE蓝牙-4.0-学习笔记

蓝牙4.0 BLE center与peripheral建立连接绑定过程 2 蓝牙4.0 BLE peripheral 广播设置 7 蓝牙4.0 BLE 数据传输 (一) 11 蓝牙4.0 BLE 数据传输 (二) 12 蓝牙4.0 BLE 数据传输(三) 16 蓝牙4.0 BLE 数据传输(四...
recommend-type

Android蓝牙库FastBle的基础入门使用

主要给大家介绍了关于Android蓝牙库FastBle的基础入门使用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

由浅入深,蓝牙4.0/BLE协议栈开发攻略大全(1)

本文将结合TI推出的CC254x SoC 系列,讲解从环境的搭建到蓝牙4.0协议栈的开发来深入学习蓝牙4.0的开发过程。
recommend-type

android Ble 蓝牙4.0 GATT 错误代码

android Ble 蓝牙4.0 GATT 错误代码对照 133 129
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

解释minorization-maximization (MM) algorithm,并给出matlab代码编写的例子

Minorization-maximization (MM) algorithm是一种常用的优化算法,用于求解非凸问题或含有约束的优化问题。该算法的基本思想是通过构造一个凸下界函数来逼近原问题,然后通过求解凸下界函数的最优解来逼近原问题的最优解。具体步骤如下: 1. 初始化参数 $\theta_0$,设 $k=0$; 2. 构造一个凸下界函数 $Q(\theta|\theta_k)$,使其满足 $Q(\theta_k|\theta_k)=f(\theta_k)$; 3. 求解 $Q(\theta|\theta_k)$ 的最优值 $\theta_{k+1}=\arg\min_\theta Q(
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。