首页 > 解决方案 > 如何在用户手机本地生成和存储UUID?

问题描述

我想为用户生成一个 UUID 并将其存储在本地,以便在传输低功耗蓝牙信号期间使用它。

这是我尝试过的:

为用户生成 UUID 的类:

public class Installation{
    private static String uniqueID = null;
    private static final String PREF_UNIQUE_ID = "PREF_UNIQUE_ID";
    public synchronized static String id(Context context) {
        if (uniqueID == null) {
            SharedPreferences sharedPrefs = context.getSharedPreferences(
                    PREF_UNIQUE_ID, Context.MODE_PRIVATE);
            uniqueID = sharedPrefs.getString(PREF_UNIQUE_ID, null);
            if (uniqueID == null) {
                uniqueID = UUID.randomUUID().toString();
                SharedPreferences.Editor editor = sharedPrefs.edit();
                editor.putString(PREF_UNIQUE_ID, uniqueID);
                editor.commit();
            }
        }    return uniqueID;
    }
}

这是传输代码:

AdvertisingSetParameters parameters = new AdvertisingSetParameters.Builder()
                .setInterval(advInterval)
                .setTxPowerLevel(AdvertisingSetParameters.TX_POWER_MEDIUM)
                .setConnectable(true)
                .build();

        deviceId = Installation.id ( this );
        ParcelUuid pUuid = new ParcelUuid( UUID.fromString( deviceId ) );
        AdvertiseData data = new AdvertiseData.Builder()
                .addServiceUuid( pUuid )
                .addServiceData( pUuid, "Data".getBytes( StandardCharsets.UTF_8 ) )
                .build();

        mAdvertiseCallback = new AdvertisingSetCallback () {

            @Override
            public void onAdvertisingSetStarted(AdvertisingSet advertisingSet,
                                                int txPower,
                                                int status) {
                super.onAdvertisingSetStarted (advertisingSet, txPower, status);
                count += 1 ;
                mPacketsSent.setText ( count );
                Toast.makeText( BluetoothActivity.this, "Started "+ status, Toast.LENGTH_LONG ).show ();

            }

但我不断收到错误: Unknown bits set in runtime_flags: 0x28000

2020-08-03 12:30:52.871 31040-31040/? E/PswFrameworkFactory:  Reflect exception getInstance: java.lang.ClassNotFoundException: com.oppo.common.PswFrameworkFactoryImpl
2020-08-03 12:30:52.990 31040-31076/com.example.beacondistance E/Perf: Fail to get file list com.example.beacondistance
2020-08-03 12:30:52.990 31040-31076/com.example.beacondistance E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array

我在上面的代码块中做错了什么?UUID 每次安装都会改变对我来说并不重要,我只想生成它并在信号中进行广告。

标签: javaandroidandroid-studiobluetooth-lowenergytransmission

解决方案


在蓝牙 LE 广告的情况下,您可以发送 31 个字节的数据。如果添加 Tx power/Service UUID/Service 数据,也会占用空间。而且也有可能是硬件问题,所以我建议你在不同的设备上也进行测试。我制作了一个 Contact Tracer 应用程序,所以我知道您可能面临的情况。我正在分享对我有用的广告代码。

private void advertise() {
        advertiser = bluetoothAdapter.getBluetoothLeAdvertiser();

        AdvertiseSettings settings = new AdvertiseSettings.Builder()
                .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY)
                .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH)
                .setConnectable(false)
                .setTimeout(0)
                .build();

        ParcelUuid pUuid = new ParcelUuid(your_uuid);

        final AdvertiseData data = new AdvertiseData.Builder()
                .setIncludeDeviceName(false)
                .setIncludeTxPowerLevel(false)
                .addServiceUuid(pUuid)
                .addManufacturerData(DATA_MARKER, DATA_VALUE)
                .build();

        advertiser.startAdvertising( settings, data, advertisingCallback );
}

几乎没有什么我想说的,当你添加 UUID 时,你已经使用了 16 个字节。所以对于 DATA_MARKER 和 DATA_VALUE 你可以选择 int,它不会超过数据包的大小。


推荐阅读