0

I have the following code I'm using to connect to a TruConnect Bobcat bluetooth module (https://ack.me/FAQs/What_are_the_UUID_s_used_by_TruConnect):

public async void Start()
    {           
        Guid RX_UUID = Guid.Parse("1cce1ea8-bd34-4813-a00a-c76e028fadcb");
        Guid TX_UUID = Guid.Parse("cacc07ff-ffff-4c48-8fae-a9ef71b75e26");
        Guid ServiceGuid = Guid.Parse("175f8f23-a570-49bd-9627-815a6a27de2a");

        var devices = await DeviceInformation.FindAllAsync(GattDeviceService.GetDeviceSelectorFromUuid(ServiceGuid));
        var service = await GattDeviceService.FromIdAsync(devices[0].Id);

        var TXCharacteristic = service.GetCharacteristics(TX_UUID)[0];
        GattReadResult result = await TXCharacteristic.ReadValueAsync(BluetoothCacheMode.Uncached);
        byte[] buffer = (result.Value.ToArray());
    }

The problem is that the buffer at the end always ends up with 20 zero bytes, even though my module is not sending anything. All this despite result.Status turns out to be Success.


This is what I've boiled it down to after trying to make it run in a bigger app and getting the same result.


Another interesting thing I've noticed is that I've tried the same approach on a WindowsHubApplication and it worked. Now, in a Universal App, it doesn't.


I've also tried both Cached and Uncached modes.

Thanks in advance

  • try listening to the OnCharacteristicChanged and OnCharacteristicRead, this works for me, I get varying buffer sizes up to 20 bytes. – pogorman Oct 21 '15 at 15:40

1 Answers1

3

Problem solved! I've initially tried subscribing to the Characteristic.ValueChanged event but the event handler never got called. Apparently it only works if you subscribe to the event AFTER you send the GattClientCharacteristicConfigurationDescriptorValue. (and I have absolutely no idea why, maybe someone can shed some light on this, but for now it works)

public async void Start()
    {           
        Guid RX_UUID = Guid.Parse("1cce1ea8-bd34-4813-a00a-c76e028fadcb");
        Guid TX_UUID = Guid.Parse("cacc07ff-ffff-4c48-8fae-a9ef71b75e26");
        Guid ServiceGuid = Guid.Parse("175f8f23-a570-49bd-9627-815a6a27de2a");

        var devices = await DeviceInformation.FindAllAsync(GattDeviceService.GetDeviceSelectorFromUuid(ServiceGuid));
        var service = await GattDeviceService.FromIdAsync(devices[0].Id);

        var TXCharacteristic = service.GetCharacteristics(TX_UUID)[0];
        var RXCharacteristic = service.GetCharacteristics(RX_UUID)[0];

        await TXCharacteristic.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);     
        TXCharacteristic.ValueChanged += TXCharacteristic_ValueChanged; ;       
    }

    private void TXCharacteristic_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args)
    {
        byte[] buffer = args.CharacteristicValue.ToArray();
    }

Now I get very nice 20 byte buffers filled with actual valid data.

  • Mark this as the answer, it just saved me some work figuring it out as well (C++ side but still) :-D – scape Jan 22 '19 at 20:43