I'm trying to create an app that will comunicate from a winforms app to arduino. here's my arduino sketch
#include "thingProperties.h"
#include "ArduinoBLE.h"
const char* deviceServiceUuid = "19b10000-e8f2-537e-4f6c-d104768a1214";
const char* dataCharacteristicUUID = "19b10001-e8f2-537e-4f6c-d104768a1215";
const char* deviceServiceResponseCharacteristicUuid = "19b10001-e8f2-537e-4f6c-d104768a1216";
BLEService carService(deviceServiceUuid);
BLEStringCharacteristic carControlRequestCharacteristic(dataCharacteristicUUID, BLEWrite, 4);
BLEStringCharacteristic carControlResponseCharacteristic(deviceServiceResponseCharacteristicUuid, BLENotify, 4);
BLEByteCharacteristic carControlCharacteristic(deviceServiceResponseCharacteristicUuid, BLERead | BLEWrite); // 2A57 is "Digital Output"
void setup() {
Serial.begin(9600);
while (!Serial);
if (!BLE.begin()) {
Serial.println("failed to initialize BLE!");
while (1);
}
BLE.setLocalName("UnoR4 BLE Car");
BLE.setAdvertisedService(carService);
// add the characteristics to the service
carService.addCharacteristic(carControlRequestCharacteristic);
carService.addCharacteristic(carControlResponseCharacteristic);
carService.addCharacteristic(carControlCharacteristic);
// add the service
BLE.addService(carService);
carControlCharacteristic.writeValue(0);
// start advertising
BLE.advertise();
Serial.println("Bluetooth® device active, waiting for connections...");
}
void loop() {
//ArduinoCloud.update();
BLEDevice controller = BLE.central();
// if a central is connected to peripheral:
}
}
and this is my C# code. OnAdvertisementReceived is never called
private void button3_Click(object sender, EventArgs e)
{
BluetoothLEAdvertisementWatcher bleWatcher;
bleWatcher = new BluetoothLEAdvertisementWatcher();
bleWatcher.Received += OnAdvertisementReceived;
bleWatcher.ScanningMode = BluetoothLEScanningMode.Passive;
bleWatcher.Start();
//bleWatcher.Received += BleWatcher_Received;
//bleWatcher.Stopped += BleWatcher_Stopped;
//bleWatcher.Start();
}
private void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
{
var manufacturerSections = args.Advertisement.ManufacturerData;
if (manufacturerSections.Count > 0)
{
// Only print the first one of the list
var manufacturerData = manufacturerSections[0];
var data = new byte[manufacturerData.Data.Length];
using (var reader = DataReader.FromBuffer(manufacturerData.Data))
{
reader.ReadBytes(data);
}
// Print the company ID + the raw data in hex format
lsb_device.Items.Add(string.Format("0x{0}: {1}",
manufacturerData.CompanyId.ToString("X"),
BitConverter.ToString(data)));
}
}