美文网首页
TI Sensor Tag 连iOS

TI Sensor Tag 连iOS

作者: babesamhsu | 来源:发表于2015-10-21 20:27 被阅读408次

偶得一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{}

未完待续

相关文章

  • TI Sensor Tag 连iOS

    偶得一TI Sensor Tag的开发板,浪费了时间把玩了一把 iOS是client(Central Mode)S...

  • Cinemachine collider

    是根据tag来选取镜头碰撞物,而不是layer 因为检测地面的sensor用的是ground层,不小心把tag和层...

  • nodejs ios 丢失 session

    tag:后端 nodejs session ios iframe 总结:ios 版本 12.1....

  • RunLoop

    参考:http://www.cocoachina.com/ios/20150601/11970.html 1、Ti...

  • Ios面试复习--传感器

    搭载在Ios设备上的传感器 运动传感器(Motion)/加速传感器、加速计(Accelermeter Sensor...

  • 学习资料

    学习笔记[https://www.cnblogs.com/GJ-ios/tag/iOS%E7%AC%94%E8%A...

  • 总结

    http://www.cnblogs.com/ldnh/tag/iOS面试/ 1、#import 跟#includ...

  • adb 打印kmsg过滤日志

    adb shell dmesg -w | grep -Ei "sensor1|sensor2"

  • git 回退到本地某一版本

    场景:git commit -a -m "--" ti 提交到本地 再 git pull 后 发现本地iOS工程文...

  • sensor曝光基本原理

    sensor曝光分为逐行曝光和全局曝光。逐行曝光的sensor 技术难度较全局曝光sensor 低,价格便宜,且分...

网友评论

      本文标题:TI Sensor Tag 连iOS

      本文链接:https://www.haomeiwen.com/subject/bkkthttx.html