Android BluetoothLeScanner.startScan()方法与传统BluetoothAdapter.startLeScan()方法使用

//以前的方式
BluetoothManager mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);

    BluetoothAdapter mBluetoothAdapter = mBluetoothManager.getAdapter();

    mBluetoothAdapter.startLeScan(mLeScanCallback);

    private BluetoothAdapter.LeScanCallback mLeScanCallback =
        new BluetoothAdapter.LeScanCallback() {
    @Override
    public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mLeDeviceListAdapter.addDevice(device);//mLeDeviceListAdapter封装了BlutoothDevice
                mLeDeviceListAdapter.notifyDataSetChanged();
            }
        });

    }
};

/*
LeDeviceListAdapter的私有属性ArrayList mLeDevices中取得device
BluetoothDevice getDevice(int position) {
return mLeDevices.get(position);
}
*/
final BluetoothDevice device = getDevice(int poisition);

/*
final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);//这是这么个情况,第一个页面已经扫描到了BluetoothDevice,把他的deviceName和Mac地址传入第二个页面,可以直接用这种方式获得一个BlueToothDevice
*/

    mBluetoothGatt = device.connectGatt(this, false, mGattCallback);

//现在的方式

    BluetoothManager blutoothManager=(BluetoothManager)context.getSystemService(Context.BLUETOOTH_SERVICE);

    BluetoothAdapter mBluetoothAdapter=blutoothManager.getAdapter();

    BluetoothLeScanner mBluetoothLeScanner=mBluetoothAdapter.getBluetoothLeScanner();

mBluetoothLeScanner.startScan(buildScanFilters(), buildScanSettings(), mScanCallback);//这三个参数需要自己写方法实现,我们重点关注mScanCallback的实现

//在mScanCallback的实现里面,得到的ScanResult封装了BluetoothDevice

private class SampleScanCallback extends ScanCallback {

    @Override
    public void onBatchScanResults(List<ScanResult> results) {
        super.onBatchScanResults(results);

        for (ScanResult result : results) {
            mAdapter.add(result);
        }
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onScanResult(int callbackType, ScanResult result) {
        super.onScanResult(callbackType, result);

        mAdapter.add(result);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onScanFailed(int errorCode) {
        super.onScanFailed(errorCode);
        Toast.makeText(getActivity(), "Scan failed with error: " + errorCode, Toast.LENGTH_LONG)
                .show();
    }
}

转载于:https://www.cnblogs.com/sunupo/p/10301278.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,以下是BluetoothLeScanner.startScan方法代码示例实现蓝牙扫描: ```java // 获取蓝牙适配器 BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (bluetoothAdapter == null) { // 设备不支持蓝牙 return; } // 获取蓝牙低功耗扫描器 BluetoothLeScanner bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner(); if (bluetoothLeScanner == null) { // 设备不支持蓝牙低功耗 return; } // 定义扫描过滤器,只扫描符合 UUID 的设备 List<ScanFilter> scanFilters = new ArrayList<>(); ScanFilter scanFilter = new ScanFilter.Builder() .setServiceUuid(ParcelUuid.fromString("0000180d-0000-1000-8000-00805f9b34fb")) // 服务 UUID .build(); scanFilters.add(scanFilter); // 定义扫描设置,设置扫描模式和回调 ScanSettings scanSettings = new ScanSettings.Builder() .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) // 低延迟模式 .build(); // 开始扫描 bluetoothLeScanner.startScan(scanFilters, scanSettings, new ScanCallback() { @Override public void onScanResult(int callbackType, ScanResult result) { // 扫描到设备的回调 BluetoothDevice device = result.getDevice(); String name = device.getName(); String address = device.getAddress(); int rssi = result.getRssi(); // 处理扫描结果 Log.d(TAG, "扫描到设备:name=" + name + ", address=" + address + ", rssi=" + rssi); } @Override public void onScanFailed(int errorCode) { // 扫描失败的回调 Log.e(TAG, "扫描失败,errorCode=" + errorCode); } }); // 停止扫描 bluetoothLeScanner.stopScan(new ScanCallback() {}); ``` 希望能够帮到你。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值