Custom UUID for multiple Bluetooth connection in Android

后端 未结 2 652
醉梦人生
醉梦人生 2021-01-26 02:34

I have an android device acting as server which connects to multiple bluetooth android clients. I understand the concept of UUID and how it is unique.

My question is, c

相关标签:
2条回答
  • 2021-01-26 03:11

    You can generate one like this. First, is needed to generate as long the 64 least and most significant bits:

    private static long get64LeastSignificantBitsForVersion1() {
        Random random = new Random();
        long random63BitLong = random.nextLong() & 0x3FFFFFFFFFFFFFFFL;
        long variant3BitFlag = 0x8000000000000000L;
        return random63BitLong + variant3BitFlag;
    }
     
    private static long get64MostSignificantBitsForVersion1() {
        LocalDateTime start = LocalDateTime.of(1582, 10, 15, 0, 0, 0);
        Duration duration = Duration.between(start, LocalDateTime.now());
        long seconds = duration.getSeconds();
        long nanos = duration.getNano();
        long timeForUuidIn100Nanos = seconds * 10000000 + nanos * 100;
        long least12SignificatBitOfTime = (timeForUuidIn100Nanos & 0x000000000000FFFFL) >> 4;
        long version = 1 << 12;
        return 
          (timeForUuidIn100Nanos & 0xFFFFFFFFFFFF0000L) + version + least12SignificatBitOfTime;
    }
    

    next you can use the above methods to build one UUID:

    public static UUID generateType1UUID() {
     
        long most64SigBits = get64MostSignificantBitsForVersion1();
        long least64SigBits = get64LeastSignificantBitsForVersion1();
     
        return new UUID(most64SigBits, least64SigBits);
    }
    
    0 讨论(0)
  • 2021-01-26 03:20

    Answer:

    I finally found out that you can use a custom UUID using a generator and it works with multiple devices. The UUID must be unique and should not collide with the ones that are common & public. Hopefully someone finds it useful

    0 讨论(0)
提交回复
热议问题