代码语言: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];