Search code examples
c#unity-game-enginebluetooth-lowenergygattbluetooth-gatt

BLE Cycle Trainer Write Characteristic UUID 2AD9 - How to adjust resistance based on what's happening in game?


Hello overflow friends,

I'd be eternally grateful if one of the resident BLE geniuses on this forum wouldn't mind helping me. I've been trying to build a little cycling game in c# and so far I've managed to read the byte array from the bike and convert power and rpm successfully and have it do stuff in the game.

However, I'm having a little trouble with the write characteristic. I have a nice piece of code from a git repo that I used to successfully write numbers to my bike but from my research, it seems to want to take an absolute value of 0-1000:

 BleApi.SubscribeCharacteristic_Write(selectedDeviceId, selectedServiceId, write_characteristic = "{00002ad9-0000-1000-8000-00805f9b34fb}", false);
        Write("00");
        byte resistance1 = Convert.ToByte(val % 256);
        byte resistance2 = Convert.ToByte(val / 256);
        byte[] payload = { 0x11, 0x00, 0x00, resistance1, resistance2, 0x00, 0x00 };
        BleApi.BLEData data = new BleApi.BLEData();
        data.buf = new byte[512];
        data.deviceId = selectedDeviceId;
        data.serviceUuid = selectedServiceId;
        data.characteristicUuid = write_characteristic;
        for (int i = 0; i < payload.Length; i++)
        {
            data.buf[i] = payload[i];
        }
        data.size = (short)payload.Length;
        BleApi.SendData(in data, false);


So the question is how can we increase resistance relatively based on whats happening in the game? For example lets say the user is in a high gear already at 0% in the game and then gets to a slope of 7%, the resistance would need to increase relative to the resistance the bike is currently on (e.g. +100), it's no use of sending a resistance of e.g. =700 as this might even be below where their gearing is.

This must mean that maybe I should be reading the current resistance in order to adjust it relatively but I haven't mananged to get any data out of my bike that tells me the current resistance. Unless I'm missing an FTMS method that relatively sets the resistance I'm not sure how to go about solving this one. Looking forward to working with someone if they have any ideas.

Thank you very much for taking a look.


Solution

  • The Set Target Power method I have been using is meant for ERG mode - which makes sense. You explicitly tell the bike which power you want to target.

    What I didn't realise is that you can construct the byte array differently to send and use a procedure called Set Indoor Bike Simulation Parameters - 4.16.2.18 in the bluetooth docs and simulate different slopes! So a change in slope sent in the byte array should result in a change in the bike's resistance.