Android蓝牙低功耗扫描并显示结果
创始人
2024-10-08 21:01:39
0次

以下是一个使用Android蓝牙低功耗(BLE)扫描并显示结果的示例代码:

首先,在你的Android项目中创建一个新的Java类,命名为BluetoothLeScanner。在该类中添加以下代码:

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.Handler;
import android.os.Looper;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class BluetoothLeScanner {
    private static final String TAG = "BluetoothLeScanner";
    private static final long SCAN_PERIOD = 10000; // 扫描持续时间(毫秒)

    private BluetoothAdapter mBluetoothAdapter;
    private BluetoothLeScanner mBluetoothLeScanner;
    private boolean mScanning;
    private Handler mHandler;
    private List mDeviceList;
    private OnScanListener mListener;

    public BluetoothLeScanner(Context context) {
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
        mHandler = new Handler(Looper.getMainLooper());
        mDeviceList = new ArrayList<>();
    }

    public void setOnScanListener(OnScanListener listener) {
        mListener = listener;
    }

    public void startScan() {
        if (!mScanning) {
            Log.d(TAG, "Starting BLE scan...");
            mDeviceList.clear();
            ScanSettings settings = new ScanSettings.Builder()
                    .setScanMode(ScanSettings.SCAN_MODE_LOW_POWER)
                    .build();
            mBluetoothLeScanner.startScan(null, settings, mScanCallback);
            mScanning = true;
            mHandler.postDelayed(this::stopScan, SCAN_PERIOD);
        } else {
            Log.d(TAG, "BLE scan already in progress.");
        }
    }

    public void stopScan() {
        if (mScanning) {
            Log.d(TAG, "Stopping BLE scan...");
            mBluetoothLeScanner.stopScan(mScanCallback);
            mScanning = false;
            if (mListener != null) {
                mListener.onScanComplete(mDeviceList);
            }
        } else {
            Log.d(TAG, "No BLE scan in progress.");
        }
    }

    private ScanCallback mScanCallback = new ScanCallback() {
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            BluetoothDevice device = result.getDevice();
            if (!mDeviceList.contains(device)) {
                mDeviceList.add(device);
                Log.d(TAG, "Found BLE device: " + device.getName());
            }
        }
    };

    public interface OnScanListener {
        void onScanComplete(List devices);
    }
}

然后,在你的Activity中使用BluetoothLeScanner类来执行扫描并显示结果。例如,你可以在onCreate方法中添加以下代码:

public class MainActivity extends AppCompatActivity {
    private BluetoothLeScanner mBluetoothLeScanner;

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

        mBluetoothLeScanner = new BluetoothLeScanner(this);
        mBluetoothLeScanner.setOnScanListener(new BluetoothLeScanner.OnScanListener() {
            @Override
            public void onScanComplete(List devices) {
                // 在这里更新UI显示扫描结果
                for (BluetoothDevice device : devices) {
                    Log.d(TAG, "Device: " + device.getName() + ", Address: " + device.getAddress());
                }
            }
        });

        // 开始扫描
        mBluetoothLeScanner.startScan();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 停止扫描
        mBluetoothLeScanner.stopScan();
    }
}

这个示例代码使用了BluetoothLeScanner类来处理BLE扫描和结果的显示。你可以根据自己的需求进行修改和扩展。

相关内容

热门资讯

专业讨论!wepoker模拟器... wepoker模拟器哪个好用AI 游戏攻略黑科技上线本地缓存功能,离线查阅能力开挂。提前下载对应游戏...
重大消息!wpk有那种辅助吗,... 重大消息!wpk有那种辅助吗,WPK有挂吗教程(先前有开挂)AI 智能体黑科技再上新,自动化能力开挂...
推荐几款新版!wepoker底... 您好:wepoker底牌透视这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用...
总算了解!wpk有作弊吗,德普... 总算了解!wpk有作弊吗,德普之星外卦神器软件教程(竟然有开挂)AI 智能体黑科技再上新,自动化能力...
大神推荐!wepoker透视脚... wepoker透视脚本免费AI 游戏攻略黑科技上线本地缓存功能,离线查阅能力开挂。提前下载对应游戏攻...
避坑细节!如何下载德普之星辅助... 『标题:当前标题』主打赛后复盘的 AI 游戏攻略黑科技走红,对局总结能力开挂。上传对局回放或者战绩截...
一分钟了解!!wepoker辅... AI 黑科技赛道又上新,这款智能处理工具堪称效率开挂,专门面向短视频创作者打造。它打通脚本撰写、AI...
玩家科普!wpk真的有透视嘛,... 玩家科普!wpk真的有透视嘛,WPK透视脚本工具(好像存在有辅助挂)AI 智能体黑科技再上新,自动化...
一分钟揭秘!德普之星辅助工具如... 您好:德普之星辅助工具如何打开这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多...
大家学习交流!wepoker游... 您好:wepoker游戏安装教程这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很...