前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >蓝牙开发笔记

蓝牙开发笔记

原创
作者头像
100000879746
发布2023-09-27 17:22:55
1550
发布2023-09-27 17:22:55
举报
文章被收录于专栏:蓝牙技术蓝牙技术
代码语言:javascript
复制
//把数据写到Characteristic中
//写数据
-(void)writeCharacteristic:(CBPeripheral *)peripheral
            characteristic:(CBCharacteristic *)characteristic
                     value:(NSData *)value{
    //打印出 characteristic 的权限,可以看到有很多种,这是一个NS_OPTIONS,就是可以同时用于好几个值,常见的有read,write,notify,indicate,知知道这几个基本就够用了,前连个是读写权限,后两个都是通知,两种不同的通知方式。
    /*
     typedef NS_OPTIONS(NSUInteger, CBCharacteristicProperties) {
     CBCharacteristicPropertyBroadcast                                                = 0x01,
     CBCharacteristicPropertyRead                                                    = 0x02,
     CBCharacteristicPropertyWriteWithoutResponse                                    = 0x04,
     CBCharacteristicPropertyWrite                                                    = 0x08,
     CBCharacteristicPropertyNotify                                                    = 0x10,
     CBCharacteristicPropertyIndicate                                                = 0x20,
     CBCharacteristicPropertyAuthenticatedSignedWrites                                = 0x40,
     CBCharacteristicPropertyExtendedProperties                                        = 0x80,
     CBCharacteristicPropertyNotifyEncryptionRequired NS_ENUM_AVAILABLE(NA, 6_0)        = 0x100,
     CBCharacteristicPropertyIndicateEncryptionRequired NS_ENUM_AVAILABLE(NA, 6_0)    = 0x200
     };
     */
    [self writeTolog:[NSString stringWithFormat:@"%lu", (unsigned long)characteristic.properties]];
    //只有 characteristic.properties 有write的权限才可以写
    if(characteristic.properties & CBCharacteristicPropertyWrite){
        /*
         最好一个type参数可以为CBCharacteristicWriteWithResponse或type:CBCharacteristicWriteWithResponse,区别是是否会有反馈
         */
        [peripheral writeValue:value forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
    }else{
        NSLog(@"该字段不可写!");
    }
}

【链接】LightBlue基础使用教程 http://www.jianshu.com/p/2bfde2ba8a99

【链接】iOS蓝牙入门 http://www.jianshu.com/p/ae8c44b166d7

【链接】『CoreBluetooth』1.初识 http://www.saitjr.com/ios/core-bluetooth-overview.html

代码语言:javascript
复制
 //1. 创建中心设备管理者,并且设置代理
 //初始化方式一:,不会提示出现"打开蓝牙允许'xxxx'连接都配件"的系统提示
   _myCentralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:nil];
   //初始化方式二
   _myCentralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档