| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- // The MIT License (MIT)
- //
- // Created by : l0gg3r
- // Copyright (c) 2014 l0gg3r. All rights reserved.
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy of
- // this software and associated documentation files (the "Software"), to deal in
- // the Software without restriction, including without limitation the rights to
- // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
- // the Software, and to permit persons to whom the Software is furnished to do so,
- // subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in all
- // copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
- // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
- // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
- // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- #import "LGUtils.h"
- #if TARGET_OS_IPHONE
- #import <CoreBluetooth/CoreBluetooth.h>
- #elif TARGET_OS_MAC
- #import <IOBluetooth/IOBluetooth.h>
- #endif
- #import "LGBluetooth.h"
- /**
- * Error domain for Write errors
- */
- NSString * const kLGUtilsWriteErrorDomain = @"LGUtilsWriteErrorDomain";
- /**
- * Error domain for Read errors
- */
- NSString * const kLGUtilsReadErrorDomain = @"LGUtilsReadErrorDomain";
- /**
- * Error domain for Discover errors
- */
- NSString * const kLGUtilsDiscoverErrorDomain = @"LGUtilsDiscoverErrorDomain";
- /**
- * Global key for providing errors of LGBluetooth
- */
- NSString * const kLGErrorMessageKey = @"msg";
- /**
- * Error code for write operation
- * Service was not found on peripheral
- */
- const NSInteger kLGUtilsMissingServiceErrorCode = 410;
- /**
- * Error code for write operation
- * Characteristic was not found on peripheral
- */
- const NSInteger kLGUtilsMissingCharacteristicErrorCode = 411;
- /**
- * Error message for write operation
- * Service was not found on peripheral
- */
- NSString * const kLGUtilsMissingServiceErrorMessage = @"Provided service UUID doesn't exist in provided pheripheral";
- /**
- * Error message for write operation
- * Characteristic was not found on peripheral
- */
- NSString * const kLGUtilsMissingCharacteristicErrorMessage = @"Provided characteristic doesn't exist in provided service";;
- /**
- * Timeout of connection to peripheral
- */
- const NSInteger kLGUtilsPeripheralConnectionTimeoutInterval = 30;
- @implementation LGUtils
- /*----------------------------------------------------*/
- #pragma mark - Public Methods -
- /*----------------------------------------------------*/
- + (void)writeData:(NSData *)aData
- charactUUID:(NSString *)aCharacteristic
- serviceUUID:(NSString *)aService
- peripheral:(LGPeripheral *)aPeripheral
- completion:(LGCharacteristicWriteCallback)aCallback
- {
- if (aPeripheral.cbPeripheral.state == CBPeripheralStateConnected) {
- [self writeData:aData
- charactUUID:aCharacteristic
- serviceUUID:aService
- readyPeripheral:aPeripheral
- completion:aCallback];
- } else {
- [aPeripheral connectWithTimeout:kLGUtilsPeripheralConnectionTimeoutInterval completion:^(NSError *error) {
- [self writeData:aData
- charactUUID:aCharacteristic
- serviceUUID:aService
- readyPeripheral:aPeripheral
- completion:aCallback];
- }];
- }
- }
- + (void)readDataFromCharactUUID:(NSString *)aCharacteristic
- serviceUUID:(NSString *)aService
- peripheral:(LGPeripheral *)aPeripheral
- completion:(LGCharacteristicReadCallback)aCallback
- {
- if (aPeripheral.cbPeripheral.state == CBPeripheralStateConnected) {
- [self readDataFromCharactUUID:aCharacteristic
- serviceUUID:aService
- readyPeripheral:aPeripheral
- completion:aCallback];
- } else {
- [aPeripheral connectWithTimeout:kLGUtilsPeripheralConnectionTimeoutInterval completion:^(NSError *error) {
- [self readDataFromCharactUUID:aCharacteristic
- serviceUUID:aService
- readyPeripheral:aPeripheral
- completion:aCallback];
- }];
- }
- }
- + (void)discoverCharactUUID:(NSString *)aCharacteristic
- serviceUUID:(NSString *)aService
- peripheral:(LGPeripheral *)aPeripheral
- completion:(LGUtilsDiscoverCharacterisitcCallback)aCallback
- {
- if (aPeripheral.cbPeripheral.state == CBPeripheralStateConnected) {
- [self discoverCharactUUID:aCharacteristic
- serviceUUID:aService
- readyPeripheral:aPeripheral
- completion:aCallback];
- } else {
- [aPeripheral connectWithTimeout:kLGUtilsPeripheralConnectionTimeoutInterval completion:^(NSError *error) {
- [self discoverCharactUUID:aCharacteristic
- serviceUUID:aService
- readyPeripheral:aPeripheral
- completion:aCallback];
- }];
- }
- }
- /*----------------------------------------------------*/
- #pragma mark - Private Methods -
- /*----------------------------------------------------*/
- + (void)writeData:(NSData *)aData
- charactUUID:(NSString *)aCharacteristic
- serviceUUID:(NSString *)aService
- readyPeripheral:(LGPeripheral *)aPeripheral
- completion:(LGCharacteristicWriteCallback)aCallback;
- {
- [aPeripheral discoverServices:@[[CBUUID UUIDWithString:aService]] completion:^(NSArray *services, NSError *error) {
- LGService *service = nil;
- if (services.count && !error && (service = [self findServiceInList:services byUUID:aService])) {
- [service discoverCharacteristicsWithUUIDs:@[[CBUUID UUIDWithString:aCharacteristic]]
- completion:^(NSArray *characteristics, NSError *error)
- {
- LGCharacteristic *characteristic = nil;
- if (characteristics.count && (characteristic = [self findCharacteristicInList:characteristics byUUID:aCharacteristic])) {
- [characteristic writeValue:aData completion:aCallback];
- } else {
- if (aCallback) {
- if (!error) {
- aCallback([LGUtils writeErrorWithCode:kLGUtilsMissingCharacteristicErrorCode
- message:kLGUtilsMissingCharacteristicErrorMessage]);
- } else {
- aCallback(error);
- }
- }
- LGLogError(@"Missing provided characteristic : %@ in service : %@", aCharacteristic, aService);
- }
- }];
- } else {
- if (aCallback) {
- if (!error) {
- aCallback([LGUtils writeErrorWithCode:kLGUtilsMissingServiceErrorCode
- message:kLGUtilsMissingServiceErrorMessage]);
- } else {
- aCallback(error);
- }
- }
- LGLogError(@"Missing provided service : %@ in peripheral", aService);
- }
- }];
- }
- + (void)readDataFromCharactUUID:(NSString *)aCharacteristic
- serviceUUID:(NSString *)aService
- readyPeripheral:(LGPeripheral *)aPeripheral
- completion:(LGCharacteristicReadCallback)aCallback;
- {
- [aPeripheral discoverServices:@[[CBUUID UUIDWithString:aService]] completion:^(NSArray *services, NSError *error) {
- if (services.count && !error) {
- LGService *service = [self findServiceInList:services
- byUUID:aService];
- [service discoverCharacteristicsWithUUIDs:@[[CBUUID UUIDWithString:aCharacteristic]] completion:^(NSArray *characteristics, NSError *error) {
- if (characteristics.count) {
- LGCharacteristic *characteristic = [self findCharacteristicInList:characteristics
- byUUID:aCharacteristic];
- [characteristic readValueWithBlock:aCallback];
- } else {
- if (aCallback) {
- if (!error) {
- aCallback(nil, [LGUtils readErrorWithCode:kLGUtilsMissingCharacteristicErrorCode
- message:kLGUtilsMissingCharacteristicErrorMessage]);
- } else {
- aCallback(nil, error);
- }
- }
- }
- }];
- } else {
- if (aCallback) {
- if (!error) {
- aCallback(nil, [LGUtils readErrorWithCode:kLGUtilsMissingServiceErrorCode
- message:kLGUtilsMissingServiceErrorMessage]);
- } else {
- aCallback(nil, error);
- }
- }
- LGLogError(@"Missing provided service : %@ in peripheral", aService);
- }
- }];
- }
- + (void)discoverCharactUUID:(NSString *)aCharacteristic
- serviceUUID:(NSString *)aService
- readyPeripheral:(LGPeripheral *)aPeripheral
- completion:(LGUtilsDiscoverCharacterisitcCallback)aCallback
- {
- [aPeripheral discoverServices:@[[CBUUID UUIDWithString:aService]] completion:^(NSArray *services, NSError *error) {
- if (services.count && !error) {
- LGService *service = [self findServiceInList:services
- byUUID:aService];
- [service discoverCharacteristicsWithUUIDs:@[[CBUUID UUIDWithString:aCharacteristic]] completion:^(NSArray *characteristics, NSError *error) {
- if (characteristics.count) {
- LGCharacteristic *characteristic = [self findCharacteristicInList:characteristics
- byUUID:aCharacteristic];
- if (aCallback) {
- aCallback(characteristic, nil);
- }
- } else {
- if (aCallback) {
- if (!error) {
- aCallback(nil, [LGUtils discoverErrorWithCode:kLGUtilsMissingCharacteristicErrorCode
- message:kLGUtilsMissingCharacteristicErrorMessage]);
- } else {
- aCallback(nil, error);
- }
- }
- }
- }];
- } else {
- if (aCallback) {
- if (!error) {
- aCallback(nil, [LGUtils discoverErrorWithCode:kLGUtilsMissingServiceErrorCode
- message:kLGUtilsMissingServiceErrorMessage]);
- } else {
- aCallback(nil, error);
- }
- }
- LGLogError(@"Missing provided service : %@ in peripheral", aService);
- }
- }];
- }
- /**
- * Find characteristic in characteristic list by providied UUID string
- * @return Found characteristic, nil if no one found
- */
- + (LGCharacteristic *)findCharacteristicInList:(NSArray *)characteristics
- byUUID:(NSString *)anID
- {
- for (LGCharacteristic *characteristic in characteristics) {
- if ([[characteristic.UUIDString lowercaseString] isEqualToString:[anID lowercaseString]]) {
- return characteristic;
- }
- }
- return nil;
- }
- /**
- * Find service in services list by providied UUID string
- * @return Found service, nil if no one found
- */
- + (LGService *)findServiceInList:(NSArray *)services
- byUUID:(NSString *)anID
- {
- for (LGService *service in services) {
- if ([[service.UUIDString lowercaseString] isEqualToString:[anID lowercaseString]]) {
- return service;
- }
- }
- return nil;
- }
- /*----------------------------------------------------*/
- #pragma mark - Error Generators -
- /*----------------------------------------------------*/
- + (NSError *)writeErrorWithCode:(NSInteger)aCode message:(NSString *)aMsg
- {
- return [NSError errorWithDomain:kLGUtilsWriteErrorDomain
- code:aCode
- userInfo:@{kLGErrorMessageKey : aMsg}];
- }
- + (NSError *)readErrorWithCode:(NSInteger)aCode message:(NSString *)aMsg
- {
- return [NSError errorWithDomain:kLGUtilsReadErrorDomain
- code:aCode
- userInfo:@{kLGErrorMessageKey : aMsg}];
- }
- + (NSError *)discoverErrorWithCode:(NSInteger)aCode message:(NSString *)aMsg
- {
- return [NSError errorWithDomain:kLGUtilsDiscoverErrorDomain
- code:aCode
- userInfo:@{kLGErrorMessageKey : aMsg}];
- }
- @end
|