34
34
import android .bluetooth .BluetoothGattService ;
35
35
import android .bluetooth .BluetoothProfile ;
36
36
import android .content .Context ;
37
+ import android .util .Log ;
37
38
38
39
import java .util .List ;
39
40
@@ -51,6 +52,8 @@ public class BluetoothLEService {
51
52
private BaseListener mBluetoothListener ;
52
53
private BluetoothGatt mBluetoothGatt ;
53
54
private BluetoothGattCharacteristic mWriteCharacteristic , mNotifyCharacteristic ;
55
+ private String writeCharacteristicUUID ;
56
+ private String readCharacteristicUUID ;
54
57
55
58
private int mState ;
56
59
@@ -89,8 +92,8 @@ public int getState() {
89
92
90
93
/**
91
94
* Connect to a GATT server.
92
- * @param context
93
- * @param device
95
+ * @param context the context
96
+ * @param device the device
94
97
*/
95
98
public void connect (Context context , BluetoothDevice device ){
96
99
setState (State .STATE_CONNECTING );
@@ -128,7 +131,7 @@ public void close(){
128
131
129
132
/**
130
133
* Write data to remote device.
131
- * @param data
134
+ * @param data data to send to the device
132
135
*/
133
136
public void write (byte [] data ){
134
137
if (mBluetoothGatt != null ){
@@ -137,6 +140,14 @@ public void write(byte[] data){
137
140
}
138
141
}
139
142
143
+ public void setWriteCharacteristic (String characteristicUUID ) {
144
+ writeCharacteristicUUID = characteristicUUID ;
145
+ }
146
+
147
+ public void setReadCharacteristic (String characteristicUUID ) {
148
+ readCharacteristicUUID = characteristicUUID ;
149
+ }
150
+
140
151
private BluetoothGattCallback mBTGattCallback = new BluetoothGattCallback () {
141
152
@ Override
142
153
public void onConnectionStateChange (BluetoothGatt gatt , int status , int newState ) {
@@ -160,26 +171,55 @@ public void onServicesDiscovered(BluetoothGatt gatt, int status) {
160
171
super .onServicesDiscovered (gatt , status );
161
172
if (status == BluetoothGatt .GATT_SUCCESS ) {
162
173
List <BluetoothGattService > services = gatt .getServices ();
174
+ if (mBluetoothListener != null ){
175
+ ((BluetoothLEListener )mBluetoothListener ).onDiscoveringServices (services );
176
+ }
163
177
for (BluetoothGattService service : services ) {
164
178
List <BluetoothGattCharacteristic > characteristics = service .getCharacteristics ();
165
179
for (BluetoothGattCharacteristic characteristic : characteristics ) {
166
180
final int charaProp = characteristic .getProperties ();
181
+ final String charaUUID = characteristic .getUuid ().toString ();
182
+
167
183
if ((charaProp | BluetoothGattCharacteristic .PERMISSION_READ ) > 0 ){
168
- if (mNotifyCharacteristic != null ){
169
- mBluetoothGatt .setCharacteristicNotification (mNotifyCharacteristic , false );
170
- mNotifyCharacteristic = null ;
184
+ if (readCharacteristicUUID .isEmpty ()){
185
+ if (mNotifyCharacteristic != null ){
186
+ mBluetoothGatt .setCharacteristicNotification (mNotifyCharacteristic , false );
187
+ mNotifyCharacteristic = null ;
188
+ }
189
+ gatt .readCharacteristic (characteristic );
171
190
}
172
- gatt . readCharacteristic ( characteristic );
191
+ Log . d ( "LMBluetoothSdk" , "Assigning read characteristic : " + characteristic . getUuid () );
173
192
}
174
- if ((charaProp | BluetoothGattCharacteristic .PROPERTY_NOTIFY ) > 0 ){
175
- mNotifyCharacteristic = characteristic ;
176
- mBluetoothGatt .setCharacteristicNotification (characteristic , true );
193
+
194
+ if ((charaProp | BluetoothGattCharacteristic .PROPERTY_NOTIFY ) > 0 ) {
195
+ if (readCharacteristicUUID .isEmpty ()){
196
+ mNotifyCharacteristic = characteristic ;
197
+ mBluetoothGatt .setCharacteristicNotification (characteristic , true );
198
+ }else if (charaUUID .equalsIgnoreCase (readCharacteristicUUID )){
199
+ mNotifyCharacteristic = characteristic ;
200
+ if ( mBluetoothGatt .setCharacteristicNotification (characteristic , true ) ) {
201
+ Log .d ("LMBluetoothSdk" , "Subscribing to characteristic : " + characteristic .getUuid ());
202
+ }
203
+ }
177
204
}
178
- if (((charaProp & BluetoothGattCharacteristic .PERMISSION_WRITE )
179
- | (charaProp & BluetoothGattCharacteristic .PROPERTY_WRITE_NO_RESPONSE )) > 0 ){
180
- mWriteCharacteristic = characteristic ;
205
+
206
+ if (writeCharacteristicUUID .isEmpty ()){
207
+ if (((charaProp & BluetoothGattCharacteristic .PERMISSION_WRITE )
208
+ | (charaProp & BluetoothGattCharacteristic .PROPERTY_WRITE_NO_RESPONSE )) > 0 ){
209
+ mWriteCharacteristic = characteristic ;
210
+ }
211
+ }else {
212
+ if (((charaProp & BluetoothGattCharacteristic .PROPERTY_WRITE )
213
+ | (charaProp & BluetoothGattCharacteristic .PROPERTY_WRITE_NO_RESPONSE )) > 0
214
+ & charaUUID .equalsIgnoreCase (writeCharacteristicUUID )) {
215
+ Log .d ("LMBluetoothSdk" , "Assigning write characteristic : " + characteristic .getUuid ());
216
+ mWriteCharacteristic = characteristic ;
217
+ }
181
218
}
182
219
}
220
+ if (mBluetoothListener != null ){
221
+ ((BluetoothLEListener )mBluetoothListener ).onDiscoveringCharacteristics (characteristics );
222
+ }
183
223
}
184
224
setState (State .STATE_GOT_CHARACTERISTICS );
185
225
}
0 commit comments