【CSRMesh蓝牙开发】CSR开发--MeshService.class使用

MeshService.class为CSR开发(Android端)中极其重要的一个Api库。基于MeshService才使得手机能够控制蓝牙模块组成的网络。我们做的其它开发,都是基于该API接口进行的。
首先,上英文版原文:

This Service provides an interface to the CSRmesh stack.
Once bound to this Service, three methods must be called to initialise the stack and connect to a CSRmesh bridge device: setHandler(Handler) sets the Handler that should receive messages about events from the CSRmesh network. The method setLeScanCallback(LeScanCallback) specifies the callback function that should receive Bluetooth LE adverts. This call back function must call processMeshAdvert(byte[] scanRecord, int rssi) for every advert it receives. This will allow the mesh stack to consume any adverts that are mesh packets. If the advert is consumed, this function will return true, otherwise the advert is not a mesh advert and can be processed within the application. An application can call setContinuousScanEnabled(boolean) to request that scanning is always enabled, otherwise the library will only enabled it when needed to receive a CSRmesh packet. This function should also be called before receiving data using data model. Finally connectBridge(bridgeDevice) is used to connect to a bridge device that will relay messages to be sent to mesh devices.

When connection is complete with the bridge device the Handler is sent the message MESSAGE_LE_CONNECTED. At this point it is possible to communicate with the CSRmesh network.
All control messages sent to devices are encrypted using the network key. The network key is generated from a pass phrase that can be set using the method setNetworkPassPhrase
In order for CSRmesh devices to be controlled by your app they must be associated. Association is a secure way of sharing the network key with a device. Devices that wish to be associated will advertise their UUID. To discover devices that are advertising to be associated use the setDeviceDiscoveryFilterEnabled method. After discovery is enabled, your Handler will receive MESSAGE_DEVICE_DISCOVERED for each device that wishes to be associated (see documentation for setDeviceDiscoveryFilterEnabled for details of the message received).
To start association for a discovered device, use the associateDevice method. Once association completes your Handler will receive MESSAGE_DEVICE_ASSOCIATED for each associated device (see documentation for associateDevice for details of the message received). The associateDevice method accepts a 31-bit device hash and an authorisation code. The authorisation code may be zero if MITM protection is not required. Functions are provided to get device hash from UUID and short code: getDeviceHashFromUUID(UUID) and getDeviceHashFromShortCode.
Once devices are associated it is possible to address them individually via their device id. This is a number in the range 0x8001 - 0x8FFF. Messages can also be sent to groups of devices using the id for a group in place of the device id. This is a number in the range 0x0000 to 0x7FFFF. The id 0x0000 is special in that all devices will respond to it. Messages can be sent to devices using the model classes. Refer to the documentation of those classes for details.
Method calls that will receive an asynchronous response from a CSRmesh device will return a “request id”. This request id is included in the response or the timeout message so that requests can be matched with responses. It is possible that there will be a time out before a response is received to a message sent to the device. This is indicated by sending the registered Handler MESSAGE_TIMEOUT with data EXTRA_MESH_REQUEST_ID and EXTRA_EXPECTED_MESSAGE indicating the message that would have been received if there hadn’t been a time out. E.g. if association fails this will be set to MESSAGE_DEVICE_ASSOCIATED. Also the data handler will contain EXTRA_MESH_REQUEST_ID indicating the unique id that identify the timeout request and EXTRA_DEVICE_ID or EXTRA_UUIDHASH_31 (if is an association request) specifying the device id or uuidhash of the request.

再来英文翻译:

一旦绑定这个Service,就会调用三个方法,来初始化栈和连接CSRmesh桥接设备:
setHandler(Handler) : 设置Handler,接受来自CSRmesh网络的事件信号。
setLeScanCallback(LeScanCallback) : 指定接受蓝牙LE广播信号的回调函数。且该回调函数必须每次接受到广播后,都对广播信号调用processMeshAdvert(byte[] scanRecord, int rssi )函数。这就允许了网络栈(mesh stack)对每个网络产生的广播都有处理。如果广播信号被处理,这个函数返回为真,否则,这个广播信号不是网络广播,并可以在应用中被处理掉。
任何一个application都可以使用 setContinuousScanEnabled(boolean) 实现一直允许扫描,否则,该函数只会在需要接受CSRmesh包的时候才被使能。这个函数应该在接受数据使用数据模式之前调用。
connectBridge(bridgeDevice) 用来连接桥接设备,传递信息。
当与桥接设备连接完成后,Handler 会发送 MESSAGE_LE_CONNECTED 信号。基于此,与CSRmesh网络通信才有可能。
所有发送给设备的信息都会使用网络密钥进行编码。网络密钥由 setNetworkPassPhrase() 函数产生。
为了将CSRmesh设备被你的APP控制,他们必须要连接起来。连接状态是一种安全的与设备分享密钥的方法。需要被连接起来的设备会广播他们的UUID。使用 setDeviceDiscoveryFilterEnabled() 方法发现那些将被连接起来正在广播的设备。当能被发现的时候,你的 Handler 将会接受到 MESSAGE_DEVICE_DISCOVERED ,每一次发现都会被发出这个信号。
使用 associateDevice() 方法为每一个发现的设备开始连接。每一次连接完成,你的 Handler 都会接受到 MESSAGE_DEVICE_ASSOCIATED 信号。associateDevice()方法接受一个31比特位的设备哈希码和授权码。授权码可以为0,如果MITM(信息技术管理)保护没有提及。可以使用 getDeviceHashFromUUID(UUID) 和 getDeviceHashFromShortCode()获取得到设备哈希码。
一旦设备被连接上,便可以通过 device ID 独立连接他们。DeviceID是一个 0x8001~0x8FFF之间的数。信息当然也可以通过分组号来传送给设备分组。设备分组号是0x0000~0x7FFF之间的一个数。ID号0x0000是一个特殊的ID,每个设备都会接受到它。信息可以使用model类发送至设备。
收到来自CSRmesh设备的异步回复的回调方法,将会返回一个”request id”。request id 被包含在回复或者超时信息中,这样就能将请求与答复相匹配起来了。在回复信息收到之前,超时函数有可能已经被发出去了。这是因为发送了携带 EXTRA_MESH_REQUEST_ID 和 EXTRA_EXPECTED_MESSAGE 数据的已注册的 Handler MESSAGE_TIMEOUT 的缘故。。。。如果连接失败,将会发送 MESSAGE_DEVICE_ASSOCIATED。当然数据handler包含EXTRA_MESH_REQUEST_ID,用来显示独一无二的ID,标识超时请求,EXTRA_DEVICE_ID 或者 EXTRA_UUIDHASH_31

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 29
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 29
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值