How to communicate with BLE devices using UNIHIKER?
![userHead](https://dfimg.dfrobot.com/dfPortrait2.png)
UNIHIKER has Bluetooth function, how to communicate with BLE devices?
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](https://dfimg.dfrobot.com/dfPortrait2.png)
No … you don't need a separate device! BLE is part of BT 4.0.
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
![](https://dfimg.dfrobot.com/64228321aa9508d63a42c28b/enshopforum/36129f8587b6db67fafff4384819ff74.png)
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.
![](https://dfimg.dfrobot.com/64228321aa9508d63a42c28b/enshopforum/b3275a4a0883ba9891439bc2033ca661.png)
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.
![](https://dfimg.dfrobot.com/64228321aa9508d63a42c28b/enshopforum/ed200f702f463dff8a78085173d8f881.png)
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.
![](https://dfimg.dfrobot.com/64228321aa9508d63a42c28b/enshopforum/b12335467b9a2ead94e90f9d183262ef.png)
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.
![](https://dfimg.dfrobot.com/64228321aa9508d63a42c28b/enshopforum/b1abb0c2c1b74f4699dfaf5eef92e802.png)
![](https://dfimg.dfrobot.com/64228321aa9508d63a42c28b/enshopforum/44c82ac431f2b3c06e939697090d5f14.png)
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()
```
![](https://dfimg.dfrobot.com/64228321aa9508d63a42c28b/enshopforum/300dcd7ec3cdd3a1e6ba5e85df2b7c99.png)
![](https://dfimg.dfrobot.com/64228321aa9508d63a42c28b/enshopforum/4530c01b68213c747c0ebe8f74e8c5f3.png)
![userHeadPic](https://dfimg.dfrobot.com/dfPortrait2.png)