Skip to content

Instantly share code, notes, and snippets.

@douglasjunior
Last active August 29, 2015 14:21
Show Gist options
  • Save douglasjunior/850600368aeac1e9ab12 to your computer and use it in GitHub Desktop.
Save douglasjunior/850600368aeac1e9ab12 to your computer and use it in GitHub Desktop.
Exemplo de código Objective-C
#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>
@interface BluetoothWriter : NSObject
-(id) initWithPeripheral:(CBPeripheral *) peripheral characteristic:(CBCharacteristic *)characteristic;
- (void) writeString:(NSString *) value;
- (void) writeData:(NSData *) value;
-(void) writeColorWithRed:(int)red withGreen:(int)green withBlue:(int)blue;
@end
#import "BluetoothWriter.h"
@interface BluetoothWriter (){
}
@property (weak, nonatomic) CBPeripheral *peripheral;
@property (weak, nonatomic) CBCharacteristic *characteristic;
@end
@implementation BluetoothWriter
-(id) initWithPeripheral:(CBPeripheral *)per characteristic:(CBCharacteristic *)charac{
if ((self = [super init])) {
self.peripheral = per;
self.characteristic = charac;
}
return self;
}
-(void) writeData:(NSData *)value{
if (self.peripheral != nil && self.characteristic != nil) {
[self.peripheral writeValue:value forCharacteristic:self.characteristic type:CBCharacteristicWriteWithoutResponse];
}
}
-(void) writeString:(NSString *)value{
[self writeData:[value dataUsingEncoding:NSUTF8StringEncoding]];
}
-(void) writeColorWithRed:(int)red withGreen:(int)green withBlue:(int)blue{
[self writeString:[NSString stringWithFormat:@"c%d,%d,%d\n",red,green,blue]];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment