<commit_before>var alertLevelCharacteristic;

function onReadButtonClick() {
  log('Requesting Bluetooth Device...');
  navigator.bluetooth.requestDevice({filters: [{services: ['link_loss']}]})
  .then(device => {
    log('Connecting to GATT Server...');
    return device.gatt.connect();
  })
  .then(server => {
    log('Getting Link Loss Service...');
    return server.getPrimaryService('link_loss');
  })
  .then(service => {
    log('Getting Alert Level Characteristic...');
    return service.getCharacteristic('alert_level');
  })
  .then(characteristic => {
    alertLevelCharacteristic = characteristic;
    document.querySelector('#writeButton').disabled = false;
    log('Reading Alert Level...');
    return characteristic.readValue();
  })
  .then(value => {
    log('> Alert Level: ' + getAlertLevel(value));
  })
  .catch(error => {
    document.querySelector('#writeButton').disabled = true;
    log('Argh! ' + error);
  });
}

function onWriteButtonClick() {
  if (!alertLevelCharacteristic) {
    return;
  }
  log('Setting Alert Level...');
  let value = document.querySelector('#alertLevelValue').value;
  alertLevelCharacteristic.writeValue(new Uint8Array([value]))
  .then(_ => {
    log('> Alert Level changed to: ' +
        getAlertLevel(alertLevelCharacteristic.value));
  })
  .catch(error => {
    log('Argh! ' + error);
  });
}

/* Utils */

const valueToAlertLevel = {
  0x00: 'No Alert',
  0x01: 'Mild Alert',
  0x02: 'High Alert'
};

function getAlertLevel(value) {
  let v = value.getUint8(0);
  return v + (v in valueToAlertLevel ?
      ' (' + valueToAlertLevel[v] + ')' : 'Unknown');
}
<commit_msg>Writing to a characteristic does not neccesarily mean that subsiquent reads will return that value (#410)<commit_after>var alertLevelCharacteristic;

function onReadButtonClick() {
  log('Requesting Bluetooth Device...');
  navigator.bluetooth.requestDevice({filters: [{services: ['link_loss']}]})
  .then(device => {
    log('Connecting to GATT Server...');
    return device.gatt.connect();
  })
  .then(server => {
    log('Getting Link Loss Service...');
    return server.getPrimaryService('link_loss');
  })
  .then(service => {
    log('Getting Alert Level Characteristic...');
    return service.getCharacteristic('alert_level');
  })
  .then(characteristic => {
    alertLevelCharacteristic = characteristic;
    document.querySelector('#writeButton').disabled = false;
    log('Reading Alert Level...');
    return characteristic.readValue();
  })
  .then(value => {
    log('> Alert Level: ' + getAlertLevel(value));
  })
  .catch(error => {
    document.querySelector('#writeButton').disabled = true;
    log('Argh! ' + error);
  });
}

function onWriteButtonClick() {
  if (!alertLevelCharacteristic) {
    return;
  }
  log('Setting Alert Level...');
  let value = document.querySelector('#alertLevelValue').value;
  alertLevelCharacteristic.writeValue(new Uint8Array([value]))
  .then(_ => {
    log('> Alert Level changed to: ' + getAlertLevel(value));
  })
  .catch(error => {
    log('Argh! ' + error);
  });
}

/* Utils */

const valueToAlertLevel = {
  0x00: 'No Alert',
  0x01: 'Mild Alert',
  0x02: 'High Alert'
};

function getAlertLevel(value) {
  let v = value.getUint8(0);
  return v + (v in valueToAlertLevel ?
      ' (' + valueToAlertLevel[v] + ')' : 'Unknown');
}

{'fraction_non_alphanumeric': 0.16761790327425655, 'fraction_numerical': 0.008110543706818865, 'mean_word_length': 3.8613138686131387, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5495178', 'n_tokens_mistral': 1062, 'n_tokens_neox': 1032, 'n_words': 211}