User avatar
marciokoko
Posts: 257
Joined: Sat Aug 27, 2016 4:33 pm

Unable to connect to BLE device

Sun May 20, 2018 5:15 pm

RPI2
I had a RPi2 with a uart-gpio connected hm10 ble module that I used to send and receive commands to a ble-relayboard (RelayBoard1, RB1). So RPi2 was connected to RB1 and it worked fine for the longest time.

RPI3
Now that I have an RPi3 with touch screen I wanted to add a gui program for fun. Of course, first I had to test the new python scripts and such. So I connected my RPi3 to another, identical ble-relayboard (RB2). Everything worked fine, I tested the gui python executable with the touchscreen and got the relays to toggle as expected.

So I disconnected the RPi2 from the RB1 and tried connecting the RPI3 to RB1, this is where Ive run into trouble. All im trying to do is run this script on the RPI3 which is equivalent to what I had on the RPi2 in order to toggle a relay:

Code: Select all

import bluepy.btle as btle
p = btle.Peripheral("00:0E:0B:00:75:12", "random")
s = p.getServiceByUUID("0000ffe0-0000-1000-8000-00805f9b34fb")
c = s.getCharacteristics()[0]
c.write("o", "utf-8")
p.disconnect()
But whenever I run it I get either:

bluepy.btle.BTLEException: Failed to connect to peripheral 00:0E:0B:00:75:12, addr type: random

or

File "/usr/local/lib/python2.7/dist-packages/bluepy/btle.py", line 449, in getServiceByUUID
raise BTLEException(BTLEException.GATT_ERROR, "Service %s not found" % (uuid.getCommonName()))
bluepy.btle.BTLEException: Service ffe0 not found

What Ive tried and what I know:

1. I know the relay board has 2 macs with different services (cause ive seen it in bluetoothctl):
-00:0E:0B:00:75:12 with 0000ffe0-0000-1000-8000-00805f9b34fb
-00:0E:0E:00:75:12 with 00001101-0000-1000-8000-00805f9b34fb which has something to do with the EDR bit on the relay board

2. I believe the first mac is the correct one for a couple of reasons:
- Ive seen this mac only from a BLE Scanner app on my phone, never the other one
- when ive managed to connect to either mac via bluetoothctl, the 0B on has 3 services vs the 0E one which has only 1

3. Ive been able to pair, connect and trust to the 0B one although sometimes it wont let me pair, like recently, even after removing it.

4. Ive not been able to connect to the 0E one, but i have been able to trust & pair it.

5. In bluetoothctl I am able to
- See both as available devices [devices]
- See 0E as paired device but not 0B (even though last night it did let me pair with 0B) [paired-devices]
- [agent on] already registered (although this morning i had to use [power on] first
- my controller is powered, pairable but not discoverable [show] (ive never changed this and i was able to pair & connect yesterday)
- 0E is paired, trusted but not connected (ive never been able to connect to this one) [info]
- 0B is trusted, not paired or connected (ive been able to pair and connect last night but even then when I ran the python code it failed to 'connect')
- Try to pair to 0B but get Failed to pair: org.bluez.Error.Failed
- Try to connect to 0B same fail as above although yesterday i was able to do both at some point
- Tried removing both, exiting ctl, restarting service, ctl, agent on, default-agent, scan on, pair = it connects, stalls and finally disconnects and returns AuthenticationFailed.
- So I tried trusting it first; trust succeeded...still will not pair (wont even ask me for pin anymore)
- Now connect to 0B works, successful, lists the UUIDs (3 of them) and appears as trusted and connected
- exit ctl and run py...but again: bluepy.btle.BTLEException: Failed to connect to peripheral 00:0E:0B:00:75:12, addr type: random
- Return to ctl, 0B still appears as trusted and connected...disconnected it in ctl [disconnect], exit
- python, File "/usr/local/lib/python2.7/dist-packages/bluepy/btle.py", line 449, in getServiceByUUID
raise BTLEException(BTLEException.GATT_ERROR, "Service %s not found" % (uuid.getCommonName()))
bluepy.btle.BTLEException: Service ffe0 not found

Dunno what else to try!

I've attached both terminal session (saturday was the first day I tried and I was able to pair with 0B, sunday which is today). Today (sunday) is probably the most organized one and the last 30 lines being the more important ones where I ran the commands I outlined here in step 5). Its a zip file with 2 pdfs in it because the forum wouldnt let me upload a pdf file.
Archive.zip
(118.88 KiB) Downloaded 7 times

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

Re: Unable to connect to BLE device

Sun May 20, 2018 6:16 pm

You had mentioned previously that you were able to write to the relay board's characteristic. What has changed since then?

The board appears to have two interfaces; a BLE one (0B) and a classic (BR/EDR) one (0E). You would use totally different techniques on each to communicate. Since you are having so much trouble with BLE programming, perhaps it would be best to try the classic Bluetooth interface.

User avatar
marciokoko
Posts: 257
Joined: Sat Aug 27, 2016 4:33 pm

Re: Unable to connect to BLE device

Sun May 20, 2018 6:29 pm

It worked perfectly last time remember? The only thing that changed js the relay board because it's a different unit but exact same model! It's just that the unit I tested (that you helped me out with and it worked) was an extra one I have. Now I'm moving over to the unit I have wired at the house.

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

Re: Unable to connect to BLE device

Mon May 21, 2018 12:15 am

Well, I would try removing the "random" bit, just to be sure. I would also double-check the primary services of the board, to see if the same service (UUID) is also available on the new board.

User avatar
marciokoko
Posts: 257
Joined: Sat Aug 27, 2016 4:33 pm

Re: Unable to connect to BLE device

Mon May 21, 2018 1:16 am

ok just as reference, here is the code from the first post (viewtopic.php?f=91&t=213594) when I was testing the python script on the trial board (RPi3-RB1):

Code: Select all

p = btle.Peripheral("00:0E:0B:0F:0F:B9", "random")
s = p.getServiceByUUID("0000ffe0-0000-1000-8000-00805f9b34fb")
and this is the new code for the board I need it to work on but it fails(RPi3-RB2):

Code: Select all

p = btle.Peripheral("00:0E:0B:00:75:12", "random")
s = p.getServiceByUUID("0000ffe0-0000-1000-8000-00805f9b34fb")
The MACs are different but the UUID's are identical. H is the data from the bluetoothctl terminal session:

Code: Select all

[bluetooth]# info 00:0E:0B:00:75:12
Device 00:0E:0B:00:75:12
     Name: BT Bee-BLE
     Alias: BT Bee-BLE
     Paired: no
     Trusted: no
     Blocked: no
     Connected: no
     LegacyPairing: no
     UUID: Unknown
  (0000ffe0-0000-1000-8000-00805f9b34fb)
  [bluetooth]# connect 00:0E:0B:00:75:12
Attempting to connect to 00:0E:0B:00:75:12
[CHG] Device 00:0E:0B:00:75:12 Connected: yes
Connection successful
[CHG] Device 00:0E:0B:00:75:12 UUIDs:
00001800-0000-1000-8000-00805f9b34fb
     00001801-0000-1000-8000-00805f9b34fb
     0000ffe0-0000-1000-8000-00805f9b34fb
     [bluetooth]# info 00:0E:0B:00:75:12
Device 00:0E:0B:00:75:12
     Name: BT Bee-BLE
     Alias: BT Bee-BLE
     Paired: no
     Trusted: yes
     Blocked: no
     Connected: yes
     LegacyPairing: no
     UUID: Generic Access Profile
  (00001800-0000-1000-8000-00805f9b34fb)
     UUID: Generic Attribute Profile
(00001801-0000-1000-8000-00805f9b34fb)
     UUID: Unknown
  (0000ffe0-0000-1000-8000-00805f9b34fb)
  
I just tried the old board again with the old mac and service uuid and it works perfectly with the python code.

Then I switched over to the new board with the modified code with the respective mac and uuid and both with and without the random bit and i still get error:

A. cannot find that service with the random bit

B. cannot connect to the mac without the random bit

I'm attaching an image of the BLE scanner app screenshots of the test RB1 which is identified by the app as BT Bee-BLE.
9873FB22-A796-48D3-AA35-4418CA72811F.jpeg
9873FB22-A796-48D3-AA35-4418CA72811F.jpeg (133.43 KiB) Viewed 685 times
And here is the same data for the wired board RB2:
71EDC50F-6C36-4A2E-AC51-673AA26D1E62.jpeg
71EDC50F-6C36-4A2E-AC51-673AA26D1E62.jpeg (100.89 KiB) Viewed 681 times

User avatar
marciokoko
Posts: 257
Joined: Sat Aug 27, 2016 4:33 pm

Re: Unable to connect to BLE device

Tue May 22, 2018 3:14 pm

So basically I have this issue. Could it be something in the tsrb430 ble relay board is saved from the RPi2 and preventing it from seeing or recognizing the service or because it has the same service UUID as RB2-test unit, it is now clashing with the UUID on the Wired-unit?:
tsrb430RPisv2.jpg
tsrb430RPisv2.jpg (155.47 KiB) Viewed 675 times
Since it seems to be a services issue, this is what I get when I get and print all services on the RB1:
Service <uuid=Generic Attribute handleStart=12 handleEnd=15>
Service <uuid=Generic Access handleStart=1 handleEnd=11>
Service <uuid=ffe0 handleStart=16 handleEnd=65535>
[email protected]:~ $
and this is what I get when i print out the chracteristics:
[email protected]:~ $ python rfcomm.py
Handle UUID Properties
-------------------------------------------------------
0x0E 00002a05-0000-1000-8000-00805f9b34fb INDICATE
0x12 0000ffe1-0000-1000-8000-00805f9b34fb NOTIFY READ WRITE NO RESPONSE

User avatar
marciokoko
Posts: 257
Joined: Sat Aug 27, 2016 4:33 pm

Re: Unable to connect to BLE device

Wed May 23, 2018 10:20 pm

I installed hcidump and im trying to run btmgmt le on but i cant, command not found.

I did:

Code: Select all

[email protected]:~ $ sudo apt-get install bluez-hcidump
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  bluez-hcidump
0 upgraded, 1 newly installed, 0 to remove and 193 not upgraded.
Need to get 157 kB of archives.
After this operation, 490 kB of additional disk space will be used.
Get:1 http://archive.raspberrypi.org/debian/ jessie/main bluez-hcidump armhf 5.23-2+rpi2 [157 kB]
Fetched 157 kB in 2s (71.3 kB/s)        
Selecting previously unselected package bluez-hcidump.
(Reading database ... 113091 files and directories currently installed.)
Preparing to unpack .../bluez-hcidump_5.23-2+rpi2_armhf.deb ...
Unpacking bluez-hcidump (5.23-2+rpi2) ...
Processing triggers for man-db (2.7.0.2-5) ...
Setting up bluez-hcidump (5.23-2+rpi2) ...
[email protected]:~ $ btmgmt le on
-bash: btmgmt: command not found
[email protected]:~ $ sudo blescan
Scanning for devices...
    Device (new): 00:0e:0b:00:75:12 (random), -52 dBm 
	Flags: <06>
	Incomplete 16b Services: <e0ff>
	16b Service Data: <00b000000000>
	Manufacturer: <484d000e0b007512>
    Device (new): 7f:cf:97:15:a6:f4 (random), -82 dBm (not connectable)
	Complete 16b Services: <9ffe>
	16b Service Data: <9ffe023149663965584c3246706b000001638f0d3f53>
	Manufacturer: <e0000167ca356a0d>
    Device (update): 00:0e:0b:00:75:12 (random), -68 dBm (not connectable)
	Flags: <06>
	Incomplete 16b Services: <e0ff>
	Complete Local Name: 'BT Bee-BLE'
	Tx Power: <00>
	16b Service Data: <00b000000000>
	Manufacturer: <484d000e0b007512>
I also tried:

Code: Select all

[email protected]:/ $ hcitool dev
Devices:
	hci0	B8:27:EB:7D:77:30
[email protected]:/ $ hciconfig
hci0:	Type: BR/EDR  Bus: UART
	BD Address: B8:27:EB:7D:77:30  ACL MTU: 1021:8  SCO MTU: 64:1
	UP RUNNING 
	RX bytes:1606 acl:4 sco:0 events:115 errors:0
	TX bytes:3038 acl:4 sco:0 commands:91 errors:0

[email protected]:/ $ hcitool scan
Scanning ...
[email protected]:/ $ sudo hcitool lescan
LE Scan ...
4C:4A:A9:4E:73:C0 (unknown)
00:0E:0B:00:75:12 (unknown)
00:0E:0B:00:75:12 BT Bee-BLE
4C:4A:A9:4E:73:C0 (unknown)
42:6A:9F:B6:72:CD (unknown)
42:6A:9F:B6:72:CD (unknown)
^[email protected]:/ $ gatttool -I -b 00:0E:0B:00:75:12
[00:0E:0B:00:75:12][LE]> primary
Command Failed: Disconnected
[00:0E:0B:00:75:12][LE]> connect
Attempting to connect to 00:0E:0B:00:75:12
[00:0E:0B:00:75:12][LE]> primary
Command Failed: Disconnected
Error: connect error: Connection refused (111)
[00:0E:0B:00:75:12][LE]> connect
Attempting to connect to 00:0E:0B:00:75:12
Error: connect error: Connection refused (111)
[00:0E:0B:00:75:12][LE]> exit
[email protected]:/ $ gatttool -I random -b 00:0E:0B:00:75:12
[00:0E:0B:00:75:12][LE]> connect
Attempting to connect to 00:0E:0B:00:75:12
Error: connect error: Connection refused (111)
[00:0E:0B:00:75:12][LE]> exit
Im also trying to understand how to enable btle.py logs.

Thanks in advance!

User avatar
marciokoko
Posts: 257
Joined: Sat Aug 27, 2016 4:33 pm

Re: Unable to connect to BLE device

Fri May 25, 2018 12:37 am

Ive upgraded bluez to 5.4.8 and I get this in bluetoothctl:
[bluetooth]# devices
Device 00:0E:0B:00:75:12 BT Bee-BLE
Device 42:6A:9F:B6:72:CD 42-6A-9F-B6-72-CD
Device 00:0E:0E:00:75:12 BT Bee-EDR
[bluetooth]# connect 00:0E:0B:00:75:12
Attempting to connect to 00:0E:0B:00:75:12
[CHG] Device 00:0E:0B:00:75:12 Connected: yes
Connection successful
[NEW] Primary Service
/org/bluez/hci0/dev_00_0E_0B_00_75_12/service000c
00001801-0000-1000-8000-00805f9b34fb
Generic Attribute Profile
[NEW] Characteristic
/org/bluez/hci0/dev_00_0E_0B_00_75_12/service000c/char000d
00002a05-0000-1000-8000-00805f9b34fb
Service Changed
[NEW] Descriptor
/org/bluez/hci0/dev_00_0E_0B_00_75_12/service000c/char000d/desc000f
00002902-0000-1000-8000-00805f9b34fb
Client Characteristic Configuration
[NEW] Primary Service
/org/bluez/hci0/dev_00_0E_0B_00_75_12/service0010
0000ffe0-0000-1000-8000-00805f9b34fb
Unknown
[NEW] Characteristic
/org/bluez/hci0/dev_00_0E_0B_00_75_12/service0010/char0011
0000ffe1-0000-1000-8000-00805f9b34fb
Unknown
[NEW] Descriptor
/org/bluez/hci0/dev_00_0E_0B_00_75_12/service0010/char0011/desc0013
00002902-0000-1000-8000-00805f9b34fb
Client Characteristic Configuration
[NEW] Descriptor
/org/bluez/hci0/dev_00_0E_0B_00_75_12/service0010/char0011/desc0014
00002901-0000-1000-8000-00805f9b34fb
Characteristic User Description
[CHG] Device 00:0E:0B:00:75:12 UUIDs: 00001801-0000-1000-8000-00805f9b34fb
[CHG] Device 00:0E:0B:00:75:12 UUIDs: 0000ffe0-0000-1000-8000-00805f9b34fb
[CHG] Device 00:0E:0B:00:75:12 ServicesResolved: yes
I was following a video whereby you can write to a connected device using bluetoothctl but the list-attributes and select-attributes for dbus addresses and the write command are no longer available. But even that would only prove or disprove if i can write to it via bluetoothctl, not via python...

User avatar
marciokoko
Posts: 257
Joined: Sat Aug 27, 2016 4:33 pm

Re: Unable to connect to BLE device

Wed May 30, 2018 1:20 am

Apparently Bluepy wasnt calling discoverServices() which meant the serviceMap[] was not being populated which meant I had no uuid to work with at that point. So I included a call in my python code to force that to happen and how I dont get the error, so it finds the service. This is great but I also dont get anything else.

Now bluetoothctl gives:
[NEW] Primary Service
/org/bluez/hci0/dev_00_0E_0B_00_75_12/service0010
0000ffe0-0000-1000-8000-00805f9b34fb
Unknown
[NEW] Characteristic
/org/bluez/hci0/dev_00_0E_0B_00_75_12/service0010/char0011
0000ffe1-0000-1000-8000-00805f9b34fb
Unknown
So Im back to square 1...or 1.5 or something. My original issue was that I had:
  • 1. Tested writing to the characteristic from rpi3 to a test ble unit and it worked!

    2. Couldnt connect the rpi3 to the new ble unit.

    3. Finally got it to connect (since I get no python error?)

    4. But now the ble unit doesnt respond to the python script.
How do I debug from here? I would need to confirm that:
A. the rpi3 is actually sending the data to the ble unit.

and

B. get some sort of response (even if its an error) from the ble unit.
So I remember Douglas6 pointed to this: viewtopic.php?f=28&t=193923&p=1232385

If I understand correctly I need to use a code similar to this but I want to understand, in my py script I would declare a Class for the delegate and give it 2 methods, an init and a handleNotification. Later in my code I declare that class as the delegate to my peripheral class. This means that whenever the ble sends data to the script, that class will be notified and the handleNotification method will be called. So that method takes self, the data parameter which is whatever the ble device sends back and then a parameter called cHandle, which is the GATT handle which Im guessing for my char its "char0011" but then again I just need to code what to do with the data, so I could just print it like so:

Code: Select all

#!/usr/bin/env python
import bluepy.btle as btle

#Delegate methods
class ReadDelegate(btle.DefaultDelegate):
    def __init__(self, params):
        btle.DefaultDelegate.__init__(self)
        # ... if I had any special parameters to set, but i cant think of any
        
    def handleNotification(self, cHandle, data):
        print(data.decode("utf-8"))

#Create peripheral, connect, services and write...
p = btle.Peripheral("00:0E:0B:00:75:12", "random")
p.withDelegate(ReadDelegate())

services=p.getServices()
for service in services:
   print service

s = p.getServiceByUUID("0000ffe0-0000-1000-8000-00805f9b34fb")
c = s.getCharacteristics()[0]
c.write("e", "utf-8")

#pass while no notifications
while True:
    while p.waitForNotifications(1):
        pass

User avatar
marciokoko
Posts: 257
Joined: Sat Aug 27, 2016 4:33 pm

Re: Unable to connect to BLE device

Wed May 30, 2018 6:00 pm

Ok so I revisited @Douglas6 posts and decided to remove the utf-8 parameter and it works.

Now Im trying to read data from the BLE device and I need help with the process because Im not sure how it works. I know that the ble device im using takes data through its serial port over bt. That is how Im sending it the "e" or the "o" to toggle relays and it works. But it has a command "[" that asks the device for data, specifically the relay states inside an array:

{1: 1, 2: 1, 3: 1, 4: 1, 5: 0, 6: 0, 7: 1}

So Im using this code but I dont get anything back except 2 blank lines. So Im trying to understand, Im writing the "[" to the service characteristic but maybe I should be writing it elsewhere?:

Code: Select all

#!/usr/bin/env python
import bluepy.btle as btle

#Delegate methods
class ReadDelegate(btle.DefaultDelegate):
    def handleNotification(self, cHandle, data):
        print("Here is the data...")
        print(data)

#Create peripheral, connect, services and write...
p = btle.Peripheral("00:0E:0B:00:75:12", "random")
p.withDelegate(ReadDelegate())

services=p.getServices()
for service in services:
   print service

s = p.getServiceByUUID("0000ffe0-0000-1000-8000-00805f9b34fb")
c = s.getCharacteristics()[0]
c.write("[")

#pass while no notifications
while True:
    while p.waitForNotifications(1):
        pass
Here is the data... prints out btw, but it just hangs there

User avatar
marciokoko
Posts: 257
Joined: Sat Aug 27, 2016 4:33 pm

Re: Unable to connect to BLE device

Tue Jun 12, 2018 12:41 am

Ok writing to the characteristic does work, but it also fails. With the rpi2 using the gpio pins it never failed. Looking at the tmux session I see:

Code: Select all

  File "/usr/local/lib/python2.7/dist-packages/bluepy/btle.py", line 410, in _connect
    "Failed to connect to peripheral %s, addr type: %s" % (addr, addrType))
BTLEException: Failed to connect to peripheral 00:0E:0B:00:75:12, addr type: random
which means its not connecting properly for some reason.

I just remembered that the power went out the other day and so I ran bluetoothctl and scan on and the BLE address wasnt showing up. If you recall I have 2 bluetooth macs on that relay board:

00:0E:0B:00:75:12

and

00:0E:0E:00:75:12

where one is classic and/or the other is EDR. The one I have managed to connect to and write to is the 0B one. It wasnt showing up on bluetoothctl scans so I unplugged the relay board and re-plugged it and I was able to connect to it again. I guess this wouldnt happen before because the rpi2 would write out thru the gpio pins!

Return to “Troubleshooting”

Who is online

Users browsing this forum: No registered users and 57 guests