Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't write the correct values #66

Closed
Dragelans opened this issue Jun 2, 2024 · 7 comments
Closed

Can't write the correct values #66

Dragelans opened this issue Jun 2, 2024 · 7 comments

Comments

@Dragelans
Copy link

Hi, I have a BLE balance board I want to connect to Unity but having issues when writing to the device.
I tested with Universal Sample and some other libraries. The Device is working and also can get notify from the Unity Demo scene.

As example one of the characteristics control the led on the board as R G B Rainbow and gets 4 byte value so when I send "FF 00 00 00" it will glow red.
I tried to change parse text input as Hex but couldn't manage to write to the board.

public static Byte[] FormatData(string data, DataFormat format)
    {
        try
        {
                string[] values = data.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                byte[] bytes = new byte[values.Length];

                for (int i = 0; i < values.Length; i++)
                    bytes[i] = Convert.ToByte(values[i], (format == DataFormat.Dec ? 10 : (format == DataFormat.Hex ? 16 : 2)));

            return bytes;

            
        }
        catch (Exception error)
        {
            return null;
        }
    }`

`
    public void Write()
    {

        byte[] payload = FormatData(writeInput.text,DataFormat.Hex);
        BleApi.BLEData data = new BleApi.BLEData();
        data.buf = new byte[512];
        data.size = (short)payload.Length;
`
@adabru
Copy link
Owner

adabru commented Jun 3, 2024

Hi @Dragelans :),
what error message do you get?

@Dragelans
Copy link
Author

Hi @adabru ,

I don't get any error , when I set blocking mode true it just freezes though so not sure if it's totally cause of an error or the format type I send

@Dragelans
Copy link
Author

Hi @adabru again,

With the dll, I wrote a test console application to check blocking mode. It returns "Write Success" but having no affect on the device.

In datasheet, there are specific lengths in this case 4 bytes. on toolBLEx I manually send the bytes it works but if send more than 4 it doesn't have any affect so I thought it might be about the buffer size maybe but when i change buffer size to bytes length it gives error about not correct size and closes the application.

@adabru
Copy link
Owner

adabru commented Jun 8, 2024

Are you setting the BLEData size property to 4?

@Dragelans
Copy link
Author

I did, but on code when sending it's already resizing bytebuffer array from 0 to data size so that was not the issue.

I checked another BLE library for console and compared the send code. there were 2 main differences.

1- Byteorder, I see in reference library it goes something like "Writer.Byteorder(LittleEndian)" meanwhile it didn't on dll. I actually encountered this in another library where i had to enter bytes reverse. Though don't think that was the main problem since even if I entered in wrong order it should have done something.

2- WriteWithoutResponse , in other library It was WriteWithResponse so I changed that and now it's working. Since haven't work on bluetooth before I have no idea why it makes a difference but now it works.

DataWriter writer;
writer.WriteBytes(array_view<uint8_t const> (data.buf, data.buf + data.size));
IBuffer buffer = writer.DetachBuffer();
auto status = co_await characteristic.WriteValueAsync(buffer, GattWriteOption::WriteWithoutResponse);

@adabru
Copy link
Owner

adabru commented Jun 10, 2024

Thank you very much for sharing. There seems to be a related issue: #33

Do you have a thought on how that could be implemented so that users won't have the same hard time as you did? Otherwise it can be added to the readme.

@Dragelans
Copy link
Author

Thank you very much for sharing. There seems to be a related issue: #33

Do you have a thought on how that could be implemented so that users won't have the same hard time as you did? Otherwise it can be added to the readme.

Good question, though not sure. Seems like difference between With and Without Response is With sends back acknowledgment but don't know how this affects the device since again as I mentioned, don't have much experience with bluetooth in general.

I'd say WithResponse can be added as default to repo but that might slow down the connection as waiting response causes a bit delay.

Maybe a section on readme can be added for possible errors and solutions

@adabru adabru closed this as completed in 03b4dcf Jun 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants