广告

原创 CC2540 微信蓝牙配置

2017-4-18 22:34 818 0 分类: MCU/ 嵌入式

写在前面,最近公司在开发一款蓝牙设备,在这里我就和大家分享一下如何在微信配置蓝牙设备。


参考资料



http://blog.csdn.net/skdev/article/details/50974343

http://blog.csdn.net/sadshen/article/details/47761801

http://blog.csdn.net/feilusia/article/details/48917625



测试APP 微信蓝牙协议调试工具AirSyncDebugger 这里可以下载


1. 首先我们看一下流程


S9){FC7LLRC0M@AP{BOP7E3



  1. BLE设备要发出符合格式的广播包

  2. 微信扫到BLE设备发出的广播包,如果广播包符合微信配置的设备要求,则微信会请求建立与BLE设备的连接,

  3. 连接建立后,通过读Indicate Descriptor确认Indicate Characteristic是否有Indicate属性,

  4. 有的话会往Indicate Descriptor写值,BLE设备收到Deiscriptor写操作后,

  5. 通过Indicate Characteristic往微信发Auth请求,微信通过Write Characteristic回应Auth请求,BLE设备收到Auth回应合,

  6. 接着发Init请求,微信则回应Init请求,

  7. 至此,连接就建立了,后面就可以不分先后往对方发数据了


2.AirSyncDebugger的日志,完整流程


*****************BLE: TwoWinter******************


***** onTestBroadcastRecord *****

result = true, Has 0xfee7 or standard service in broadcast record


广播包:02 01 06 03 02 E7 FE 09 FF 0D 00 90 59 AF 0E 62 88(这几个是mac) 0A 09 54 77 6F 57 69 6E 74 65 72 05 12 50 00 20 03 02 0A 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


***** onTestManufatureData *****

resut= true, 广播包中 manufacture specific data 字段中MAC地址校验成功.



如果这里显示警告,就是的mac地址不对,这里说一下mac的数据是可以自己写的,但是前提要前后保持一致



***** onDiscoverService *****

result = true, DiscoverService success

Discovered Services

ServiceUUID: 00001800-0000-1000-8000-00805f9b34fb

ServiceUUID: 00001801-0000-1000-8000-00805f9b34fb

ServiceUUID: 0000180a-0000-1000-8000-00805f9b34fb

ServiceUUID: 0000fee7-0000-1000-8000-00805f9b34fb


***** onTestHasWeChatService *****

result = true, has WeChatService or standard service


***** onTestHasIndicateCharacteristic *****

result = true, has WeChat Indicate Characteristic


***** onTestHasWriteCharacteristic *****

result = true, has Wechat Write Characteristic


***** onTestHasReadCharacteristic *****

result = true, Has WeChat read characteristic


***** onTestWriteCharacteristicPermisson *****

result = true, has Write permission


***** onTestIndicateCharacteristicPermisson *****

result = true, has Indication permission


***** onTestReadCharacteristicPermisson *****

result = true, Read Characteristic is read able


***** onTestStartIndicating *****

result = true, can Start Indicate


***** onConnected *****

result = true, connected


------onDataReceived------

data length = 20

data dump = FE 01 00 24 27 11 00 01 0A 00 18 84 80 04 20 01 28 02 3A 06

data receive seq = 0


------onDataReceived------

data length = 16

data dump = 90 59 AF 0E 62 88(MAC) 62 08 6D 79 64 65 76 69 63 65

data receive seq = 1


***** onTestRecvAuthReqtWhenStartedIndicating *****

result = true, received auth request pack


***** onTestIsValidAuthReqPack *****

result = true, is a valid auth request pack

AuthRequestPack: FE 01 00 24 27 11 00 01 0A 00 18 84 80 04 20 01 28 02 3A 06 90 59 AF 0E 62 88 62 08 6D 79 64 65 76 69 63 65

has BaseRequest

no Md5DeviceTypeAndDeviceId field!

has MacAddress field, Mac Address = 90 59 AF 0E 62 88

MacAddress BitLength = 48bit

Mac Address from broadcast record = 90:59:AF:0E:62:88

Mac address checkout success

has ProtoVersion field, ProtoVersion = 65540

has AuthProto field, AuthProto = 1

has AuthMethod field, AuthMethod = EAM_macNoEncrypt

no AesSign field!

has DeviceName field, DeviceName = mydevice


**** send auth response ****

data len = 14

data dump = FE 01 00 0E 4E 21 00 01 0A 02 08 00 12 00


**------onDataReceived------

data length = 16

data dump = FE 01 00 10 27 13 00 02 0A 00 1A 04 11 22 33 44

data receive seq = 2


***** onTestRecvInitReqPack *****

result = true, received init request pack


***** onTestIsValidInitReqPack *****

result = true, valid init request pack: has BaseRequest

has Challenge field, Challenge = 11 22 33 44


*****Disconnected Device*****


418971839110117919


3.几个特征值的配置情况


![L@KVR4${L1(8~O0@X{YRAC


Write Characteristic 是微信向BLE设备发数据的通道。

Indicate Characteristic 是BLE设备向微信发数据的通道。

Read Characteristic 返回MAC地址

通信主要由Writet和Indicate 两个特征来完成。

___________________

有几点强调一下



  1. BlE indicate > http://blog.csdn.net/feilusia/article/details/48917625 我当初看这个文档收获很多,感谢原作者,后来者也仔细看看该文件. 当检测到微信发过来的初始化指令的时候,做一个标志位,在定时器里面处理发送,而且要发两次,中间要加点时间间隔,不加的话可能只能发前面一帧数据,不可过大不然会被看成不同的两帧,因为蓝牙协议栈最多支持20个字节的传输,关于AUTH的指令的协议的字节数大于20,所以要发两帧



    ------onDataReceived------

    data length = 20

    data dump = FE 01 00 24 27 11 00 01 0A 00 18 84 80 04 20 01 28 02 3A 06

    data receive seq = 0





------onDataReceived------

data length = 16

data dump = 90 59 AF 0E 62 88(MAC) 62 08 6D 79 64 65 76 69 63 65

data receive seq = 1




  1. 各个标志位的属性配置要做好,这里需要多时间注意一下。



    `

    static gattAttribute_t simpleProfileAttrTbl[SERVAPP_NUM_ATTR_SUPPORTED] =

    {

    // Simple Profile Service

    {

    { ATT_BT_UUID_SIZE, primaryServiceUUID }, /* type /

    GATT_PERMIT_READ, /
    permissions /

    0, /
    handle /

    (uint8
    )&simpleProfileService /* pValue */

    },



    // Characteristic 1 Declaration

    {

    { ATT_BT_UUID_SIZE, characterUUID },

    GATT_PERMIT_READ,

    0,

    &simpleProfileChar1Props

    },


    // Characteristic Value 1

    {

    { ATT_BT_UUID_SIZE, simpleProfilechar1UUID },

    GATT_PERMIT_READ | GATT_PERMIT_WRITE,

    0,

    &simpleProfileChar1

    },


    // Characteristic 1 User Description

    {

    { ATT_BT_UUID_SIZE, charUserDescUUID },

    GATT_PERMIT_READ,

    0,

    simpleProfileChar1UserDesp

    },


    // Characteristic 2 Declaration

    {

    { ATT_BT_UUID_SIZE, characterUUID },

    GATT_PERMIT_READ,

    0,

    &simpleProfileChar2Props

    },


    // Characteristic Value 2

    {

    { ATT_BT_UUID_SIZE, simpleProfilechar2UUID },

    GATT_PERMIT_READ,

    0,

    simpleProfileChar2

    },


    // Characteristic 2 User Description

    {

    { ATT_BT_UUID_SIZE, charUserDescUUID },

    GATT_PERMIT_READ,

    0,

    simpleProfileChar2UserDesp

    },


    // Characteristic 3 Declaration

    {

    { ATT_BT_UUID_SIZE, characterUUID },

    GATT_PERMIT_READ,

    0,

    &simpleProfileChar3Props

    },


    // Characteristic Value 3

    {

    { ATT_BT_UUID_SIZE, simpleProfilechar3UUID },

    GATT_PERMIT_WRITE,

    0,

    simpleProfileChar3

    },


    // Characteristic 3 User Description

    {

    { ATT_BT_UUID_SIZE, charUserDescUUID },

    GATT_PERMIT_READ,

    0,

    simpleProfileChar3UserDesp

    },


    // Characteristic 4 Declaration

    {

    { ATT_BT_UUID_SIZE, characterUUID },

    GATT_PERMIT_READ,

    0,

    &simpleProfileChar4Props

    },


    // Characteristic Value 4

    {

    { ATT_BT_UUID_SIZE, simpleProfilechar4UUID },

    0,

    0,

    simpleProfileChar4

    },


    // Characteristic 4 configuration

    {

    { ATT_BT_UUID_SIZE, clientCharCfgUUID },

    GATT_PERMIT_READ | GATT_PERMIT_WRITE,

    0,

    (uint8 *)simpleProfileChar4Config

    },


    // Characteristic 4 User Description

    {

    { ATT_BT_UUID_SIZE, charUserDescUUID },

    GATT_PERMIT_READ | GATT_PERMIT_WRITE,

    0,

    simpleProfileChar4UserDesp

    },


    // Characteristic 5 Declaration

    {

    { ATT_BT_UUID_SIZE, characterUUID },

    GATT_PERMIT_READ,

    0,

    &simpleProfileChar5Props

    },


    // Characteristic Value 5

    {

    { ATT_BT_UUID_SIZE, simpleProfilechar5UUID },

    GATT_PERMIT_READ,

    0,

    simpleProfileChar5

    },


    // Characteristic 5 User Description

    {

    { ATT_BT_UUID_SIZE, charUserDescUUID },

    GATT_PERMIT_READ,

    0,

    simpleProfileChar5UserDesp

    },


    // Characteristic 6 Declaration

    {

    { ATT_BT_UUID_SIZE, characterUUID },

    GATT_PERMIT_READ,

    0,

    &simpleProfileChar6Props

    },


    // Characteristic Value 6

    {

    { ATT_BT_UUID_SIZE, simpleProfilechar6UUID },

    GATT_PERMIT_READ|GATT_PERMIT_WRITE,

    0,

    simpleProfileChar6

    },


    // Characteristic 6 configuration

    {

    { ATT_BT_UUID_SIZE, clientCharCfgUUID },

    GATT_PERMIT_READ | GATT_PERMIT_WRITE,

    0,

    (uint8 *)simpleProfileChar6Config

    },


    // Characteristic 6 User Description

    {

    { ATT_BT_UUID_SIZE, charUserDescUUID },

    GATT_PERMIT_READ,

    0,

    simpleProfileChar6UserDesp

    },

    };

    `

  2. 特别注意当微信向BLE设备发生init指令的时候,是对于 ‘Write Characteristic’进行操作,通过‘indicate’回复

  3. 有些协议需要回复mac,每个设备的mac都不同

  4. 自由通讯



广告

文章评论 0条评论)

登录后参与讨论
相关推荐阅读
Bruce-Woo 2016-01-05 17:16
DHT21(AM2301) 数字温湿度传感器在stm32F0和stm8s105的移植
     写在之后:排版有些混乱,见谅哈。转载请注明出处。      写在之后的之后: 我再放几张实物图哈哈,由于我之前在STM32F0移植的时候没有放照片,只能用现在用STM8S105的照片...
Bruce-Woo 2015-12-22 00:38
小批量 项目的一些心得与体会
写在最前:转载请注明出处。最近几个月来都在做公司的智能家居,智能锁之类的开发,自己负责技术研发的同时,也负责硬件方面的生产。我在这里也就简单说一下自己的一些收获和体会,也是一种总结吧。关于技术;1,慎...
Bruce-Woo 2015-12-16 22:33
JLX12864G_086 LCD在STM8S105 和STM32的移植
写在最前: 装载请注明出处,谢谢。 写在之前: 写博客的目的是为了记录自己的一些工作生活,其中可能有些由于本人水平有限而说的不对或者不准确,也请各位不吝赐教。之前在STM32F1上的移植没...
Bruce-Woo 2015-12-09 23:52
C51入门程序 串口通信 和多次按键
写在前面:转载请注明出处,如果对您有帮助,请您点个赞。 这是我13年上大学时候上单片机课写的一个题目,是:按一次按,一个灯亮2秒(另外一个不亮),同时发送1到电脑端;同一个键按两次,刚才亮的现在暗,另...
Bruce-Woo 2015-12-09 16:11
C51入门程序 定时器和中断
  写在之前:转载请注明出处,如果给您帮助,请您点个赞。 和上文一样,是我大学13年是上单片机的习题,拿出来看看有没有帮助到需要的人 。与上文要相互联系。     ...
我要评论
0
0
广告