Bluetooth BL Client won't connect
-
Hi,
I am trying to run a simple Bluetooth LE client program to connect to an RFduino but, like others (https://forum.pycom.io/topic/791/bluetooth-wipy-client-connecting-to-wipy-peripheral), I am having problems getting the code to work. The code will scan and find the server but will not connect, reporting a 'connection refused' error. I have tried this with both the RFduino and a Cypress BLE development board (CY8CKIT-042-BLE), both are found during scanning but neither will connect. I can connect to both boards using scanning software on a PC, so I know both devices are working correctly.I am using the latest firmware release (1.7.6.b1), and have also tried and older version (1.4.0.b1), as BLE client code has been reported to work with this, but with no joy.
I have included listings of the code, output response and WiPy 2.0 firmware below.
Any help in getting this working would be appreciated.
Thanks,
Mervynfrom machine import Pin from network import Bluetooth import machine import time import binascii # Initalise GP17 in gpio mode (alt=0) as an input with pull-up enabled. pin_in = Pin('G17', mode=Pin.IN, pull=Pin.PULL_UP) # Initalise GP16 in gpio mode (alt=0) as an output. p_out = Pin('G16', mode=Pin.OUT) bt = Bluetooth() bt.start_scan(-1) print("Starting BLE device scan.......") while True: adv = bt.get_adv() if adv and bt.resolve_adv_data(adv.data, Bluetooth.ADV_NAME_CMPL) == 'RFduino': print() bt.stop_scan() if not bt.isscanning(): print("Scanning stopped.") print("Device {} found at address {}".format(bt.resolve_adv_data(adv.data, Bluetooth.ADV_NAME_CMPL), binascii.hexlify(adv.mac))) print("Connecting....") #conn = bt.connect("00A0502C6816") #Cypress BLE conn = bt.connect('f8f27d6df919') #RFduino print("Connected....")
The response to running this code is:
Starting BLE device scan....... Scanning stopped. Device RFduino found at address b'f8f27d6df919' Connecting.... Traceback (most recent call last): File "main.py", line 27, in <module> OSError: connection refused MicroPython v1.8.6-703-g5e80328a on 2017-07-05; WiPy with ESP32 Type "help()" for more information. >>>
The firmware release and version numbers are:
(sysname='WiPy', nodename='WiPy', release='1.7.6.b1', version='v1.8.6-703-g5e80328a on 2017-07-05', machine='WiPy with ESP32')
-
@tenspot said in Bluetooth BL Client won't connect:
bt.connect
from network import LoRa
from network import Bluetoothimport machine
import socket
import pycom
import binasciibluetooth = Bluetooth()
bluetooth.start_scan(10)
adv = Nonewhile True:
adv = bluetooth.get_adv() #if adv and bluetooth.resolve_adv_data(adv.data, Bluetooth.ADV_NAME_CMPL) == 'JonathanBLE': try: #print(bluetooth.resolve_adv_data(adv.data, Bluetooth.ADV_APPEARANCE)) bluetooth.connect (b'\xe0\x00\xc3\xca\x16\xf9') #JonathanBLE mac adress?
Is this the wrong way to connect using the mac adress.. ? I connect to the BLE with the name and it connect in seconds... but I want to be able to connect to devices using there mac adress instead... Im using LOPY btw ... and I want to try send data between 2 LOPY decies with BLE...
-
Re: Bluetooth BL Client won't connect
I have now sorted the problem. I was trying to connect using what I thought was a hard coded mac address (i.e. 'f8f27d6df919') as described in the docs:
bluetooth.connect('112233eeddff') # mac address is accepted as a string
It seems this is not the right format so I am now usingconn = bt.connect(adv.mac)
and the code is working.Update 9/7/2017:
The correct format for a hard coded mac address is:b'\xF8\xF2\x7D\x6D\xF9\x19'
, sobt.connect(b'\xF8\xF2\x7D\x6D\xF9\x19')
works in the above example.