Hello everyone,
Goal
My goal is to turn on my BOOM speaker from Ultimate Ears via Phytonista.
Someone already got it working using gatttool as you can see here. It has to be possible to do the same from Pythonista. I'm trying to mimic the following command in my Phytonista script:
gatttool -i hci0 -b $MAC_ADDRESS --char-write-req -a 0x0003 -n 4098ADA356C401
This writes the 4098ADA356C401 value to the 0x0003 handle, which causes the speaker to turn on.
In my Phytonista script i'm using this function:
self.peripheral.write_characteristic_value(c, data, True)
How can I pass the value and handle to the write_characteristic_value function?
The documentation says: data should be a byte string. How can i transform them into a byte string?
I tried the following:
- data = bytes('4098ADA356C401', 'utf-8')
- data = bytes('0x4098ADA356C401', 'utf-8')
- data = hex(int('4098ADA356C401', 16))
As you can see I'm not sure what the syntax would be or where to pass the handle (maybe I don't need the handle because i'm already talking to the right characteristic?).
Response
When I execute my Phytonista script the did_write_value function is called. When I print the error it says: (15, 'Encryption is insufficient.'). My assumption is that I did not send the correct data value.
Full script:
import cb
import sound
import time
import struct
class SpeakerManager (object):
def __init__(self):
self.peripheral = None
def did_discover_peripheral(self, p):
if p.uuid and 'UUID-OF-MY-SPEAKER' in p.uuid and not self.peripheral:
self.peripheral = p
print('Connecting to UltraBOOM...')
cb.connect_peripheral(p)
def did_connect_peripheral(self, p):
print('Connected:', p.uuid)
print('Discovering services...')
p.discover_services()
def did_fail_to_connect_peripheral(self, p, error):
print('Failed to connect: %s' % (error,))
def did_disconnect_peripheral(self, p, error):
print('Disconnected, error: %s' % (error,))
self.peripheral = None
def did_discover_services(self, p, error):
for s in p.services:
print('Service uuid: ', s.uuid)
if s.uuid == '61FE':
print('Discovered turning-on service, discovering characteristics...')
p.discover_characteristics(s)
def did_discover_characteristics(self, s, error):
print('Did discover characteristics...')
for c in s.characteristics:
if c.uuid == 'UUID-OF-THE-CHARACTERISTIC':
self.peripheral.set_notify_value(c, True)
data = bytes('4098ADA356C401', 'utf-8')
print('Write bytes:', data)
self.peripheral.write_characteristic_value(c, data, True)
def did_write_value(self, c, error):
print('did_write_value to', c.uuid)
print('Write error:', error)
def did_update_value(self, c, error):
print('did_update_value')
print('(1)', c.value)
mngr = SpeakerManager()
cb.set_central_delegate(mngr)
print('Scanning for peripherals...')
cb.scan_for_peripherals()
try:
while True: pass
except KeyboardInterrupt:
cb.reset()