This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)centralManager:(CBCentralManager *)central | |
didDiscoverPeripheral:(CBPeripheral *)peripheral | |
advertisementData:(NSDictionary *)advertisementData | |
RSSI:(NSNumber *)RSSI { | |
if ([self isIgnorePeripheral:RSSI]) { | |
return; | |
} | |
if (peripheral) { | |
if (![self.peripherals containsObject:peripheral]) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)centralManagerDidUpdateState:(CBCentralManager *)central { | |
if (central.state != CBCentralManagerStatePoweredOn) { | |
return; | |
} | |
[self scan:central]; | |
} | |
- (void)scan:(CBCentralManager *)central { | |
CBUUID *uuid = [CBUUID UUIDWithString:kServiceUUID]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSDictionary *options = @{CBCentralManagerOptionRestoreIdentifierKey : CENTRAL_IDENTIFIER_KEY}; | |
_centralManager = [[CBCentralManager alloc] initWithDelegate:self | |
queue:_serialQueue | |
options:options]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)sendData:(NSData *)sendMsg | |
dataIndex:(NSInteger)sendMsgIndex { | |
// First up, check if we're meant to be sending an EOM | |
static BOOL sendingEOM = NO; | |
NSString *logMessage = nil; | |
const int NOTIFY_MTU = 20; | |
const float SendMessageTimeInterval = 0.2f; | |
if (sendingEOM) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)peripheralManager:(CBPeripheralManager *)peripheral | |
didReceiveWriteRequests:(NSArray <CBATTRequest *>*)requests { | |
for (CBATTRequest *request in requests) { | |
if (request.value) { | |
NSString *value = [[NSString alloc] initWithData:request.value encoding:NSUTF8StringEncoding]; | |
if ([_delegate respondsToSelector:@selector(BLEPeripheralComponentReceiveMessage:message:)]) { | |
__weak typeof(self) weakSelf = self; | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
[weakSelf.delegate BLEPeripheralComponentReceiveMessage:self message:value]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)peripheralManager:(CBPeripheralManager *)peripheral | |
central:(CBCentral *)central | |
didSubscribeToCharacteristic:(CBCharacteristic *)characteristic { | |
if (![self.centralM containsObject:central]) { | |
[self.centralM addObject:central]; | |
} | |
[self updateCharacteristicValue]; | |
[self stopAdvertising]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral | |
error:(NSError *)error { | |
if (error) { | |
// error handle | |
return; | |
} | |
// start advertising | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)peripheralManager:(CBPeripheralManager *)peripheral | |
didAddService:(CBService *)service | |
error:(NSError *)error { | |
if (error) { | |
return; | |
} | |
[self startAdvertising]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)setupService { | |
// Step 1: Create CharacteristicUUID | |
CBUUID *characteristicUUID = [CBUUID UUIDWithString:kCharacteristicUUID]; | |
CBMutableCharacteristic *characteristicM = [[CBMutableCharacteristic alloc] | |
initWithType:characteristicUUID | |
properties:CBCharacteristicPropertyNotify | | |
CBCharacteristicPropertyRead | | |
CBCharacteristicPropertyWrite | | |
CBCharacteristicPropertyWriteWithoutResponse | |
value:nil |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral { | |
if (peripheral.state != CBPeripheralManagerStatePoweredOn) { | |
// stopAdvertising | |
return; | |
} | |
// setup service | |
[self setupService]; | |
} |