当前位置:网站首页>Fast realization of Bluetooth communication between MCU and mobile phone

Fast realization of Bluetooth communication between MCU and mobile phone

2022-06-27 06:24:00 shine_ blink

Big update !

shineblink.com Development free App, Developers don't have to do anything Android or IOS Development , That is, you can have a mobile phone that can and Core Development board communication specialty App

One 、 Realization function

Core Our main chip comes with BLE Bluetooth function , So just Core You can easily realize the mutual communication with mobile phones . The function of this chapter is to establish Bluetooth connection with mobile phone :(1)Core every other 0.2 Send a packet of data to the mobile phone in seconds ,(2) We're debugging on the phone App Customize any packet sent to Core,Core When received, pass through Print() Function print out .

Be careful : because Core Inside comes 2.4G Wireless capabilities , Therefore, the Bluetooth communication function of this chapter can be realized without external devices or modules . But if it's used Ble Bluetooth function ,Core Of 2.4G The single point or multi-point communication function cannot be used , Developers need to pay attention to this .

Mobile debugging App Here we choose to use apples IOS Well known LightBlue, Android mobile phone download and install BleTool Or Android version LightBlue It's fine too .

Two 、 Implementation mechanism

Core Of ble Low power Bluetooth offers two features , One is Write characteristic , One is Notify characteristic . among :
Write characteristic : It can be seen as a mobile phone sending data to Core The passage of , The mobile phone is sent to Core The data is stored in Write In the feature .
Notify characteristic : It can be seen as Core Channel to send data to the phone ,Core The data sent to the mobile phone through Bluetooth is stored in Notify On . But we need to pay attention , The phone wants to receive from Core The data of , You need to enable this on your mobile phone Notify( It will be mentioned in the following demonstration ).
The following is true. Core Of Ble Provided by the service describe , It doesn't matter if you don't understand this paragraph , Just follow the above understanding .

service(UUID: 0x0001) describe : service There are two features (Characteristic):
One for sending data , One to receive data

Rx Characteristic (UUID: 0x0002) Used to receive data from mobile phones Mobile phone Bluetooth can be used through ATT Write Way to Rx
Characteristic send data Tx Characteristic (UUID: 0x0003) To send data to a mobile phone If Bluetooth is Tx
Characteristic Enabled notification, Then the device can send notification To send data to mobile phone Bluetooth

3、 ... and 、 Complete code

-- Configure Low Power Bluetooth to work with default parameters , The name of the device is "MyBle002"
-- The device name is the name shown in the list when the mobile phone scans the Bluetooth device .
LIB_NrfBleDefaultConfig("MyBle002")
-- Start the big cycle 
while(GC(1) == true)
do
    LIB_DelayMs(200)
    send_tab = {
    0,1,2,3,4,5,6,7,8,9}
    -- take send_tab In the array 10 Elements sent to the phone 
    LIB_NrfBleSend(send_tab)
    -- Check whether you receive Bluetooth data from your mobile phone , If you receive it print Print out 
    recv_flag, recv_tab = LIB_NrfBleRecv()
    if recv_flag == 1 then
        -- Print received data 
        for k,v in ipairs(recv_tab) do
            print(k,v)
        end
    end
end

Four 、 demonstration

1. Turn on the iPhone LightBlue Software , And start searching for Bluetooth devices nearby , find "MyBle002" Then click to enter
2. find properties notify characteristic , Click to enter
3. Click on Listen for notification, So that you can receive Core The data is coming in
4. You can see every 0.2 A packet of data will be received in seconds
5. Back to the step 2 The interface of , Click on properties write, And then click write new value
6. Set to send to Core The data is AA BB CC DD EE FF

 Insert picture description here
7. Last in Core Of TF Cary LOG.TXT The file can be seen that we just sent it manually by mobile phone Core Of AA BB CC DD EE FF data
 Insert picture description here
For more details, please refer to shineblink.com The website links

原网站

版权声明
本文为[shine_ blink]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270617053542.html