Skip to content

Instantly share code, notes, and snippets.

@1amageek
Last active January 3, 2016 14:27
Show Gist options
  • Save 1amageek/66fe81a6abe3a594861b to your computer and use it in GitHub Desktop.
Save 1amageek/66fe81a6abe3a594861b to your computer and use it in GitHub Desktop.
CoreBluetooth swift bug
#import "ViewController.h"
@import CoreBluetooth;
@interface ViewController () <CBPeripheralManagerDelegate>
@property (nonatomic) CBPeripheralManager *peripheralManager;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil options:nil];
}
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral
{
// Opt out from any other state
if (peripheral.state != CBPeripheralManagerStatePoweredOn) {
return;
}
CBMutableCharacteristic *characteristic = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:@"3E770C8F-DB75-43AA-A335-1013A728BF42"]
properties:CBCharacteristicPropertyNotify
value:nil
permissions:CBAttributePermissionsReadable];
CBMutableService *transferService = [[CBMutableService alloc] initWithType:[CBUUID UUIDWithString:@"CB0CC42D-8F20-4FA7-A224-DBC1707CF89A"]
primary:YES];
transferService.characteristics = @[characteristic];
[self.peripheralManager addService:transferService];
}
- (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(NSError *)error
{
NSLog(@"%@", peripheral);
}
@end
import UIKit
import CoreBluetooth
class ViewController: UIViewController, CBPeripheralManagerDelegate {
var peripheralManager: CBPeripheralManager?
override func viewDidLoad() {
super.viewDidLoad()
peripheralManager = CBPeripheralManager(delegate: self, queue: nil, options: nil)
}
func peripheralManagerDidUpdateState(peripheral: CBPeripheralManager) {
if peripheral.state != CBPeripheralManagerState.PoweredOn {
return
}
let characteristic: CBMutableCharacteristic = CBMutableCharacteristic(type: CBUUID(string: "3E770C8F-DB75-43AA-A335-1013A728BF42"), properties: CBCharacteristicProperties.Notify, value: nil, permissions: .Readable)
let transferService: CBMutableService = CBMutableService(type: CBUUID(string: "CB0CC42D-8F20-4FA7-A224-DBC1707CF89A"), primary: true)
transferService.characteristics = [characteristic]
self.peripheralManager!.addService(transferService)
}
func peripheralManager(peripheral: CBPeripheralManager, didAddService service: CBService, error: NSError?) {
print(peripheral)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment