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

Error in Python Implementation of ble_cmd_attributes_write #7

Closed
bsalinas opened this issue Aug 29, 2013 · 2 comments
Closed

Error in Python Implementation of ble_cmd_attributes_write #7

bsalinas opened this issue Aug 29, 2013 · 2 comments

Comments

@bsalinas
Copy link

When I call:

handle=15
value='01'
ble.send_command(ser, ble.ble_cmd_attributes_write(handle, 0, value))
ble.check_activity(ser, 1)

I get the following error:

Traceback (most recent call last):
  File "test.py", line 14, in <module>
    machine.groups[0].startBrewing()
  File "/Users/ben/Github/bt4prototype/MockEspresso/Group.py", line 86, in startBrewing
    listener.didStartBrewing(self.id)
  File "/Users/ben/Github/bt4prototype/MockEspresso/EspressoMachine.py", line 32, in didStartBrewing
    self.bluetooth.write_attribute(24,'01')
  File "/Users/ben/Github/bt4prototype/MockEspresso/Bluetooth.py", line 55, in write_attribute
    self.ble.send_command(self.ser, self.ble.ble_cmd_attributes_write(handle, 0, value))
  File "/Users/ben/Github/bt4prototype/bglib.py", line 172, in ble_cmd_attributes_write
    return struct.pack('<4BHBB' + str(len(value)) + 's', 0, 4 + len(value), 2, 0, handle, offset, len(value), b''.join(chr(i) for i in value))
  File "/Users/ben/Github/bt4prototype/bglib.py", line 172, in <genexpr>
    return struct.pack('<4BHBB' + str(len(value)) + 's', 0, 4 + len(value), 2, 0, handle, offset, len(value), b''.join(chr(i) for i in value))
TypeError: an integer is required

I think the problem line is Line 172 in bglib.py
It reads:

return struct.pack('<4BHBB' + str(len(value)) + 's', 0, 4 + len(value), 2, 0, handle, offset, len(value), b''.join(chr(i) for i in value))

When I change it to:

return struct.pack('<4BHBB' + str(len(value)) + 's', 0, 4 + len(value), 2, 0, handle, offset, len(value), b''.join(i for i in value))

I am able to successfully write attributes.

Am I overlooking something?

@jrowberg
Copy link
Owner

Hi @bsalinas,

Your solution works, but I wrote the code under the assumption that the values being written would be byte arrays rather than string literals, and without extra code I don't think there's a simple way to handle both seamlessly. If you change your "value" definition to the following:

value = [ 0x30, 0x31 ]

...then it should work without changes. For efficiency, data sent over BLE is often sent in compact binary form rather than strings, which is why I made that design choice. Does that work?

@bsalinas
Copy link
Author

Thanks for the clarification. That will work just fine.
On Aug 29, 2013 3:54 PM, "Jeff Rowberg" notifications@github.com wrote:

Hi @bsalinas https://github.com/bsalinas,

Your solution works, but I wrote the code under the assumption that the
values being written would be byte arrays rather than string literals, and
without extra code I don't think there's a simple way to handle both
seamlessly. If you change your "value" definition to the following:

value = [ 0x30, 0x31 ]

...then it should work without changes. For efficiency, data sent over BLE
is often sent in compact binary form rather than strings, which is why I
made that design choice. Does that work?


Reply to this email directly or view it on GitHubhttps://github.com//issues/7#issuecomment-23518596
.

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