LinuxKoku
Posts: 75
Joined: Wed May 16, 2018 9:16 am
Location: France
Contact: Website

[Solved] Send/Receive a byte frame with Bluetooth between PC and Raspberry PI 3

Tue Jul 03, 2018 8:07 am

Hello,
I would like to create a code in python 3 on my Raspberry PI 3 which will aim to read frames of bytes sent from a pc that uses a Bluetooth dongle, by my RPI using its built-in Bluetooth (a kind of client server in Bluetooth)

So I did several research, but I can not even do a good test to see if my Bluetooth works, the Bluetooth RPI works fine in command lines (bluetoothctl), but the code test does not work and I receive an error: here is the code:

Code: Select all

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
  
import bluetooth
from random import randint
  
bd_addr = "XX:XX:XX:XX:XX:XX" #j'ai mis l'add mac du raspberry que j'ai pris avec la la ligne de commande (bluetoothctl)
  
port = 1
 
sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((bd_addr, port))
  
rnd = randint(1,3)
toSend = "profil:%d" % rnd
sock.send(toSend)
data = sock.recv(1024)
print "received [%s]" % data
sock.close()
Here's the mistake: (If I unlock here I think quickly move forward)
sock.connect ((bd_addr, port))
File "<string>", line 5, in connect
bluetooth.btcommon.BluetoothError: (113, 'No route to host')


:?:
Indications on the error?
Indications (tutorials, documentation, codes, ...) on the code I want to create?

Thank you
Last edited by LinuxKoku on Mon Jul 09, 2018 2:33 pm, edited 1 time in total.
Thank you all :)

User avatar
topguy
Posts: 5569
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Send/Receive a byte frame with Bluetooth between PC and Raspberry PI 3

Tue Jul 03, 2018 9:02 am

Code: Select all

bd_addr = "XX:XX:XX:XX:XX:XX" #j'ai mis l'add mac du raspberry que j'ai pris avec la la ligne de commande (bluetoothctl)
Did you remove the MAC address here on purpose or is it cut&paste ?

LinuxKoku
Posts: 75
Joined: Wed May 16, 2018 9:16 am
Location: France
Contact: Website

Re: Send/Receive a byte frame with Bluetooth between PC and Raspberry PI 3

Tue Jul 03, 2018 9:42 am

[Edited]
Hello,

I put the MAC address of the RPI (B8: 27: EB: 48: 3A: 70)

I think I have found what's wrong! the RPI port com service is not enabled or not installed, i tried to follow this tutorial (viewtopic.php?p=947185#p947185) and i i discovered when my bluetooth file the rfcomm file does not exist, so i think there is something that is not installed yet. I even did a test with:

Code: Select all

sudo rfcomm connect hci0 xx: xx: xx: xx: xx: xx 
Can't connect RFCOMM socket: Invalid exchange 
:?: What do I need to install to get this configuration file to enable the bluetooth port com service?

Thank you
Last edited by LinuxKoku on Tue Jul 03, 2018 12:37 pm, edited 2 times in total.
Thank you all :)

LinuxKoku
Posts: 75
Joined: Wed May 16, 2018 9:16 am
Location: France
Contact: Website

Re: Send/Receive a byte frame with Bluetooth between PC and Raspberry PI 3

Tue Jul 03, 2018 11:56 am

Hello

In need of emergency help, let me post the question about the problem of rfcomm.conf file that does not exist on my Raspberry PI 3, to have more chance to have answers

Thank you
Thank you all :)

User avatar
B.Goode
Posts: 7868
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Send/Receive a byte frame with Bluetooth between PC and Raspberry PI 3

Tue Jul 03, 2018 12:10 pm

I put the IP address of the RPI (BB: 27: EB: 48: 3A: 70)
Since it is your area of specialist knowledge it is important to say that is not an IP Address. It is some form of MAC (hardware) address. I don't know enough to say whether you might have used the correct MAC address for your Bluetooth adaptor.

I realise that your aim is to communicate with a PC. But for troubleshooting/diagnostics you might be interested in the Blue Dot tool for Raspbian and Android. http://bluedot.readthedocs.io/en/latest ... arted.html

This would allow you to prove that the bluetooth adapator in your RPi is working, and reading the documentation might help you to understand bluetooth networking and configuration a little better.

LinuxKoku
Posts: 75
Joined: Wed May 16, 2018 9:16 am
Location: France
Contact: Website

Re: Send/Receive a byte frame with Bluetooth between PC and Raspberry PI 3

Tue Jul 03, 2018 12:27 pm

The built-in bluetooth works very well! I even send a file to my PC with Blueman, but me is to send data automatically using Python code, but for its, I must activated the bluetooth COM serial port service for data exchange! and for his I have to modify the configuration file rfcomm.conf! gold me this file it does not exist in my Raspberry PI, so my problem is a missing file, so I think I would have to install something more :roll:
Thank you all :)

User avatar
B.Goode
Posts: 7868
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Send/Receive a byte frame with Bluetooth between PC and Raspberry PI 3

Tue Jul 03, 2018 12:33 pm

I put the IP address of the RPI (BB: 27: EB: 48: 3A: 70)
Even considering that as a MAC address, it seems to be invalid - using several online lookup tools the OID is not recognised.

Maybe it should be B8: 27: EB: 48: 3A: 70 - that would indicate a device manufactured by Raspberry Pi.

LinuxKoku
Posts: 75
Joined: Wed May 16, 2018 9:16 am
Location: France
Contact: Website

Re: Send/Receive a byte frame with Bluetooth between PC and Raspberry PI 3

Tue Jul 03, 2018 12:38 pm

Exactly, it is errors of inattention
Thank you all :)

User avatar
Douglas6
Posts: 4670
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Send/Receive a byte frame with Bluetooth between PC and Raspberry PI 3

Tue Jul 03, 2018 4:11 pm

Good call on the 'MAC' address! Strictly speaking, it's not a MAC address either, it's a 'Bluetooth device address', but that is raising pedantry to an unnecessary level.

User avatar
B.Goode
Posts: 7868
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Send/Receive a byte frame with Bluetooth between PC and Raspberry PI 3

Tue Jul 03, 2018 6:43 pm

Strictly speaking, it's not a MAC address either, it's a 'Bluetooth device address'
I am more than happy to be set straight by one of the few people who can comment knowledgeably about the Bluetooth implementation on the RPi!

Since I am not in front of a bt-enabled RPi to check for myself, could you say whether a Bluetooth device address on an RPi begins with a manufacturer-specific OID as defined for an Ethernet MAC address? And if so, is the OID numerically the same for both networks?

User avatar
Douglas6
Posts: 4670
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Send/Receive a byte frame with Bluetooth between PC and Raspberry PI 3

Tue Jul 03, 2018 7:11 pm

Yes and yes. On my Pi3, both the Bluetooth device address and WiFi MAC address begin with B8:27:EB, which an online lookup tells me is registered to the Raspberry Pi Foundation.

To be honest, the difference between Bluetooth device addresses (BD_ADDR or bdaddr) and MAC addresses may be just conventional. Both are governed by the IEEE, and share formats and manufacturer OUI standards. Although Bluetooth devices may also use randomly generated bdaddrs.

LinuxKoku
Posts: 75
Joined: Wed May 16, 2018 9:16 am
Location: France
Contact: Website

Re: Send/Receive a byte frame with Bluetooth between PC and Raspberry PI 3

Wed Jul 04, 2018 12:24 pm

Hello,
I managed to send byte frames from the raspberry PI to my PC, I could read his grace to a Terminal, to display his I create a server code on my PC to read this data, but I came across an error, here are the two codes:

Customer code works very well, without error:

Code: Select all

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
 import bluetooth
import socket

bd_addr = "20:16:03:09:3A:E6" 
port = 1
sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((bd_addr, port))
 
trame_envoi_1= [0x7E, 0x7E, 0x73, 0x02, 0x73, 0x02]

for i in trame_envoi_1:
    sock.send(bytes(i))
    print(hex(i))

trame_recue=[]
i=0
while i<9:
    x=sock.recv(1)
    trame_recue.append(ord(x))
    i=i+1
print ('Trame_recue = ',trame_recue)
Server Code:

Code: Select all

import bluetooth
import socket

hostMACAddress = 'B8:27:EB:48:3A:70' MAC Raspberry PI
port = 12
backlog = 1
size = 1024
s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
print('OK')
s.bind((hostMACAddress, port))
s.listen(backlog)

client, clientInfo = s.accept()
data =s.recv(size)
print(data)

client.close()
s.close()
Server Error :
OK
Traceback (most recent call last):
File "C:\Users\FujitsuGreen\Desktop\serveurb.py", line 10, in <module>
s.bind((hostMACAddress, port))
File "C:\Python34\lib\site-packages\bluetooth\msbt.py", line 60, in bind
bt.bind (self._sockfd, addr, port)
OSError


Knowing that I downloaded pybluez in this link (https://github.com/karulis/pybluez) then I installed with the command prompt (cmd of Windows) by running setup.py install and everything is well installed as bluetooth import is recognized

  :?: What is the origin of this error?
Thank you all :)

User avatar
Douglas6
Posts: 4670
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Send/Receive a byte frame with Bluetooth between PC and Raspberry PI 3

Wed Jul 04, 2018 11:47 pm

I'm a little confused. You seem to be running the 'server code' on the Pi, but your 'server error' is clearly a Windows error.

Can't help much with Windows code. One guess is that you've installed a Linux-compiled Python library on the Windows PC. And why not use the standard PyBluez fork?

LinuxKoku
Posts: 75
Joined: Wed May 16, 2018 9:16 am
Location: France
Contact: Website

Re: Send/Receive a byte frame with Bluetooth between PC and Raspberry PI 3

Thu Jul 05, 2018 10:53 am

Hello, I am French so excuse my english :oops: ! I wanted to say:

Client code on Raspberry PI
Server code on Windows
Thank you all :)

LinuxKoku
Posts: 75
Joined: Wed May 16, 2018 9:16 am
Location: France
Contact: Website

Re: Send/Receive a byte frame with Bluetooth between PC and Raspberry PI 3

Mon Jul 09, 2018 2:32 pm

Hello,

The error is caused by the allocation of the com port 12 to several connections, it was necessary to disconnect all the other systems connected to this port or put on the com 13, and everything works!

Thank you again for your help :mrgreen:
Thank you all :)

Return to “Networking and servers”