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

Not working with python3 #4

Open
PaidupatiBharath opened this issue Aug 14, 2018 · 8 comments
Open

Not working with python3 #4

PaidupatiBharath opened this issue Aug 14, 2018 · 8 comments

Comments

@PaidupatiBharath
Copy link

Can you write the package for python3, so that it would be more useful

@rtomasa
Copy link

rtomasa commented Jan 7, 2020

Any new on this topic? I can successfully pip3 install bluetool but python program says it cannot import Bluetooth

@rtomasa
Copy link

rtomasa commented Jan 13, 2020

Just for any other trying to use this library in Python3. I was able to make some very simple changes in the module's imports, transforming the same into relatives instead of absolute and worked perfectly. I only made the changes to the Client because it is what I needed, but probably the same procedure can be done in the server side.

@Tetsujinfr
Copy link

Just for any other trying to use this library in Python3. I was able to make some very simple changes in the module's imports, transforming the same into relatives instead of absolute and worked perfectly. I only made the changes to the Client because it is what I needed, but probably the same procedure can be done in the server side.

hi, can you please give a bit more details on your tweaks to make it work with python3 please? thanks

@rtomasa
Copy link

rtomasa commented Jun 28, 2020

@Tetsujinfr I do not remember how many changes I made as compared with the original code (it was actually not so much), but let me share my modified library so that you can use the same and/or compare the code with the original files. As I mentioned in my old post, it only covers the client side. I hope it helps you.
bluetool-py3.zip

@Tetsujinfr
Copy link

Tetsujinfr commented Jun 28, 2020

@Tetsujinfr I do not remember how many changes I made as compared with the original code (it was actually not so much), but let me share my modified library so that you can use the same and/or compare the code with the original files. As I mentioned in my old post, it only covers the client side. I hope it helps you.
bluetool-py3.zip

thanks a lot for the lightspeed response. I actually just got it to work and I was coming back here to share the tweaks, so I go for it while it is still fresh in my mind (and bash history). Indeed there were quite a few tweaks to be made.
Main steps here (I hope I did not forgot one but the changes are all of the same nature):

  1. Go to the bluetool python dist package folder under /usr/local/lib/python3.6/dist-package/bluetool/ (or wherever you installed it on your system) and edit the __init__.py with (basically add the '.' to import the modules locally):
from .tcp_bridge import TCPBridge, TCPBridgeError
from .tcp_server import TCPServer
from .sink import SocketSink, FileSink
  1. In the file 'bluetool.py', change the line 29 with:
    from bluetool.utils import print_error
    and in the whole file replace the string bluezutils. by bluetool.bluezutils. (23 instances)

  2. In the file 'blueserver.py', change the line 36 with (as usual add the '.' for the local module import) with:
    from .utils import print_error, print_info

  3. you need a package named 'tcpbridge' as indicated in the dependencies of the repo readme, but you need to tweak it as well , so:
    4.1 modify the __init__.py of the tcpbridge module folder too with (basically add the '.' after the from statement as below):

from .tcp_bridge import TCPBridge, TCPBridgeError
from .tcp_server import TCPServer
from .sink import SocketSink, FileSink

4.2 in the file tcp_bridge.py, edit with the below changes. Attention: change from Queue import Queue, Empty to from queue import Queue, Empty : yes, under Python3 the queue stuff requires a lower case instead of a upper case, nuts but necessary!

from queue import Queue, Empty
from .sink import SinkError, SocketSink
from .tcp_server import TCPServer, TCPError

I think that should be it. If you have another error while typing from bluetool import Bluetooth in a python3 console then lookup the error file/line and it is likely you need to add the '.' to the import statement as mentioned above.

Good luck to all, you can still use rtomasa zip file above as well.

@shoeffner
Copy link

@emlid Is there a way to contribute @Tetsujinfr's changes back to the repository and also push this to PyPI? I also had to patch tcpbridge and bluetool manually now. And while I can push bluetool as a fork, the license notices in tcpbridge prohibit me from doing the same – and emlid/tcp-bridge seems to be private or non-existent.

@solderdot72
Copy link

My script runs perfectly fine in Python 2.7.16, I can connect to a bluetooth speaker easily.
Using Python 3.7.3 (and the patches being applied to both bluteool and tcpbridge) the same script executes without errors, however no bluetooth connection is established.
Did anybody try this with Python 3.7.3 and the December 2020 version of Raspberry Pi OS?

@solderdot72
Copy link

Ientified the root cause: Different string handling in Python 3. Ensure the string encoding is utf-8 and everything is fine. Required e.g. when comparing device names and device MAC-addresses.
E.G.:

devices = self.__bluetooth.get_available_devices()
for dev in devices:
    name = dev['name'].decode('utf-8')
    macAdr = dev['mac_address'].decode('utf-8')
    # do whatever you like with name and macAdr...

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

5 participants