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

Reading response from BLE device on rpi3

Thu Jun 21, 2018 11:44 pm

I have a bluetooth relay board that when sent the character over serial "[", responds with this array of relay states:

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

Now Im connecting with it via BLE and I can send it commands just fine and it responds fine. But when I send it the "[" character the python script just hangs without a response until I break Ctrl+C with a keyboard interrupt. This is the python code Im using:

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
What am I missing?

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

Re: Reading response from BLE device on rpi3

Fri Jun 22, 2018 9:15 am

A Bluetooth LE "characteristic" represents a short data item which can be read or written. These can be fixed (e.g. a string representing the manufacturer name) or change dynamically (such as the current temperature or state of a button). Most interaction with Bluetooth LE peripherals is done by reading or writing characteristics.
You dont send commands with the write function, you already have direct access to the value. My guess is that you should print the "c"-object first. My guess is that its 1 or 0 based on the state of the first relay. You should then try to write 0 or 1 to change the state of the relay.

BLE already have built in protocol for accessing data on the device, you dont need to send special commands : https://www.bluetooth.com/specification ... s-overview

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

Re: Reading response from BLE device on rpi3

Fri Jun 22, 2018 10:51 pm

So you're saying I should print the c object as in:

Code: Select all

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

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

Re: Reading response from BLE device on rpi3

Fri Jun 22, 2018 11:18 pm

You should probably read the bluepy documentation so you understand what kind of object is stored in "c" and thus learn how to use the "write()" method of that class.

Printing the object might just dump some information about the "characteristic" so you can learn more about it.
I dont really think it will actually do something, it was bad wording on my part.

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

Re: Reading response from BLE device on rpi3

Sun Jun 24, 2018 2:35 am

I just tried printing the c object, its just the characteristic:

Code: Select all

Service <uuid=Generic Attribute handleStart=12 handleEnd=15>
Service <uuid=Generic Access handleStart=1 handleEnd=11>
commonName= 0000ffe0-0000-1000-8000-00805f9b34fb
commonName= ffe0
Service <uuid=ffe0 handleStart=16 handleEnd=65535>
65535
commonName= 0000ffe1-0000-1000-8000-00805f9b34fb
commonName= ffe1
Characteristic <ffe1>
with this code:

Code: Select all

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]
print(c)
In my previous setup (with serial gpio connection hm10 on the rpi2) I would have to send "[" and read the response.

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

Re: Reading response from BLE device on rpi3

Sun Jun 24, 2018 2:45 pm

https://ianharvey.github.io/bluepy-doc/ ... istic.html

Do a "print(c.read())" and see what you get then.. if this is one of the relays you should (in theory) get either the character "1" or "0" or the byte 1 or 0.

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

Re: Reading response from BLE device on rpi3

Mon Jun 25, 2018 1:40 am

I get nothing...just a blank line

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

Re: Reading response from BLE device on rpi3

Mon Jun 25, 2018 9:40 am

This is what I would maybe do..
- Look for some documentation for BLE for you device.
- Since you only look at the first charateristic ( c = s.getCharacteristics()[0] ) maybe loop trough all of them.
- Or : Install a BLE tool on my smart-phone so I could easily look at and explore all the characteristics.
( https://play.google.com/store/apps/deta ... nner&hl=en )

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

Re: Reading response from BLE device on rpi3

Mon Jun 25, 2018 4:13 pm

I am not familiar with the BLE device in question, so please take my comments as, well, comments.

Some BLE devices (in particular HM-10 style serial devices) communicate not by responding to characteristic read requests, but by sending notifications (if you are unfamiliar with these BLE concepts, please do some research on BLE programming.) Perhaps by writing a bracket to the characteristic, the device responds by sending a notification. This theory is supported by the fact that reading the characteristic returns nothing.

I've already given marciokoko example code for reading notifications, and I feel that I have have made these points before. There isn't much more I can do without having the device in hand.

As topguy suggests, exploring the device with a good Android BLE app is an excellent way to learn about the device.

And as I have also pointed out, given marciokoko's unfamiliarity with BLE, I suggest he give up on it. His device seems to have a classic Bluetooth interface as well, which can be used with rfcomm to give a more familiar serial device protocol.

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

Re: Reading response from BLE device on rpi3

Mon Jun 25, 2018 5:20 pm

Here is the link to the board:
https://www.tinyosshop.com/index.php?ro ... uct_id=684

Here is the screenshot of when I read the value:
ble2.jpg
ble2.jpg (32.03 KiB) Viewed 149 times

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

Re: Reading response from BLE device on rpi3

Mon Jun 25, 2018 5:23 pm

@Douglas6

Hadnt seen your post. Yes I did try using notiications and writing the bracket...Thats the OP of this thread. My code is included and the python script just hangs when I run it.

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

Re: Reading response from BLE device on rpi3

Tue Jun 26, 2018 9:17 am

Why exactly do you want to use BLE instead of the serial (rfcomm) profile of standard BT, since you got it working.
The documentation and example apps seems to always uses serial BT. ( cant really be sure about the iphone/android apps )

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

Re: Reading response from BLE device on rpi3

Tue Jun 26, 2018 3:22 pm

Im not for any particular way. Im coming from an rpi2 with gpio connected hm10 module which used serial. When I migrated to rpi3, I asked for help and was suggested to use BLE instead, so I did for writing. Now that I have to read I was suggested to use the delegate to listen for notifications.

So how do you suggest I read from the ble device?

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 27 guests