Peripheral does not have a characteristic

With the sketches below, "Peripheral does not have a characteristic" prints from the Central sketch. The characteristic is the same in both sketches so I'm unsure as to why it's not working. Any help would be greatly appreciated. Thank you.

#include <ArduinoBLE.h>
#include <Wire.h>
#include "SparkFun_Qwiic_Joystick_Arduino_Library.h" //Click here to get the library: http://librarymanager/All#SparkFun_joystick

JOYSTICK joystick; //Create instance of this object

int x, y;
int output[2];

void setup() {
  Serial.begin(9600);
  while (!Serial);
  
  BLE.begin();

  BLE.scanForUuid("19B10000-E8F2-537E-4F6C-D104768A1214");

  pinMode(10, OUTPUT);

  if(joystick.begin() == false) {
    Serial.println("Joystick does not appear to be connected. Please check wiring. Freezing...");
    while(1);
  }
}

void loop() {
  BLEDevice peripheral = BLE.available();

  if (peripheral) {
    Serial.print("Found ");
    Serial.print(peripheral.address());
    Serial.print(" '");
    Serial.print(peripheral.localName());
    Serial.print("' ");
    Serial.print(peripheral.advertisedServiceUuid());
    Serial.println();

    if (peripheral.localName() != "Alvik") {
      return;
    }

    BLE.stopScan();

    BLECharacteristic directionCharacteristic = peripheral.characteristic("19B10001-E8F2-537E-4F6C-D104768A1214");

    //Serial.println("Characteristic: " + peripheral.characteristic("19B10001-E8F2-537E-4F6C-D104768A1214"));

    if (!directionCharacteristic) {
      Serial.println("Peripheral does not have characteristic");
      peripheral.disconnect();
      return;
    } else if (!directionCharacteristic.canWrite()) {
      Serial.println("Peripheral does not have a writable characteristic!");
      peripheral.disconnect();
      return;
    }

    while (peripheral.connected()) {
      digitalWrite(10, HIGH);
      readJoyStick();

      output[0] = (x / 4) - 15;
      output[1] = (y / 4) - 15;

      directionCharacteristic.writeValue(output, 2);
    }

    BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");
  }
}

void readJoyStick() {
  x = joystick.getHorizontal();
  y = joystick.getVertical();
}
#include <ArduinoBLE.h>

BLEService joystick("19B10000-E8F2-537E-4F6C-D104768A1214");

BLEByteCharacteristic characteristic("19b10001-e8f2-537e-4f6c-d104768a1214", BLERead | BLEWrite);

void setup() {
  Serial.begin(9600);
  while (!Serial);

  if (!BLE.begin()) {
    Serial.println("starting Bluetooth® Low Energy module failed!");

    while (1);
  }

  BLE.setLocalName("Alvik");
  BLE.setAdvertisedService(joystick);

  joystick.addCharacteristic(characteristic);

  BLE.addService(joystick);

  characteristic.writeValue(0);

  BLE.advertise();

  Serial.println("Alvik Peripheral");
}

void loop() {
  BLEDevice central = BLE.central();

  if (central) {
    Serial.print("Connected to central: ");
    
    Serial.println(central.address());

    while (central.connected()) {
      if (characteristic.written()) {
        Serial.println(characteristic.value());
      }
    }

    Serial.print(F("Disconnected from central: "));
    Serial.println(central.address());
  }
}
directionCharacteristic

The value of this is zero, causing the output. Find the source of this.

Look more carefully at the ArduinoBLE.h library Central example sketch "LedControl". With the local name of the peripheral changed, it indeed finds your characteristic.

You have left out needed chunks of the routine related to attribute discovery.

  // discover peripheral attributes
  Serial.println("Discovering attributes ...");
  if (peripheral.discoverAttributes()) {
    Serial.println("Attributes discovered");
  } else {
    Serial.println("Attribute discovery failed!");
    peripheral.disconnect();
    return;
  }

I tried adding switchCharacteristic.writeValue(1); in void setup of the peripheral sketch but that didn't seem to work. The central Arduino does detect the peripheral but for some reason the characteristic is blank. Why would that be?

Where in the central sketch do you think you are reading the value?

Look closely at the library example "Peripheral Explorer" for where it reads the characteristic value.