Skip to content

Commit

Permalink
Merge pull request #29 from jaspervp/readqueu
Browse files Browse the repository at this point in the history
Queue for reading characteristics, merged with queue for writing
  • Loading branch information
thoutbeckers committed Aug 10, 2015
2 parents 86c275d + e93f848 commit 41de6f2
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,22 @@
class LeGattCharacteristic43 implements LeGattCharacteristic {
final BluetoothGattCharacteristic characteristic;
final BluetoothGatt gatt;
final LeRemoteDevice43 leRemoteDevice43;

LeGattCharacteristic43(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic) {


LeGattCharacteristic43(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic, LeRemoteDevice43 leRemoteDevice43) {
this.characteristic = characteristic;
this.gatt = gatt;
this.leRemoteDevice43 = leRemoteDevice43;
}


@Override
public void read() {
gatt.readCharacteristic(characteristic);
if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_READ ) !=0) {
leRemoteDevice43.addToQueue(characteristic);
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public LeGattCharacteristic getCharacteristic(UUID uuid) {
BluetoothGattCharacteristic characteristic = getGattService().getCharacteristic(uuid);
if (characteristic == null || leRemoteDevice43.gatt == null)
return null;
return new LeGattCharacteristic43(leRemoteDevice43.gatt, characteristic);
return new LeGattCharacteristic43(leRemoteDevice43.gatt, characteristic,leRemoteDevice43);
}

@Override
Expand All @@ -56,7 +56,7 @@ public boolean enableCharacteristicNotification(UUID characteristic) {
}else {
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
}
leRemoteDevice43.writeGattDescriptor(descriptor);
leRemoteDevice43.addToQueue(descriptor);
return true;
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ public class LeRemoteDevice43 extends BluetoothGattCallback implements LeRemoteD
final Map<UUID, LeCharacteristicListener> uuidCharacteristicListeners = new HashMap<UUID, LeCharacteristicListener>(0);
final Map<UUID, LeCharacteristicWriteListener> uuidCharacteristicWriteListeners = new HashMap<UUID, LeCharacteristicWriteListener>(0);

final ConcurrentLinkedQueue<BluetoothGattDescriptor> descriptorWriteQueue = new ConcurrentLinkedQueue<BluetoothGattDescriptor>();

final ConcurrentLinkedQueue<Object> queue = new ConcurrentLinkedQueue<>();

public LeRemoteDevice43(LeDevice43 leDevice43, BluetoothDevice device) {
this.leDevice43 = leDevice43;
Expand Down Expand Up @@ -195,35 +194,61 @@ public void onServicesDiscovered(BluetoothGatt gatt, int status) {

}

public void writeGattDescriptor(BluetoothGattDescriptor d){
//put the descriptor into the write queue
descriptorWriteQueue.add(d);
//if there is only 1 item in the queue, then write it. If more than 1, we handle asynchronously in the callback above
if(descriptorWriteQueue.size() == 1){
gatt.writeDescriptor(d);
public void addToQueue(Object object){
if (object instanceof BluetoothGattCharacteristic || object instanceof BluetoothGattDescriptor) {
synchronized (queue) {
queue.add(object);
if (queue.size() == 1) {
sendFirst();
}
}
}
}



public void sendFirst(){
synchronized (queue) {

if (queue.size() > 0) {
Object object = queue.element();
if (object instanceof BluetoothGattCharacteristic) {
gatt.readCharacteristic((BluetoothGattCharacteristic) object);
} else if (object instanceof BluetoothGattDescriptor) {
gatt.writeDescriptor((BluetoothGattDescriptor) object);

}


}
}
}


@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
try {
this.characteristicNotificationChanged(gatt,descriptor.getCharacteristic(),(status==gatt.GATT_SUCCESS));

descriptorWriteQueue.remove(); //pop the item that we just finishing writing
//if there is more to write, do it!
if (descriptorWriteQueue.size() > 0)
gatt.writeDescriptor(descriptorWriteQueue.element());
queue.remove();
sendFirst();

} catch (Throwable t) {
Log.w("LeBlue", "error during onDescriptorWrite callback", t);
}
}



@Override
public void onCharacteristicRead (BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status){
queue.remove();
sendFirst();

if(status==gatt.GATT_SUCCESS)
this.characteristicUpdated(gatt,characteristic);


}

@Override
Expand All @@ -245,7 +270,7 @@ public void characteristicNotificationChanged(BluetoothGatt gatt, BluetoothGattC
LeCharacteristicListener uuidListener = uuidCharacteristicListeners.get(uuid);

if ((nullListener != null || uuidListener != null) && gatt != null) {
LeGattCharacteristic43 characteristic43 = new LeGattCharacteristic43(gatt, characteristic);
LeGattCharacteristic43 characteristic43 = new LeGattCharacteristic43(gatt, characteristic,this);
if (nullListener != null)
nullListener.leCharacteristicNotificationChanged(uuid, this, characteristic43,success);
if (uuidListener != null)
Expand All @@ -269,7 +294,7 @@ public void characteristicUpdated(BluetoothGatt gatt, BluetoothGattCharacteristi
LeCharacteristicListener uuidListener = uuidCharacteristicListeners.get(uuid);

if ((nullListener != null || uuidListener != null) && gatt != null) {
LeGattCharacteristic43 characteristic43 = new LeGattCharacteristic43(gatt, characteristic);
LeGattCharacteristic43 characteristic43 = new LeGattCharacteristic43(gatt, characteristic,this);
if (nullListener != null)
nullListener.leCharacteristicChanged(uuid, this, characteristic43);
if (uuidListener != null)
Expand All @@ -288,19 +313,19 @@ public void onCharacteristicWrite(android.bluetooth.BluetoothGatt gatt, android.

UUID uuid = characteristic.getUuid();

LeCharacteristicWriteListener nullListener = uuidCharacteristicWriteListeners.get(null);
LeCharacteristicWriteListener nullListener = uuidCharacteristicWriteListeners.get(null);
LeCharacteristicWriteListener uuidListener = uuidCharacteristicWriteListeners.get(uuid);

if ((nullListener != null || uuidListener != null) && gatt != null) {
LeGattCharacteristic43 characteristic43 = new LeGattCharacteristic43(gatt, characteristic);
if (nullListener != null)
nullListener.leCharacteristicWritten(uuid, this, characteristic43,succes);
if (uuidListener != null)
uuidListener.leCharacteristicWritten(uuid, this, characteristic43,succes);
if ((nullListener != null || uuidListener != null) && gatt != null) {
LeGattCharacteristic43 characteristic43 = new LeGattCharacteristic43(gatt, characteristic,this);
if (nullListener != null)
nullListener.leCharacteristicWritten(uuid, this, characteristic43,succes);
if (uuidListener != null)
uuidListener.leCharacteristicWritten(uuid, this, characteristic43,succes);
}
} catch (Throwable t) {
Log.w("LeBlue", "error during onCharacteristicChanged callback", t);
}
} catch (Throwable t) {
Log.w("LeBlue", "error during onCharacteristicChanged callback", t);
}

}

Expand All @@ -320,10 +345,10 @@ public void readRssi() {
@Override
public void onReadRemoteRssi(android.bluetooth.BluetoothGatt gatt, int rssi, int status)
{
if (status == BluetoothGatt.GATT_SUCCESS) {
for (LeRemoteDeviceListener listener : listeners)
listener.rssiRead(leDevice43, this, rssi);
}
if (status == BluetoothGatt.GATT_SUCCESS) {
for (LeRemoteDeviceListener listener : listeners)
listener.rssiRead(leDevice43, this, rssi);
}
}


Expand Down

0 comments on commit 41de6f2

Please sign in to comment.