iOS-Performing Common Central Role Tasks(API Refrence) of the third part

7 篇文章 0 订阅

Performing Common Central Role Tasks


设备实现central角色,在蓝牙低能量通信执行大量常见的任务,例如,发现和连接一个可获得peripherals,和探索,和peripherals提供的数据交互。设备实现peripheral角色,也执行大量常见的任务,不同的任务是,例如,从连接的central发布和广播services和响应读取,写入和订阅请求。

在这个章节,你将学习怎么使用Core Bluetooth framework执行更多常见的蓝牙低能量任务来自central side。下面的代码例子将帮助你在开发你的app实现central 角色在你的本地设备。具体来说,你将如何学习:

  • 开启一个central 管理者对象
  • 发现和连接广播中的peripheral设备
  • 你连接它以后在peripheral设备中探索数据
  • 发送读取和写入请求到一个peripheral的service的一个characteristic的值。
  • 当它更新的时候订阅一个characteristic的值去notified

在下一个章节,你将学习怎样开发你的app实现peripheral角色在你本地设备。

这个章节的例子你会发现是简单和抽象的。你大概需要适当的更改才能纳入你真实的app中。更高级的实现central 角色的相关主题–包括提示,技巧,和最佳的练习在之后的章节。


Starting Up a Central Manager


因为CBCentralManager对象是一个本地central设备 Core Bluetooth面向对象的代表,你分配和初始化一个central管理者对象之后,你可以执行任何蓝牙低能量交易。你初始化你的central管理者通过调用它initWithDelegate:queue:options:

myCentralManager =
        [[CBCentralManager alloc] initWithDelegate:self queue:nil options:nil];

在这个例子中,self是作为一个delegate去接收任何central角色的事件。特别的dispatch queue 是nil,central管理者dispatches central角色事件使用main queue.

当你创建一个central 管理者。central管理者调用delegate中的centralManagerDidUpdateState:方法。你必须实现这个delegate方法去确保是支持蓝牙低能量和在central设备中是可获得的。关于更多怎么实现delegate方法的信息,看CBCentralManagerDelegate Protocol Reference.


Discovering Peripheral Devices That Are Advertising


一旦初始化,central 管理者第一个任务是发现peripheral,在Centrals Discover and Connect to Peripherals That Are Advertising提到.peripherals通过广播存在,你的app搜素附近的peripheral 设备通过调用central 管理者的:
scanForPeripheralsWithServices:options:方法

 [myCentralManager scanForPeripheralsWithServices:nil options:nil];
 Note:  如果你指定第一个参数为nil,这个central管理者返回所有的发现的peripherals, 不管他们是否支持services,在一个正式的app,你通常指定一个CBUUID对象数组,每一个对象代表广播中的peripheral的service的|全局唯一标识符,当你指定一个service UUIDs数组,central 管理者仅仅广播那些service的peripherals,只允许你扫描你感兴趣的peripherals。
  UUIDs和CBUUID对象代表他们,更多细节讨论在Services and Characteristics Are Identified by UUIDs.

每个时间central管理者发现一个peripheral,它调用它的代理对象的方法
centralManager:didDiscoverPeripheral:advertisementData:RSSI:
新发现的peripheral作为一个CBPeripheral对象返回。如果你计划连接这个发现的peripheral,保持一个强引用给它,因此系统不会释放它。下面的例子演示一个类属性维护一个引用到发现的peripheral的场景:

- (void)centralManager:(CBCentralManager *)central
 didDiscoverPeripheral:(CBPeripheral *)peripheral
     advertisementData:(NSDictionary *)advertisementData
                  RSSI:(NSNumber *)RSSI {
 
    NSLog(@"Discovered %@", peripheral.name);
    self.discoveredPeripheral = peripheral;
    ...

如果你希望连接多个设备,你大概使用NSArray发现设备,在任何情况下,一旦你发现你想连接的设备,停止扫描为设备省电。

 [myCentralManager stopScan];


Connecting to a Peripheral Device After You’ve Discovered It


你发现你感兴趣的peripheral设备广播services之后,你请求一个连接到periperhal通过调用central 管理者的 connectPeripheral:options:方法,你想连接发现的peripheral:

 [myCentralManager connectPeripheral:peripheral options:nil];

如果这个连接请求是成功的,central 管理者的delegate对象调用 centralManager:didConnectPeripheral:方法,你开始和这个peripheral交互之后,你设置它的代理确保delegate接收到合适的回调:

- (void)centralManager:(CBCentralManager *)central
  didConnectPeripheral:(CBPeripheral *)peripheral {
 
    NSLog(@"Peripheral connected");
    peripheral.delegate = self;
    ...


Discovering the Services of a Peripheral That You’re Connected To


建立了一个连接到peripherlal之后,你可以探索它的数据,第一步在探索一个periperal提供发现可获得的services,因为能广播的peripheral的数据有大小限制,你大概发现一个peripheral的services比它的广播更多(在它的广播包)。你能发现一个peripheral提供的所有的service,通过调用periperal的 discoverServices:方法,像这样:

 [peripheral discoverServices:nil];
Note: 在一个正式的app,你通常不通过nil作为参数,因为这样做返回所有的可用的peripheral 设备的所有services,因为一个peripheral中的services可能比你感兴趣的多,发现所有的 servic会减短电池寿命和一种不必要的使用时间。你通常指定你在发现中感兴趣的你已经知道的UUID去发现service,明智的探索peripheral的数据显示。

当指定services是发现的时候,periperhal(CBPeripheral对象)调用它的代理方法的
peripheral:didDiscoverServices:方法。Core Bluetooth创建一个数组为在periperal中的每一个services(CBService对象)。以下演示,你能实现这个delegate方法去获得这个发现services的数组:

- (void)peripheral:(CBPeripheral *)peripheral
didDiscoverServices:(NSError *)error {
 
    for (CBService *service in peripheral.services) {
        NSLog(@"Discovered service %@", service);
        ...
    }
    ...


Discovering the Characteristics of a Service


当你发现你感兴趣的服务,下一步在探索service的characterisits。发现service的chatacteristics简单的调用periperal的 discoverCharacteristics:forService:。指明适当的service,像这样:

 NSLog(@"Discovering characteristics for service %@", interestingService);
    [peripheral discoverCharacteristics:nil forService:interestingService];
Note:在一个真实的app,你通常不会使用Nil作为第一个参数,因为这样做会返回peripheral的service的所有的characteristics。因为一个peripheral的service大概包含的characteristics比你感兴趣的要多。发现所有的会减短电池寿命和一些不必要的浪费。反而,你指定一个你感兴趣的你已经知道的UUIDs。

当指定发现的service的chatacteristic的时候,peripheral调用它的代理方法
peripheral:didDiscoverCharacteristicsForService:error: 。Core Bluetooth为每一个发现的characteristic创建一个CBCharacteristic对象数组。下面的例子演示你怎样实现delegate和简单的打印每个发现的characteristic:

- (void)peripheral:(CBPeripheral *)peripheral
didDiscoverCharacteristicsForService:(CBService *)service
             error:(NSError *)error {
 
    for (CBCharacteristic *characteristic in service.characteristics) {
        NSLog(@"Discovered characteristic %@", characteristic);
        ...
    }
    ...


Retrieving the Value of a Characteristic


characteristic包含一个单一的值代表peripheral的service的信息。例如:在一个健康温度service的温度测量characteristic可能有一个值表明摄氏温度。您可以直接读取或者通过订阅它检索characteristic的值。


Reading the Value of a Characteristic


你发现你感兴趣的service的chatacteristic之后,你可以读取characteristic的值通过调用peripheral的
readValueForCharacteristic:方法,指定一个合适的characteristic,像这样:

 NSLog(@"Reading value for characteristic %@", interestingCharacteristic);
    [peripheral readValueForCharacteristic:interestingCharacteristic];

当你尝试读取characteristic的值的时候,peripheral调用它的delegate方法
peripheral:didUpdateValueForCharacteristic:error:检索这个值。如果这个值成功的检索,你能访问它通过characteristic的值属性。像这样:

- (void)peripheral:(CBPeripheral *)peripheral
didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic
             error:(NSError *)error {
 
    NSData *data = characteristic.value;
    // parse the data as needed
    ...
Note:不是所有的characteristics都是可以读取的,你可以确保一个characteristic是否可读通过检测它的属性properties 是否包含
CBCharacteristicPropertyRead常量。如果你尝试读取一个不能读取的characteristic的值 peripheral:didUpdateValueForCharacteristic:error:代理方法返回一个合适的错误

Subscribing to a Characteristic’s Value


通过读取characteristic的值使用readValueForCharacteristic:方法是有效的为静态值。这不是最有效的方法来检索一个动态值。检索characterisic的值变化——例如,你的心率订阅。当你订阅一个characteristic的值,当这个值改变的时候你可以从peripheral接收不同的通知。

你订阅你感兴趣的characteristic的值通过调用peripheral的方法。指明第一个参数为YES,像这样:

 [peripheral setNotifyValue:YES forCharacteristic:interestingCharacteristic];

当你订阅或者不订阅一个characteristic的值,peripheral调用它的代理对象方法:
peripheral:didUpdateNotificationStateForCharacteristic:error:如果订阅失败,你能实现这个代理方法获取错误的原因,例如下面的例子演示:

- (void)peripheral:(CBPeripheral *)peripheral
didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic
             error:(NSError *)error {
 
    if (error) {
        NSLog(@"Error changing notification state: %@",
           [error localizedDescription]);
    }
    ...

Note:不是所有的characteristics提供订阅,你能确保它是否提供订阅通过检查它的属性properties 是否包括 CBCharacteristicPropertyNotify或者CBCharacteristicPropertyIndicate常量

你成功订阅characteristics的characteristic值后,当这个值改变的时候peripheral设备唤醒你的app。每次改变。peripheral调用它的代理对象方法:peripheral:didUpdateValueForCharacteristic:error:.检索这个更新的值,你可以实现这个相同的方法在上面的讨论
Reading the Value of a Characteristic.


Writing the Value of a Characteristic


有时候写入characteristic是有意义的。例如:如果你的app与一个蓝牙低能量数码恒温器交互,你可能想要提供温度值设置这个房间的温度。如果一个characteristic的值是可写的,你能写入它的值作为数据(NSData实例)通过调用peripheral的writeValue:forCharacteristic:type:,像这样

NSLog(@"Writing value for characteristic %@", interestingCharacteristic);
    [peripheral writeValue:dataToWrite forCharacteristic:interestingCharacteristic
        type:CBCharacteristicWriteWithResponse];

当你写入characteristic的值的时候,你指定一个想要执行的写入类型,在上面这个例子,写入类型是:CBCharacteristicWriteWithResponse ,让你的app知道是否可以写入或者不可以成功通过调用:peripheral:didWriteValueForCharacteristic:error: 。如果你实现这个代理方法处理这个额外的错误,以下例子演示:

- (void)peripheral:(CBPeripheral *)peripheral
didWriteValueForCharacteristic:(CBCharacteristic *)characteristic
             error:(NSError *)error {
 
    if (error) {
        NSLog(@"Error writing characteristic value: %@",
            [error localizedDescription]);
    }
    ...

如果你指明写入类型为CBCharacteristicWriteWithoutResponse,执行写入的操作最优,和交付既不保证也不报道。peripheral不调用任何delegate方法。关于支持蓝牙核心框架写类型的更多信息,看CBCharacteristicWriteType enumeration in CBPeripheral Class Reference.

Note:Characteristics可能支持某些类型的写入,或者不支持所有。你确保你的写入类型,如果需要支持任何characteristic,使用CBCharacteristicPropertyWriteWithoutResponse 或者 CBCharacteristicPropertyWrite 常量

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
The Cortex-M0 processor does not have a hardware divider, which means that division calculations are performed using software routines. There are various algorithms for performing software division, but one commonly used method is called "long division". In long division, the divisor is repeatedly subtracted from the dividend until the remainder is less than the divisor. The number of times the divisor is subtracted is the quotient, and the remainder is the final result. This process is repeated until all digits of the dividend have been processed. Here is a sample code for performing integer division on Cortex-M0 using long division: ``` int divide(int dividend, int divisor) { int quotient = 0, remainder = 0; int sign = ((dividend < 0) ^ (divisor < 0)) ? -1 : 1; // convert both operands to positive if (dividend < 0) dividend = -dividend; if (divisor < 0) divisor = -divisor; // perform long division for (int i = 31; i >= 0; i--) { remainder <<= 1; // left shift remainder remainder |= (dividend >> i) & 1; // add next bit from dividend to remainder if (remainder >= divisor) { remainder -= divisor; quotient |= (1 << i); // set corresponding bit in quotient } } // apply sign quotient = sign * quotient; return quotient; } ``` Note that this code assumes that both the dividend and divisor are 32-bit integers. It also handles negative operands correctly and applies the correct sign to the result. However, it may not be the most efficient implementation and may need to be optimized for specific use cases.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

有头发的猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值