【问题标题】:How to generate and store UUID locally on the user's phone?如何在用户手机本地生成和存储UUID?
【发布时间】:2020-08-03 07:26:49
【问题描述】:

我想为用户生成一个 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 每次安装都会改变对我来说并不重要,我只想生成它并在信号中公布。

【问题讨论】:

  • 你在模拟器上运行它吗?
  • 不,我正在手机上运行它

标签: java android android-studio bluetooth-lowenergy transmission


【解决方案1】:

在蓝牙 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,不会超过包的大小。

【讨论】:

  • 2020-08-03 15:38:22.817 26082-26082/com.example.beacondistance E/.beacondistanc: Invalid ID 0x00000001. 2020-08-03 15:38:22.820 26082-26082/com.example.beacondistance E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.beacondistance, PID: 26082 android.content.res.Resources$NotFoundException: String resource ID #0x1
  • 需要有关错误的更多详细信息。能详细分享一下吗?
猜你喜欢
  • 1970-01-01
  • 2016-11-05
  • 2011-04-14
  • 1970-01-01
  • 2020-12-19
  • 2021-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多