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());
}
}