TI Sensor Tag 连iOS

偶得一TI Sensor Tag的开发板,浪费了时间把玩了一把

iOS是client(Central Mode)SensorTag是server(Peripheral Mode)。一直不是特别为毛设备会是server,后来看到下面这句才搞清楚:

server is the side where there is data to serve

把玩目的

  • 发现并连接设备
  • 用Notify方式和直接读取的方式来获取传感器的数据
  • 写入某个Characteristic
  • OTA更新设备固件

准备工作

苹果官方给出的和蓝牙相关的开发文档

在工程里引入CoreBluetooth.framework
ViewController的类实现这两个Delegate<CBCentralManagerDelegate, CBPeripheralDelegate>

创建 CoreBluetooth Central Manager

_centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

在创建成功后,CBCentralManager会触发回调函数,在回调函数中应用程序可以有机会处理蓝牙的不同状态。

- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
    
    /*
    CBCentralManagerStateUnknown = 0,
    CBCentralManagerStateResetting,
    CBCentralManagerStateUnsupported,
    CBCentralManagerStateUnauthorized,
    CBCentralManagerStatePoweredOff,
    CBCentralManagerStatePoweredOn
    */
    if (central.state != CBCentralManagerStatePoweredOn) {
        return;
    }
    [self scan];
}

连接步骤:

  1. 扫描蓝牙设备
NSArray *services = @[[CBUUID UUIDWithString:TI_SENSOR_UUID]];
[self.centralManager scanForPeripheralsWithServices:services options:nil];

碰到一个奇怪的情况,当指定一个Service UUID时,无法发现包含这个UUID的外设。原因是在Sensor Tag的广播包里没有包括任何服务的UUID信息,所以iOS无法找到对应的外设。

  1. 连接外设 - 发现设备成功后或失败后,对应的回调函数会被调用。
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{
     // 可以在这里根据广播包的内容来判断是否是我们想要连接的设备
     if (![localName isEqualToString:@"SensorTag"]) { 
        return;
     }
     
     // RSS值可以用来判断蓝牙信号的强弱,对于有些应用场景,可能需要蓝牙信号足够强才允许用户使用
     if (RSSI.integerValue < -35) {
         return;
     }
     
     // 最后可以试图连接外设设备
     [self.centralManager connectPeripheral:peripheral options:nil];
}
  1. 发现服务 Services - 设备连接成功或失败后,对应的回调函数也会被触发,在回调函数中可以进一步查询在外设设备上的服务。
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{
    // 连接了外设后,就可以停止扫面设备了
    [self.centralManager stopScan];
    
    // 用来在下一步发现设备时获得回调函数的调用
    peripheral.delegate = self;
    
    // 在此外设上寻找UUID为TI_SENSOR_TAG_DEVICE_INFO的服务
    NSArray *services = @[[CBUUID UUIDWithString:TI_SENSOR_TAG_DEVICE_INFO]];
    [peripheral discoverServices:services];
}
  1. 发现特征 Characteristics - 同样在发现完设备上的服务后,对应的回调函数也会被调用。
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
    if (error) {
        NSLog(@"Error discovering services: %@", [error localizedDescription]);
        [self cleanup];
        return;
    }
    
    // 遍历外设上所有的服务
    for (CBService *service in peripheral.services) {
        NSLog(@"Service UUID:%@ - %@", service.UUID.UUIDString, service.description);
        // 可以按照UUID在指定的Service上发现对应的特征
        [peripheral discoverCharacteristics:nil forService:service];
    }
}
  1. 更新特征值 / 通过Notify方式来获得特征值的变化
// 发现特征值后被触发的回调函数
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error { 
    if (error) {
        NSLog(@"Error discovering characteristics: %@", [error localizedDescription]);
        [self cleanup];
        return;
    }
    
    // 遍历该服务上的所有特征值
    for (CBCharacteristic *characteristic in service.characteristics) {
        NSLog(@"Character.UUID: %@ of Service %@-%@", characteristic.UUID, service.description, service.UUID);
        
        // 根据特征值是否可Notify方式侦听来对应处理
        if (characteristic.isNotifying) {
            [peripheral setNotifyValue:YES forCharacteristic:characteristic];
        }
        else {
            [peripheral readValueForCharacteristic:characteristic];
        }
    }
}
  1. 读取数据 - 直接读取方式
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{}
  1. 读取数据 - Notify方式
- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{}

未完待续

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容