zhuguifei
2025-12-30 61eee1173c00a7ba9d9c748d28fe3acdb33b9441
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
178
179
180
181
182
183
184
185
186
187
package com.blakequ.bluetooth_manager_lib.connect;
 
import com.blakequ.bluetooth_manager_lib.device.resolvers.GattAttributeResolver;
 
import java.util.UUID;
 
/**
 * Copyright (C) BlakeQu All Rights Reserved <blakequ@gmail.com>
 * <p/>
 * Licensed under the blakequ.com License, Version 1.0 (the "License");
 * 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.
 * <p/>
 * author  : quhao <blakequ@gmail.com> <br>
 * date     : 2016/8/18 17:48 <br>
 * last modify author : <br>
 * version : 1.0 <br>
 * description:
 */
public class BluetoothSubScribeData {
 
    private UUID characteristicUUID;
    private byte[] characteristicValue;
    private UUID descriptorUUID;
    private byte[] descriptorValue;
    //the notification uuid for Characteristic
    private Type operatorType;
 
    private BluetoothSubScribeData(UUID characteristicUUID, byte[] characteristicValue, UUID descriptorUUID
        ,byte[] descriptorValue, Type operatorType){
        this.characteristicUUID = characteristicUUID;
        this.characteristicValue = characteristicValue;
        this.descriptorUUID = descriptorUUID;
        this.descriptorValue = descriptorValue;
        this.operatorType = operatorType;
    }
 
    private BluetoothSubScribeData(UUID characteristicUUID, Type operatorType){
        this.characteristicUUID = characteristicUUID;
        this.operatorType = operatorType;
    }
 
 
    public UUID getCharacteristicUUID() {
        return characteristicUUID;
    }
 
    public byte[] getCharacteristicValue() {
        return characteristicValue;
    }
 
    public UUID getDescriptorUUID() {
        return descriptorUUID;
    }
 
    public byte[] getDescriptorValue() {
        return descriptorValue;
    }
 
    public Type getOperatorType() {
        return operatorType;
    }
 
    public static final class Builder {
        private UUID characteristicUUID;
        private byte[] characteristicValue;
        private UUID descriptorUUID;
        private byte[] descriptorValue;
        //the notification uuid for Characteristic
        private Type operatorType;
 
        /**
         * read Characteristic
         * @param characteristicUUID
         * @return
         */
        public Builder setCharacteristicRead(UUID characteristicUUID){
            this.operatorType = Type.CHAR_READ;
            this.characteristicUUID = characteristicUUID;
            return this;
        }
 
        /**
         * write Characteristic
         * @param characteristicUUID
         * @param characteristicValue
         * @return
         */
        public Builder setCharacteristicWrite(UUID characteristicUUID, byte[] characteristicValue){
            this.operatorType = Type.CHAR_WIRTE;
            this.characteristicUUID = characteristicUUID;
            this.characteristicValue = characteristicValue;
            return this;
        }
 
        /**
         * read Descriptor
         * @param characteristicUUID
         * @param descriptorUUID
         * @return
         */
        public Builder setDescriptorRead(UUID characteristicUUID, UUID descriptorUUID){
            this.operatorType = Type.DESC_READ;
            this.characteristicUUID = characteristicUUID;
            this.descriptorUUID = descriptorUUID;
            return this;
        }
 
        /**
         * write Descriptor
         * @param characteristicUUID
         * @param descriptorUUID
         * @param descriptorValue
         * @return
         */
        public Builder setDescriptorWrite(UUID characteristicUUID, UUID descriptorUUID, byte[] descriptorValue){
            this.operatorType = Type.DESC_WRITE;
            this.characteristicUUID = characteristicUUID;
            this.descriptorUUID = descriptorUUID;
            this.descriptorValue = descriptorValue;
            return this;
        }
 
        /**
         * get notify
         * @param characteristicNotificationUUID notify characteristic uuid
         * @return
         */
        public Builder setCharacteristicNotify(UUID characteristicNotificationUUID){
            this.operatorType = Type.NOTIFY;
            this.characteristicUUID = characteristicNotificationUUID;
            this.descriptorUUID = UUID.fromString(GattAttributeResolver.CLIENT_CHARACTERISTIC_CONFIG);
            return this;
        }
 
        public BluetoothSubScribeData build(){
            if (characteristicUUID == null){
                throw new IllegalArgumentException("invalid characteristic, and characteristic can not be null");
            }
            BluetoothSubScribeData data = null;
            switch (operatorType){
                case CHAR_READ:
                    data = new BluetoothSubScribeData(characteristicUUID, operatorType);
                    break;
                case CHAR_WIRTE:
                    if (characteristicValue == null){
                        throw new IllegalArgumentException("invalid null characteristic value");
                    }
                    data = new BluetoothSubScribeData(characteristicUUID, characteristicValue, null,null, operatorType);
                    break;
                case DESC_READ:
                    if (descriptorUUID == null){
                        throw new IllegalArgumentException("invalid null descriptor UUID");
                    }
                    data = new BluetoothSubScribeData(characteristicUUID, null, descriptorUUID, null, operatorType);
                    break;
                case DESC_WRITE:
                    if (descriptorUUID == null || descriptorValue == null){
                        throw new IllegalArgumentException("invalid null descriptor UUID or value");
                    }
                    data = new BluetoothSubScribeData(characteristicUUID, null, descriptorUUID, descriptorValue, operatorType);
                    break;
                case NOTIFY:
                    if (descriptorUUID == null){
                        throw new IllegalArgumentException("invalid null descriptor UUID");
                    }
                    data = new BluetoothSubScribeData(characteristicUUID, null, descriptorUUID, null, operatorType);
                    break;
            }
            return data;
        }
    }
 
    /**
     * bluetooth subscribe operator type
     */
    public static enum Type{
        CHAR_WIRTE,
        CHAR_READ,
        DESC_WRITE,
        DESC_READ,
        NOTIFY
    }
}