Skip to content

Commit e0febe6

Browse files
author
Douglas Nassif Roma Junior
committed
Changed LE behavior to change the status to connected only when it finds the characteristic
1 parent a1c8eba commit e0febe6

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

BluetoothLowEnergyLibrary/src/main/java/com/github/douglasjunior/bluetoothlowenergylibrary/BluetoothLeService.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ else if (mStatus == BluetoothStatus.CONNECTED)
105105
} else {
106106
if (newState == BluetoothProfile.STATE_CONNECTED) {
107107
//createBound(gatt.getDevice());
108-
updateDeviceName(gatt.getDevice());
109-
updateState(BluetoothStatus.CONNECTED);
110108
gatt.discoverServices();
111109
} else if (newState == BluetoothProfile.STATE_CONNECTING) {
112110
updateState(BluetoothStatus.CONNECTING);
@@ -138,26 +136,36 @@ public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
138136
super.onReliableWriteCompleted(gatt, status);
139137
}
140138

139+
@RequiresPermission(Manifest.permission.BLUETOOTH)
141140
@Override
142141
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
143142
super.onServicesDiscovered(gatt, status);
144143
Log.v(TAG, "onServicesDiscovered: " + status);
144+
145145
if (BluetoothGatt.GATT_SUCCESS == status) {
146146
for (BluetoothGattService service : gatt.getServices()) {
147147
Log.v(TAG, "Service: " + service.getUuid());
148-
if (mConfig.uuidService.equals(service.getUuid())) {
148+
if (service.getUuid().equals(mConfig.uuidService)) {
149149
for (BluetoothGattCharacteristic characteristic : service.getCharacteristics()) {
150-
Log.v(TAG, "Characteristic: " + characteristic.getUuid());
151-
if (mConfig.uuidCharacteristic.equals(characteristic.getUuid())) {
150+
Log.v(TAG, "Characteristic: " + characteristic.getUuid() + " write: " + (characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE));
151+
if (characteristic.getUuid().equals(mConfig.uuidCharacteristic)) {
152152
characteristicRxTx = characteristic;
153153
gatt.setCharacteristicNotification(characteristic, true);
154+
155+
updateDeviceName(gatt.getDevice());
156+
updateState(BluetoothStatus.CONNECTED);
157+
return;
154158
}
155159
}
156160
}
157161
}
162+
Log.e(TAG, "Could not find uuidService:" + mConfig.uuidService + " and uuidCharacteristic:" + mConfig.uuidCharacteristic);
158163
} else {
159164
Log.e(TAG, "onServicesDiscovered error " + status);
160165
}
166+
167+
// If arrived here, no service or characteristic has been found.
168+
gatt.disconnect();
161169
}
162170
};
163171

0 commit comments

Comments
 (0)