如何在用户的手机上生成和存储本地UUID?

huangapple go评论62阅读模式
英文:

How to generate and store UUID locally on the user's phone?

问题

以下是翻译好的代码部分:

我想为用户生成一个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 ();

            }

在上面的代码块中,我保留了原始的Java代码,并删除了不需要翻译的部分。如果您有任何问题或需要进一步的帮助,请随时提出。

英文:

I want to generate a UUID for the user and store it locally for using it during transmission of a Bluetooth Low Energy Signal.

Here's what I have tried:

Class for generating UUID for a user:

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;
}
}

And here's the transmission code:

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 ();
}

But I keep getting the errors:
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

What am I doing wrong in the above block of code? It doesn't matter to me that the UUID would change per installation I just want it to be generated and be advertised in the signal.

答案1

得分: 1

在蓝牙低功耗广告的情况下,您可以发送31字节的数据。如果您添加了传输功率/服务UUID/服务数据,那也会占用空间。而且这也可能是硬件问题,因此我建议您也在不同的设备上进行测试。我制作了一个接触追踪器应用程序,所以我了解您可能会面临的情况。我正在分享对我有效的广告代码。

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类型,这不会超出数据包大小。

英文:

In case of Bluetooth LE Advertising, you can send 31 bytes of data. If you add Tx power/Service UUID/Service data, that will consume spaces also. And it may also a hardware problem, so I recommend that you also test on different devices. I made a Contact Tracer app, so I know the situations you may face. I am sharing the advertising code worked for me.

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 );
}

Few things I want to mention that, when you added the UUID, you used 16 bytes already. So for DATA_MARKER and DATA_VALUE you can chose int, that will not exceed the packet size.

huangapple
  • 本文由 发表于 2020年8月3日 15:26:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63225348.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定