java - 如何在用户手机本地生成和存储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 每次安装都会改变对我来说并不重要,我只想生成它并在信号中进行广告。
解决方案
在蓝牙 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,它不会超过数据包的大小。
推荐阅读
- node.js - 安装 reactJS 时遇到问题
- php - 全屏面板引导与高图表(100% 高度)
- java - 如何使用 Firestore 对象填充数组
- python - 将 .sql 数据库转储转储为 pandas 数据框
- javascript - 如何使用 React 为 SVG 组件中的新样式设置动画?
- python - Python - 用 sudo date 替换缺失的日期
- c# - 跨多个数据网格视图的函数处理,重复代码最少
- c - 用指针/数组差异赋值
- c - 使用 `setp` 代替 `setb` 有什么好处吗?
- javascript - Javascript 中的 window.setInterval 不会多次运行一个函数