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

Provide Write capability on DeviceCharacteristic #153

Open
BYEDUCK opened this issue Apr 5, 2023 · 6 comments
Open

Provide Write capability on DeviceCharacteristic #153

BYEDUCK opened this issue Apr 5, 2023 · 6 comments
Labels
enhancement New feature or request

Comments

@BYEDUCK
Copy link

BYEDUCK commented Apr 5, 2023

Is it possible for this library to provide Write method on DeviceCharacteristic (MacOS) ?
Some devices have disabled writeWithoutResponse and only enabled write on their characteristics.

@BYEDUCK
Copy link
Author

BYEDUCK commented Apr 5, 2023

Isn't

func (c DeviceCharacteristic) Write(p []byte) (n int, err error) {
	c.service.device.prph.WriteCharacteristic(p, c.characteristic, true)

	return len(p), nil
}

in gattc_darwin.go all that is needed ?

@aykevl
Copy link
Member

aykevl commented Apr 27, 2023

I would suggest WriteRequest instead of Write.
If you want to make a pull request, please implement this for at least two different BLE stacks, and test it on both stacks. This makes sure the API is portable.

For example, here is an implementation for Linux I wrote a few days ago:

diff --git a/gattc_linux.go b/gattc_linux.go
index 4d3fcb5..7a5ec47 100644
--- a/gattc_linux.go
+++ b/gattc_linux.go
@@ -207,12 +207,20 @@ func (s *DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacter
        return chars, nil
 }
 
+func (c DeviceCharacteristic) WriteRequest(p []byte) (n int, err error) {
+       err = c.characteristic.WriteValue(p, map[string]interface{}{"type": "request"})
+       if err != nil {
+               return 0, err
+       }
+       return len(p), nil
+}
+
 // WriteWithoutResponse replaces the characteristic value with a new value. The
 // call will return before all data has been written. A limited number of such
 // writes can be in flight at any given time. This call is also known as a
 // "write command" (as opposed to a write request).
 func (c DeviceCharacteristic) WriteWithoutResponse(p []byte) (n int, err error) {
-       err = c.characteristic.WriteValue(p, nil)
+       err = c.characteristic.WriteValue(p, map[string]interface{}{"type": "command"})
        if err != nil {
                return 0, err
        }

@deadprogram deadprogram added the enhancement New feature or request label May 1, 2023
@ansoni-san
Copy link
Contributor

ansoni-san commented May 10, 2023

@aykevi I believe gattc_windows.go already uses Write to provide write with response.

Should that be changed as well? It may break compatibility unnecessarily. Although this is early days, so now is a better time to change it than later on 🤔

@aykevl
Copy link
Member

aykevl commented May 10, 2023

@aykevi I believe gattc_windows.go already uses Write to provide write with response.

Hmm, yes, you are right. Write it is, then.

(Also, I realized there are two different ways of calling them, either "write"/"write without response" or "write request"/"write command" - perhaps it's best to not mix those two).

@BYEDUCK
Copy link
Author

BYEDUCK commented May 10, 2023

Yes, these are two separate concepts:

write === write request

write without response === write command

gate_windows.go indeed already has write capability

I've tested the code with changes I mentioned and everything seemed fine at the beginning but with two different sequential write requests when I read response for the second one I still got response for the first.

I didn't have time to investigate what is the problem.

I switched to bleak and can recommend it if you are not sticked to golang.

@Ziphone
Copy link

Ziphone commented Nov 10, 2023

The suggestion of @BYEDUCK did the trick for me. Did an awful workaround using go mod vendor - rather than maintaining a fork.

My project uses the original Go compiler, but includes this library because it is the best option with regular maintenance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants