blob: 18bad9c3c259c90648b8e03477f08a660126fb05 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.bluetooth.le;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
import java.util.Arrays;
/**
* Wrapper for Transport Discovery Data Transport Blocks.
* This class represents a Transport Block from a Transport Discovery Data.
*
* @see TransportDiscoveryData
* @see AdvertiseData
*/
public final class TransportBlock implements Parcelable {
private static final String TAG = "TransportBlock";
private final int mOrgId;
private final int mTdsFlags;
private final int mTransportDataLength;
private final byte[] mTransportData;
/**
* Creates an instance of TransportBlock from raw data.
*
* @param orgId the Organization ID
* @param tdsFlags the TDS flags
* @param transportDataLength the total length of the Transport Data
* @param transportData the Transport Data
*/
public TransportBlock(int orgId, int tdsFlags, int transportDataLength,
@Nullable byte[] transportData) {
mOrgId = orgId;
mTdsFlags = tdsFlags;
mTransportDataLength = transportDataLength;
mTransportData = transportData;
}
private TransportBlock(Parcel in) {
mOrgId = in.readInt();
mTdsFlags = in.readInt();
mTransportDataLength = in.readInt();
if (mTransportDataLength > 0) {
mTransportData = new byte[mTransportDataLength];
in.readByteArray(mTransportData);
} else {
mTransportData = null;
}
}
@Override
public void writeToParcel(@NonNull Parcel dest, int flags) {
dest.writeInt(mOrgId);
dest.writeInt(mTdsFlags);
dest.writeInt(mTransportDataLength);
if (mTransportData != null) {
dest.writeByteArray(mTransportData);
}
}
/**
* @hide
*/
@Override
public int describeContents() {
return 0;
}
/**
* @hide
*/
@Override
public boolean equals(@Nullable Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
TransportBlock other = (TransportBlock) obj;
return Arrays.equals(toByteArray(), other.toByteArray());
}
public static final @NonNull Creator<TransportBlock> CREATOR = new Creator<TransportBlock>() {
@Override
public TransportBlock createFromParcel(Parcel in) {
return new TransportBlock(in);
}
@Override
public TransportBlock[] newArray(int size) {
return new TransportBlock[size];
}
};
/**
* Gets the Organization ID of the Transport Block which corresponds to one of the
* the Bluetooth SIG Assigned Numbers.
*/
public int getOrgId() {
return mOrgId;
}
/**
* Gets the TDS flags of the Transport Block which represents the role of the device and
* information about its state and supported features.
*/
public int getTdsFlags() {
return mTdsFlags;
}
/**
* Gets the total number of octets in the Transport Data field in this Transport Block.
*/
public int getTransportDataLength() {
return mTransportDataLength;
}
/**
* Gets the Transport Data of the Transport Block which contains organization-specific data.
*/
@Nullable
public byte[] getTransportData() {
return mTransportData;
}
/**
* Converts this TransportBlock to byte array
*
* @return byte array representation of this Transport Block or null if the conversion failed
*/
@Nullable
public byte[] toByteArray() {
try {
ByteBuffer buffer = ByteBuffer.allocate(totalBytes());
buffer.put((byte) mOrgId);
buffer.put((byte) mTdsFlags);
buffer.put((byte) mTransportDataLength);
if (mTransportData != null) {
buffer.put(mTransportData);
}
return buffer.array();
} catch (BufferOverflowException e) {
Log.e(TAG, "Error converting to byte array: " + e.toString());
return null;
}
}
/**
* @return total byte count of this TransportBlock
*/
public int totalBytes() {
// 3 uint8 + byte[] length
int size = 3 + mTransportDataLength;
return size;
}
}
|