b3b / able

Python for Android Bluetooth Low Energy package
MIT License
38 stars 18 forks source link

Write on remote device using ble #10

Closed juasiepo closed 4 years ago

juasiepo commented 4 years ago

I have an working app that publishes a write service with UUID 0d06. This service works ok as I can send some text using the BLE scanner app for android

I am trying to send text using ble's write_characteristic. Here you are my piece of code (based on BLE functions test):

    def write(self, characteristic):
        Logger.debug("Inside write")
        characteristic = self.characteristics['write']
        self.ble.write_characteristic(characteristic, 'TEST')

I have declared:

    uids = {
        'string': '0d01',
        'counter_reset': '0d02',
        'counter_increment': '0d03',
        'counter_read': '0d04',
        'notifications': '0d05',
       write': '0d06'
    }

and

    def on_services(self, ble, status, services):
        if status != GATT_SUCCESS:
            self.state = 'services_error'
            return
        self.state = 'connected'
        self.services = services
        self.read_test_string(ble)
        self.characteristics = {
            'counter_increment': self.services.search(
                self.uids['counter_increment']),
            'counter_reset': self.services.search(
                self.uids['counter_reset']),
            'write': self.services.search(
                self.uids['write']),
        }

The services are detected ok:

12-09 23:07:31.017 10385 10417 I python  : [INFO   ] [Base        ] Start application main loop                                                                                             
12-09 23:07:33.316 10385 10417 I python  : [DEBUG  ] set queue timeout to 1.0                                                                                                               
12-09 23:07:34.737 10385 10385 I python  : [DEBUG  ] on_scan_started
12-09 23:07:39.018 10385 10385 I python  : [DEBUG  ] on_scan_completed
12-09 23:07:40.283 10385 10401 I python  : [DEBUG  ] [on_connection_state_change status=0 state] 2                                                                                          
12-09 23:07:40.400 10385 10401 I python  : [DEBUG  ] [Service discovered] 16fe0d00-c111-11e3-b8c8-0002a5d5c51b                                                                              
12-09 23:07:40.432 10385 10401 I python  : [DEBUG  ] [Characteristic discovered] 16fe0d05-c111-11e3-b8c8-0002a5d5c51b                                                                       
12-09 23:07:40.433 10385 10401 I python  : [DEBUG  ] [Characteristic discovered] 16fe0d06-c111-11e3-b8c8-0002a5d5c51b  

But when I try to send the text I get the following error:

12-09 23:07:42.808 10385 10417 I python  :    File "/data/data/org.kivy.kivy_ble_write/files/app/bluetooth.kv", line 50, in <module>
12-09 23:07:42.809 10385 10417 I python  :      on_press: app.write()
12-09 23:07:42.810 10385 10417 I python  :  TypeError: write() missing 1 required positional argument: 'characteristic'                                                                                          
12-09 23:07:42.810 10385 10417 I python  : Python for android ended.
^CTraceback (most recent call last):
  File "/usr/local/bin/buildozer", line 11, in <module>
    load_entry_point('buildozer==0.40.dev0', 'console_scripts', 'buildozer')()
  File "/usr/local/lib/python3.7/dist-packages/buildozer-0.40.dev0-py3.7.egg/buildozer/scripts/client.py", line 13, in main
    Buildozer().run_command(sys.argv[1:])
  File "/usr/local/lib/python3.7/dist-packages/buildozer-0.40.dev0-py3.7.egg/buildozer/__init__.py", line 1071, in run_command
    self.target.run_commands(args)
  File "/usr/local/lib/python3.7/dist-packages/buildozer-0.40.dev0-py3.7.egg/buildozer/target.py", line 92, in run_commands
    func(args)
  File "/usr/local/lib/python3.7/dist-packages/buildozer-0.40.dev0-py3.7.egg/buildozer/targets/android.py", line 1336, in cmd_logcat
    show_output=True)
  File "/usr/local/lib/python3.7/dist-packages/buildozer-0.40.dev0-py3.7.egg/buildozer/__init__.py", line 311, in cmd                                                                                            
    readx = select.select([fd_stdout, fd_stderr], [], [])[0]
KeyboardInterrupt

Any idea what am I doing wrong?

Thanks for your time

b3b commented 4 years ago

This happens because write method is declared with a characteristic argument. Changing def write(self, characteristic) to def write(self) should help.

juasiepo commented 4 years ago

Now I am getting:

11921 11941 I python : File "/home/juan/git/able/write/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/kivy_ble_write/kivy/lang/builder.py", line 64, in custom_callback 11921 11941 I python : File "/data/data/org.kivy.kivy_ble_write/files/app/bluetooth.kv", line 50, in 11921 11941 I python : on_press: app.write() 11921 11941 I python : File "/home/juan/git/able/write/.buildozer/android/app/main.py", line 194, in write 11921 11941 I python : File "/home/juan/git/able/write/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/kivy_ble_write/able/queue.py", line 18, in wrapper 11921 11941 I python : File "/home/juan/git/able/write/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/kivy_ble_write/able/queue.py", line 63, in enque 11921 11941 I python : File "/home/juan/git/able/write/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/kivy_ble_write/able/queue.py", line 36, in wrapped 11921 11941 I python : File "/home/juan/git/able/write/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/kivy_ble_write/able/queue.py", line 77, in execute_next 11921 11941 I python : File "/home/juan/git/able/write/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/kivy_ble_write/able/queue.py", line 90, in execute_task 11921 11941 I python : File "/home/juan/git/able/write/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/kivy_ble_write/able/dispatcher.py", line 127, in write_characteristic 11921 11941 I python : File "jnius/jnius_export_class.pxi", line 760, in jnius.jnius.JavaMethod.call 11921 11941 I python : File "jnius/jnius_conversion.pxi", line 128, in jnius.jnius.populate_args 11921 11941 I python : jnius.jnius.JavaException: Expecting a python list/tuple, got 'TEST' 11921 11941 I python : Python for android ended.

Thanks for your time

b3b commented 4 years ago

It seems like Pyjnius do not convert str arguments to a Java arrays. It worked before, with Python 2.

I added a workaround, so write_characteristic(characteristic, 'TEST') should work with a new able version. To work with a previous version, code should be changed to write_characteristic(characteristic, b'TEST').

juasiepo commented 4 years ago

It works with the write_characteristic(characteristic, b'TEST') I will test later the newest version

:) thanks a lot for your time