I am able to get the MAC address of the device (Nonin Onyx II) through the following code and also it's printed in the device itself.
import bluetooth
from bluetooth.ble import DiscoveryService
def scan():
print("Scanning for bluetooth devices:")
devices = bluetooth.discover_devices(duration=40, lookup_names = True, lookup_class = True)
number_of_devices = len(devices)
print(number_of_devices,"devices found")
for addr, name, device_class in devices:
print("\n")
print("Device:")
print("Device Name: %s" % (name))
print("Device MAC Address: %s" % (addr))
print("Device Class: %s" % (device_class))
print("\n")
services = bluetooth.find_service(address=addr)
if len(services) <=0:
print("zero services found on", addr)
else:
for serv in services:
print(serv['name'])
print("\n")
return
scan();
When I insert the index finger in the device and run the above code, it prints the MAC address. Now, I'm trying to connect with the device to receive data from it. I'm using the following code:
import binascii
import struct
import time
from bluepy.bluepy.btle import UUID, Peripheral
service_uuid = UUID(0x1822)
p = Peripheral("00:1C:05:01:A1:2E", "random")
try:
ch = p.getCharacteristics(uuid=service_uuid)[0]
if (ch.supportsRead()):
while 1:
val = binascii.b2a_hex(ch.read())
val = binascii.unhexlify(val)
val = struct.unpack('f', val)[0]
print (val)
time.sleep(1)
finally:
p.disconnect()
After running the above code, I get the following:
Traceback (most recent call last):
File "/home/sabhya/extractdata.py", line 8, in <module>
p = Peripheral("00:1C:05:01:A1:2E", "random")
File "/home/sabhya/bluepy/bluepy/btle.py", line 190, in __init__
self.connect(deviceAddr, addrType)
File "/home/sabhya/bluepy/bluepy/btle.py", line 310, in connect
raise BTLEException(BTLEException.DISCONNECTED,
bluepy.bluepy.btle.BTLEException: Failed to connect to peripheral 00:1C:05:01:A1:2E, addr type: random
Do I need to use device specific API for it? Are there any good library of python to communicate with BLE devices (e-health)?
Thank you.