CoreBluetooth:如何设计code许多特性(30 - 40)? [英] CoreBluetooth: How to design code for many characteristics (30 - 40)?

查看:163
本文介绍了CoreBluetooth:如何设计code许多特性(30 - 40)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了一下周围,只是发现这是一个可能重复的问题:

I searched around a bit and just found this as a possible duplicate question:

多CBPeripheral对同一设备

我的问题是:

我的多个服务其中一起有关于 30-40特点(是的,我需要所有的人......)。
为出发点来处理CoreBluetooth我一直使用的苹果样品code(<一个href=\"https://developer.apple.com/library/ios/sample$c$c/TemperatureSensor/Introduction/Intro.html\">CoreBluetooth温度传感器)。

I have multiple services which all together have about 30-40 characteristics (Yes, I need all of them...). As starting point for dealing with CoreBluetooth I always used the Apple Sample Code (CoreBluetooth Temperature Sensor).

发现和服务/特性处理是分为两类,这工作得很好只是几个特点。但是,在一个类中处理这个庞大的特征量是不是我理解下的好的软件设计

Discovery and Service/Characteristic handling is divided into two classes and this works fine for just a few characteristics. But handling this huge amount of characteristics in one class is not what I understand under "good software-design".

这是进入的人想到的第一个想法是创建一个类为每个服务。可惜的 CBPeripheral 的只是能在同一时间有一个的 CBPeripheralDelegate 的。这意味着我不能把它分成几类。

The first idea that come into ones mind is to create one class for every service. But unfortunately a CBPeripheral just can have one CBPeripheralDelegate at the same time. This means I can't divide it up into several classes.

我们不必展开了讨论,如果BLE是获取这个数据量正确的技术 - 它不是但也有使用BLE制造商,使他们不必一起麻烦。小额信贷机构项目... 的)

我也看了最后提供的CoreBluetooth编程指南,但它只是描述基本的工作流程 - 一无所知正确的设计

I also read the finally provided CoreBluetooth Programming Guide but it just describes basic workflows - nothing about the right design.

我在寻找一个不错的设计方法。你可能有任何建议,提示或链接来样code?
事先非常感谢!

I'm looking for a nice design approach. You may have any suggestions, hints or links to sample code? Many thanks in advance!

推荐答案

突围逻辑分成几个自我包含的类始终是不错的设计。你应该根据服务或其他类别势必要组你的code。尽管外围只有一个代表,可以方便地实现在您注册的各种服务实现和选择键(实际的服务对象),并分派调用指定的服务处理器调度模式。如果服务类实现 CPPeripheralDelegate 的协议,那么这样的设计可以让你测试/重复使用的每个服务,如果你需要与在code最小的变化。

Breaking out logic into several self contained classes is always good design. You should definitely try to group your code according to services or other categories. Even though the peripheral has only a single delegate, you can easily implement the dispatcher pattern where you register the various service implementations and selection keys (practically the service objects) and dispatch the call to the designated service handler. If the service classes implement the CPPeripheralDelegate protocol, then this design will allow you to test/reuse each service separately if you need to with minimal changes in the code.

在伪OBJ-C code调度外围委托看起来像如下:

In pseudo obj-c code the dispatcher peripheral delegate would look like as follows:

// The ivar/property serving as the registry
NSMutableDictionary *registeredHandlers = [[NSMutableDictionary alloc] init];

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
  // for each service create an instance of its handler class and
  // add them to the registered handlers
  for (CBService *service : peripheral.services) {
    if (!registeredHandlers[service]) { // don't reinitialize if not needed
      ExtendedCBPeripheralDelegate *serviceHandler = [self instantiateHandlerForService:service];
      [registeredHandlers setObject:serviceHandler forKey:service];
      [serviceHandler discoverCharacteristics]; // make this functionality self contained for the service
    }
  }
}

在服务或特性相关回调调度应当执行。举个例子:

In service or characteristic related callbacks the dispatching should be implemented. An example:

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
  ExtendedCBPeripheralDelegate *serviceHandler = registeredHandlers[service];
  [serviceHandler peripheral:peripheral didDiscoverCharacteristicsForService:service error:error];
}

- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
  ExtendedCBPeripheralDelegate *serviceHandler = registeredHandlers[characteristic.service];
  [serviceHandler peripheral:peripheral didWriteValueForCharacteristic:characteristic error:error];
}

如果中央管理器断电,那么最好的解决办法是放弃整个外围委托。不要用重新初始化打扰,而计划进行处置。当然,如果需要,可以通知即将毁灭的服务处理程序。

If the central manager is powered off, then the best solution is to drop the whole peripheral delegate. Don't bother with reinitialization, rather plan for disposal. Of course, if needed, you can notify the service handlers of the imminent destruction.

这篇关于CoreBluetooth:如何设计code许多特性(30 - 40)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆