2113 views|0 replies

3836

Posts

19

Resources
The OP
 

CC2541 Mini Development Kit Project Application Review + WeChat Applet Control cc2541 by Wulunda [Copy link]

As mobile phones become more and more popular, the Internet of Things becomes more and more popular, and WeChat applets become more and more popular, whether you are a college student or an engineer, you should learn WeChat applet Internet of Things development to keep yourself at the forefront of the times.

Advantages of WeChat Mini Programs
1. It is really convenient for users to use. You can open it when you need it and close it when you don’t need it. You can use it and go. This is better than APPs that need to be downloaded and take up space in the phone’s memory.
2. The main style codes are encapsulated in WeChat Mini Programs, so the opening speed is faster than ordinary H5 and close to native APP.
3. You can call more mobile phone system functions than H5 for development, such as GPS positioning, recording, video shooting, gravity sensing, etc., and you can develop richer usage scenarios.
4. On Android phones, you can add it to the mobile phone desktop, which looks similar to the native APP, but it is limited to Android phones, not iPhones.
5. The running speed is similar to APP, and it can also do many functions that H5 cannot do. The development cost is similar to H5, and the development cost is relatively lower than APP.

After learning WeChat Mini Programs, my classmates will learn how to develop Alipay Mini Programs, Quick Apps, and other mini programs, which can run on both IOS and Android phones. Mini Programs will definitely be an indispensable part of the future Internet of Things.

1. Create a new mini program project

1. I won’t say much about how to download the mini program development tool. The download address is developers.weixin.qq.com/.../download.html

2. After the installation is complete, open the mini program development tool and scan the QR code to log in

3. Create a new project. You need to fill in the project name and directory of the mini program. It is recommended to use a test number for AppID first. Click Create

Fourth, create a new lanyatest directory and lanyatest page, and move this page to the top in app.json

查看详情

5. Initialize the Bluetooth adapter (wx.openBluetoothAdapter)

Note
Other Bluetooth-related APIs must be used after calling wx.openBluetoothAdapter. Otherwise, the API will return an error (errCode=10000).
If the user's Bluetooth switch is not turned on or the phone does not support the Bluetooth function, calling wx.openBluetoothAdapter will return an error (errCode=10001), indicating that the phone's Bluetooth function is unavailable. At this point, the mini-program Bluetooth module has been initialized, and the changes in the phone's Bluetooth status can be monitored through wx.onBluetoothAdapterStateChange, and all APIs of the Bluetooth module can also be called.

1

2

3

4

5

wx.openBluetoothAdapter({

success (res) {

console.log(res)

}

})

6. Get Bluetooth Adapter State (getBluetoothAdapterState)

Usually, if the Bluetooth adapter is successfully initialized in the previous step, there will be no problem in this step. There are two parameters in res, discovering indicates whether the device is being searched, and available indicates whether the Bluetooth adapter is available.

1

2

3

4

5

wx.getBluetoothAdapterState({

success (res) {

console.log(res)

}

})

7. Search for Bluetooth devices (startBluetoothDevicesDiscovery)

Start searching for nearby Bluetooth peripherals. This operation consumes system resources. Please call wx.stopBluetoothDevicesDiscovery method to stop searching after searching and connecting to the device.

1

2

3

4

5

6

7

// 以微信硬件平台的蓝牙智能灯为例,主服务的 UUID 是 FEE7。传入这个参数,只搜索主服务 UUID 为 FEE7 的设备

wx.startBluetoothDevicesDiscovery({

services: ['FEE7'],

success (res) {

console.log(res)

}

})

8. Get Bluetooth Device List (getBluetoothDevices)

Get all discovered Bluetooth devices during the Bluetooth module is in effect, including devices that are already connected to the device.

The device list obtained by this interface is all the Bluetooth devices searched during the Bluetooth module is effective. If wx.closeBluetoothAdapter is not called in time to release resources after the Bluetooth module usage process ends, calling this interface will return the Bluetooth devices searched in the previous Bluetooth usage process. The device may no longer be with the user and cannot be connected.
When a Bluetooth device is searched, the name field returned by the system is generally the device name in the LocalName field in the broadcast packet. If a connection is established with a Bluetooth device, the name field returned by the system will be changed to the GattName obtained from the Bluetooth device. If you need to dynamically change the device name and display it, it is recommended to use the localName field.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

// ArrayBuffer转16进度字符串示例

function ab2hex(buffer) {

var hexArr = Array.prototype.map.call(

new Uint8Array(buffer),

function(bit) {

return ('00' + bit.toString(16)).slice(-2)

}

)

return hexArr.join('');

}

wx.getBluetoothDevices({

success: function (res) {

console.log(res)

if (res.devices[0]) {

console.log(ab2hex(res.devices[0].advertisData))

}

}

})

9. Connect to discovered Bluetooth devices (createBLEConnection)

Connect to Bluetooth Low Energy devices.

If the applet has previously searched for a Bluetooth device and successfully established a connection, you can directly pass in the deviceId obtained from the previous search to try to connect to the device without performing a search operation.

Note
Please make sure to call createBLEConnection and closeBLEConnection interfaces in pairs. If Android calls createBLEConnection multiple times to create a connection, it may cause the system to hold multiple instances of the same device, resulting in the inability to truly disconnect the device when calling closeBLEConnection. The
Bluetooth connection may be disconnected at any time. It is recommended to monitor the wx.onBLEConnectionStateChange callback event and perform a reconnection operation on demand when the Bluetooth device is disconnected.
If the interface for data read and write operations is called for an unconnected device or a disconnected device, a 10006 error will be returned. It is recommended to reconnect.

1

2

3

4

5

6

7

wx.createBLEConnection({

// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接

deviceId,

success (res) {

console.log(res)

}

})

10. Stop searching for Bluetooth devices (stopBluetoothDevicesDiscovery)

Stop searching for nearby Bluetooth peripherals. If the required Bluetooth device has been found and there is no need to continue searching, it is recommended to call this interface to stop Bluetooth search.

1

2

3

4

5

wx.stopBluetoothDevicesDiscovery({

success (res) {

console.log(res)

}

})

11. Get all services of Bluetooth devices (getBLEDeviceServices)

Get all services of the Bluetooth device.

1

2

3

4

5

6

7

wx.getBLEDeviceServices({

// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接

deviceId,

success (res) {

console.log('device services:', res.services)

}

})

12. Get all characteristic values (getBLEDeviceCharacteristics)

Get all characteristic values in a service of a Bluetooth device.

1

2

3

4

5

6

7

8

9

wx.getBLEDeviceCharacteristics({

// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接

deviceId,

// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取

serviceId,

success (res) {

console.log('device getBLEDeviceCharacteristics:', res.characteristics)

}

})

13. Enable characteristic value changes (notifyBLECharacteristicValueChange)

Enable the notify function when the characteristic value of the low-power Bluetooth device changes, and subscribe to the characteristic value. Note: The characteristic value of the device must support notify or indicate to be successfully called.

In addition, you must enable notifyBLECharacteristicValueChange before you can listen to the device characteristicValueChange event.

1

2

3

4

5

6

7

8

9

10

11

12

wx.notifyBLECharacteristicValueChange({

state: true, // 启用 notify 功能

// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接

deviceId,

// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取

serviceId,

// 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取

characteristicId,

success (res) {

console.log('notifyBLECharacteristicValueChange success', res.errMsg)

}

})

14. Receive Bluetooth return messages (onBLECharacteristicValueChange) to monitor the characteristic value change events of low-power Bluetooth devices. notifyBLECharacteristicValueChangeThe interface must be enabled before receiving notifications pushed by the device

15. Send information to Bluetooth (writeBLECharacteristicValue)

Write binary data to the characteristic value of the low-power Bluetooth device. Note: The characteristic value of the device must support write in order to be successfully called.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

// 向蓝牙设备发送一个0x00的16进制数据

let buffer = new ArrayBuffer(1)

let dataView = new DataView(buffer)

dataView.setUint8(0, 0)

wx.writeBLECharacteristicValue({

// 这里的 deviceId 需要在 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取

deviceId,

// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取

serviceId,

// 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取

characteristicId,

// 这里的value是ArrayBuffer类型

value: buffer,

success (res) {

console.log('writeBLECharacteristicValue success', res.errMsg)

}

})

16. Disconnect Bluetooth device connection (closeBLEConnection)

Disconnect from a Bluetooth Low Energy device.

1

2

3

4

5

6

wx.closeBLEConnection({

deviceId,

success (res) {

console.log(res)

}

})

Experience of using cc2541 mini kit:

Compared with the Bluetooth serial port module used before, using cc2541 directly is much cheaper, but it is quite difficult to get started. It took me more than half a month to get the hang of it.

Before you start learning, I recommend you to read the book "Bluetooth 4.0 BLE Development Complete Manual---Practical Internet of Things Development Technology". This book starts from the basics and is indeed very useful for beginners. It is useless for beginners to browse the forum for a long time, because you have no basics and can't even understand what people in the forum are discussing. Only by reading this book can you get started with osal development quickly.

Genuine book link: http://product.dangdang.com/23237630.html

This post is from Wireless Connectivity
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list