如何获取android中的ble设备的uuid

时间:2018-02-14 09:57:48

标签: android bluetooth-lowenergy uuid

我是Android开发的新手。我正在使用ble scanningconnection等。我想根据UUID显示特定的ble设备。 如何使用UUID 扫描播放设备。

1 个答案:

答案 0 :(得分:1)

您可以在BluetoothLeScanner类中使用startScan (List<ScanFilter> filters, ScanSettings settings, ScanCallback callback)方法。在ScanFilter中设置服务UUID,以根据UUID显示特定的BLE设备。

扫描示例:

@Override
public void startScan(final BleDeviceScanCallback callback) {
    mCallback = callback;

    List<ScanFilter> filters = new ArrayList<ScanFilter>();
    if (mServiceUuids != null && mServiceUuids.length > 0) {
        for (UUID uuid : mServiceUuids) {
            ScanFilter filter = new ScanFilter.Builder().setServiceUuid(
                    new ParcelUuid(uuid)).build();
            filters.add(filter);
        }
    }
    ScanSettings settings = new ScanSettings.Builder().build();

    mBleScanner = mBluetoothAdapter.getBluetoothLeScanner();
    if (mBleScanner != null) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            mBleScanner.startScan(filters, settings, mScanCallback);
        } else {
            // Unless required permissions were acquired, scan does not start.
            if (BleUtils.isBLEPermission(mContext)) {
                mBleScanner.startScan(filters, settings, mScanCallback);
            }
        }
    }
}

详情请参阅here

可以找到更多API详细信息here

了解Scan Filters here

Google蓝牙LE中央应用示例here

请注意: BluetoothAdapter.startLeScan方法在API级别21中已弃用。请改用startScan(List,ScanSettings,ScanCallback)。