0

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.

Ravish Jha
  • 481
  • 3
  • 25

1 Answers1

1

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.

Eishon
  • 1,274
  • 1
  • 9
  • 17
  • `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` – Ravish Jha Aug 03 '20 at 10:09
  • Need more details about the error. Can you share it in more details? – Eishon Aug 03 '20 at 17:29