Java 如何在用户';谁的电话?
我想为用户生成UUID,并将其存储在本地,以便在传输蓝牙低能量信号时使用 以下是我尝试过的: 用于为用户生成UUID的类:Java 如何在用户';谁的电话?,java,android,android-studio,bluetooth-lowenergy,transmission,Java,Android,Android Studio,Bluetooth Lowenergy,Transmission,我想为用户生成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(
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 ();
}
但我一直在犯错误:
运行时中设置的未知位\u标志: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电源/服务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个字节。因此,对于数据标记和数据值,您可以选择int,它不会超过数据包大小。您在Emulator上运行它吗?不,我正在我的手机上运行它
2020-08-03 15:38:22.817 26082-26082/com.example.beacondance E/.beacondistance:Invalid ID 0x00000001。2020-08-03 15:38:22.820 26082-26082/com.example.beacondance E/AndroidRuntime:致命异常:主进程:com.example.beacondance,PID:26082 android.content.res.Resources$NotFoundException:字符串资源ID#0x1
需要有关错误的更多详细信息。你能分享更多细节吗?