【发布时间】: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