UNIHIKERGeneral

How to communicate with BLE devices using UNIHIKER?

userHead LL 2024-08-05 23:18:51 1161 Views3 Replies

UNIHIKER has Bluetooth function, how to communicate with BLE devices?

2024-09-03 08:34:02

I'm confused. Does UNIHIKER have Bluetooth? UNIHIKER is widely advertised as having Bluetooth, but the DFRobot instructions here say you need to buy a dongle? Am I missing something?

userHeadPic alanhudson
Lupin wrote:

No … you don't need a separate device! BLE is part of BT 4.0.

2024-09-04 00:18:55
1 Replies
2024-08-05 23:58:53

we will take the DFRobot's BLE-Link Dongle as an example to illustrate how to use UNIHIKER to communicate with BLE devices.

 

https://www.dfrobot.com/product-1220.html

The BLE-Link Dongle is a device that can convert BLE messages into USB serial port messages, making it ideal for BLE communication testing. First, we plug the BLE-Link Dongle into the computer's USB port, and then use the serial assistant software to follow the BLE-Link user manual to configure it as a slave mode, and obtain its MAC address and device name.

 

First, we run the following command to connect to the BLE device with UNIHIKER:

```

gatttool -b 20:91:48:CD:9F:BC -I  


connect  

```

Once "Connection successful" is returned and the blue LED light on the BLE Dongle lights up, it indicates that UNIHIKER has successfully connected with the BLE Dongle.

Once the connection is successful, we can use the characteristics command to view all the features and their attributes of the device.

As we can see, this BLE device has many services. By looking up the BLE-Link's documentation, we know that the service UUID of the BLE-Link Dongle is "0000dfb0-0000-1000-8000-00805f9b34fb", and the communication UUID is "0000dfb1-0000-1000-8000-00805f9b34fb". You can also use BLE software to query the service UUID and communication UUID of the module, or try them one by one.

 

Next, we use the char-write-cmd command to send "hello" to the channel with the ID 0x0025. We can see that "hello" is received on the BLE link end. When we send "abc" from the BLE link end, we also receive the message in the terminal. This indicates that the bidirectional communication is successful.

 

After the test is completed, we can use the  ```exit``` command to exit gatttool.

 

---------

Next, we unplug and re-plug the BLE-Link Dongle to disconnect, and then use the python code for data sending and receiving. The sample code is as follows:

```

import time

from bluepy.btle import Peripheral, UUID, DefaultDelegate


 

class MyDelegate(DefaultDelegate):

    def __init__(self):

        DefaultDelegate.__init__(self)


 

    def handleNotification(self, cHandle, data):

        print('Received data:', data)


 

p = Peripheral("20:91:48:CD:9F:BC""public")


 

try:

    p.setDelegate(MyDelegate())

    svc = p.getServiceByUUID(UUID("0000dfb0-0000-1000-8000-00805f9b34fb"))

    char = svc.getCharacteristics(UUID("0000dfb1-0000-1000-8000-00805f9b34fb"))[0]

    

    while True:


 

        char.write(bytes("hello\n"'utf-8'))

        time.sleep(0.2)


 

        if p.waitForNotifications(0.1):

            # handleNotification() was called

            continue


 

        print("Waiting...")


 

finally:

    p.disconnect()


 

```

 

userHeadPic LL