Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
BTUtils.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2020 Gothel Software e.K.
4 * Copyright (c) 2020 ZAFENA AB
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25package org.direct_bt;
26
27import java.nio.charset.StandardCharsets;
28
29public class BTUtils {
30 /**
31 * Base UUID128 used to express a UUID16, etc.
32 */
33 public static final String UUID128_BASE = "00000000-0000-1000-8000-00805f9b34fb";
34
35 /**
36 * Converts the given 4 digits uuid16 value to UUID128 representation.
37 * @param uuid16 the 4 characters long uuid16 value to convert
38 * @return UUID128 representation if uuid16 is 4 characters long, otherwise {@link #UUID128_BASE}.
39 */
40 public static final String toUUID128(final String uuid16) {
41 if( 4 == uuid16.length() ) {
42 return "0000" + uuid16 + UUID128_BASE.substring(8);
43 } else {
44 return UUID128_BASE;
45 }
46 }
47
48 /**
49 * Defining the supervising timeout for LE connections to be a multiple of the maximum connection interval as follows:
50 * <pre>
51 * ( 1 + conn_latency ) * conn_interval_max_ms * max(2, multiplier) [ms]
52 * </pre>
53 * If above result is smaller than the given min_result_ms, min_result_ms/10 will be returned.
54 * @param conn_latency the connection latency
55 * @param conn_interval_max_ms the maximum connection interval in [ms]
56 * @param min_result_ms the minimum resulting supervisor timeout, defaults to 500ms.
57 * If above formula results in a smaller value, min_result_ms/10 will be returned.
58 * @param multiplier recommendation is 6, we use 10 as default for safety.
59 * @return the resulting supervising timeout in 1/10 [ms], suitable for the {@link BTDevice#connectLE(short, short, short, short, short, short)}.
60 * @see BTDevice#connectLE(short, short, short, short, short, short)
61 */
62 public static short getHCIConnSupervisorTimeout(final int conn_latency, final int conn_interval_max_ms,
63 final int min_result_ms, final int multiplier) {
64 return (short) ( Math.max(min_result_ms,
65 ( 1 + conn_latency ) * conn_interval_max_ms * Math.max(2, multiplier)
66 ) / 10 );
67 }
68 /**
69 * Defining the supervising timeout for LE connections to be a multiple of the maximum connection interval as follows:
70 * <pre>
71 * ( 1 + conn_latency ) * conn_interval_max_ms * max(2, multiplier) [ms]
72 * </pre>
73 *
74 * If above result is smaller than the given min_result_ms, min_result_ms/10 will be returned.
75 *
76 * - Using minimum resulting supervisor timeout of 500ms.
77 * - Using multiplier of 10 as default for safety.
78 *
79 * @param conn_latency the connection latency
80 * @param conn_interval_max_ms the maximum connection interval in [ms]
81 * @return the resulting supervising timeout in 1/10 [ms], suitable for the {@link BTDevice#connectLE(short, short, short, short, short, short)}.
82 * @see BTDevice#connectLE(short, short, short, short, short, short)
83 */
84 public static short getHCIConnSupervisorTimeout(final int conn_latency, final int conn_interval_max_ms) {
85 return getHCIConnSupervisorTimeout(conn_latency, conn_interval_max_ms, 500 /* min_result_ms */, 10);
86 }
87
88 /**
89 * Decodes the given consecutive UTF-8 characters within buffer to String.
90 */
91 public static String decodeUTF8String(final byte[] buffer, final int offset, final int size) {
92 return new String(buffer, offset, size, StandardCharsets.UTF_8);
93 }
94}
static final String UUID128_BASE
Base UUID128 used to express a UUID16, etc.
Definition: BTUtils.java:33
static final String toUUID128(final String uuid16)
Converts the given 4 digits uuid16 value to UUID128 representation.
Definition: BTUtils.java:40
static String decodeUTF8String(final byte[] buffer, final int offset, final int size)
Decodes the given consecutive UTF-8 characters within buffer to String.
Definition: BTUtils.java:91
static short getHCIConnSupervisorTimeout(final int conn_latency, final int conn_interval_max_ms)
Defining the supervising timeout for LE connections to be a multiple of the maximum connection interv...
Definition: BTUtils.java:84
static short getHCIConnSupervisorTimeout(final int conn_latency, final int conn_interval_max_ms, final int min_result_ms, final int multiplier)
Defining the supervising timeout for LE connections to be a multiple of the maximum connection interv...
Definition: BTUtils.java:62