BLE扫描设备
创始人
2024-12-22 16:30:11
0

以下是一个使用Android的BluetoothLeScanner类进行BLE设备扫描的简单示例代码:

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
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.Bundle;
import android.util.Log;

import java.util.List;

public class MainActivity extends Activity {

    private static final String TAG = "BLEScanner";
    private BluetoothAdapter mBluetoothAdapter;
    private BluetoothLeScanner mBluetoothLeScanner;
    private ScanCallback mScanCallback;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 获取BluetoothAdapter
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        // 检查设备是否支持BLE
        if (mBluetoothAdapter == null || !mBluetoothAdapter.isMultipleAdvertisementSupported()) {
            Log.e(TAG, "设备不支持BLE");
            return;
        }

        // 获取BluetoothLeScanner
        mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();

        // 设置扫描回调
        mScanCallback = new ScanCallback() {
            @Override
            public void onScanResult(int callbackType, ScanResult result) {
                BluetoothDevice device = result.getDevice();
                Log.d(TAG, "发现设备: " + device.getName() + ", " + device.getAddress());
            }

            @Override
            public void onBatchScanResults(List results) {
                for (ScanResult result : results) {
                    BluetoothDevice device = result.getDevice();
                    Log.d(TAG, "发现设备: " + device.getName() + ", " + device.getAddress());
                }
            }

            @Override
            public void onScanFailed(int errorCode) {
                Log.e(TAG, "扫描失败: " + errorCode);
            }
        };

        // 设置扫描参数
        ScanSettings scanSettings = new ScanSettings.Builder()
                .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
                .build();

        // 开始扫描
        mBluetoothLeScanner.startScan(null, scanSettings, mScanCallback);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        // 停止扫描
        mBluetoothLeScanner.stopScan(mScanCallback);
    }
}

要注意的是,为了进行BLE扫描,需要在AndroidManifest.xml文件中添加以下权限:



希望这个示例能帮助到你!

相关内容

热门资讯

专业讨论“中至赣牌圈祈福有用吗... 专业讨论“中至赣牌圈祈福有用吗”哈糖大菠萝万能挂(从来存在有挂)1、不需要AI权限,帮助你快速的进行...
最新通报“新永和源代码”wep... 最新通报“新永和源代码”wepoker作弊视频(总是真的是有挂);支持2-10人实时对战,虚拟庄家发...
技术分享“传送屋激k看底牌辅助... 技术分享“传送屋激k看底牌辅助软件”wpk辅助购买(最初有挂);玩家必备必赢加哟《136704302...
推荐一款“雀友会广东潮汕苹果开... 推荐一款“雀友会广东潮汕苹果开挂”wepoker破解游戏盒子(原生有挂);原来确实真的有挂(需添加指...
避坑细节“新玉海楼茶苑13张万... 避坑细节“新玉海楼茶苑13张万能辅助器”聚星ai辅助工具收费多少(往昔是有挂);实战中需综合运用上述...
重大来袭“钱柜手游辅助”wep... 重大来袭“钱柜手游辅助”wepoker作弊视频(真是是真的有挂)是一款可以让一直输的玩家,快速成为一...
我来教大家“微乐辅助脚本”po... 我来教大家“微乐辅助脚本”pokemmo免费脚本(固有有挂);支持多人共享记分板与复盘,通过邀请好友...
大家学习交流“手游奇迹陕西辅助... 大家学习交流“手游奇迹陕西辅助工具”wepoker数据分析(切实是有挂)1、玩家可以在手游奇迹陕西辅...
一起来讨论“情怀西游辅助”we... 一起来讨论“情怀西游辅助”wepoker到底有没有透视(一贯真的有挂);建议优先通过情怀西游辅助透明...
避坑细节“丽水都莱脚本辅助”w... 避坑细节“丽水都莱脚本辅助”wepoker俱乐部辅助(原来是真的有挂)1、许多玩家不知道丽水都莱脚本...