Skip to content

Commit

Permalink
Revert "Add service UUID, identifier and name for LTOBD2Adapter"
Browse files Browse the repository at this point in the history
This reverts commit eb4b733.
  • Loading branch information
Bennett Yuan committed Apr 29, 2019
1 parent adc2833 commit 955178d
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 128 deletions.
9 changes: 2 additions & 7 deletions Demo/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,13 @@ - (void)viewDidLoad
}

_transporter = [LTBTLESerialTransporter transporterWithIdentifier:nil serviceUUIDs:serviceUUIDs];
[_transporter connectWithBlock:^(NSInputStream *inputStream, NSOutputStream *outputStream,
NSString *identifier, NSString *serviceUUID, NSString *name) {
[_transporter connectWithBlock:^(NSInputStream *inputStream, NSOutputStream *outputStream) {
if (!inputStream) {
LOG(@"Could not connect to OBD2 adapter");
return;
}

self->_obd2Adapter = [LTOBD2AdapterELM327 adapterWithInputStream:inputStream
outputStream:outputStream
serviceUUID:serviceUUID
identifier:identifier
name:name];
self->_obd2Adapter = [LTOBD2AdapterELM327 adapterWithInputStream:inputStream outputStream:outputStream];
[self->_obd2Adapter connect];
}];

Expand Down
16 changes: 6 additions & 10 deletions LTSupportAutomotive/LTBTLESerialTransporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,18 @@ NS_ASSUME_NONNULL_BEGIN

extern NSString* const LTBTLESerialTransporterDidUpdateSignalStrength;

typedef void(^LTBTLESerialTransporterConnectionBlock)(NSInputStream * _Nullable inputStream,
NSOutputStream * _Nullable outputStream,
NSString * _Nullable serviceUUID,
NSString * _Nullable identifier,
NSString * _Nullable name);
typedef void(^LTBTLESerialTransporterConnectionBlock)(NSInputStream* _Nullable inputStream, NSOutputStream* _Nullable outputStream);

@interface LTBTLESerialTransporter : NSObject <CBCentralManagerDelegate, CBPeripheralDelegate>

@property(strong,nonatomic,readonly) NSNumber* signalStrength;

+ (instancetype)transporterWithIdentifier:(nullable NSUUID *)identifier serviceUUIDs:(NSArray<CBUUID *> *)serviceUUIDs;
- (void)connectWithBlock:(LTBTLESerialTransporterConnectionBlock)block;
- (void)disconnect;
+(instancetype)transporterWithIdentifier:(nullable NSUUID*)identifier serviceUUIDs:(NSArray<CBUUID*>*)serviceUUIDs;
-(void)connectWithBlock:(LTBTLESerialTransporterConnectionBlock)block;
-(void)disconnect;

- (void)startUpdatingSignalStrengthWithInterval:(NSTimeInterval)interval;
- (void)stopUpdatingSignalStrength;
-(void)startUpdatingSignalStrengthWithInterval:(NSTimeInterval)interval;
-(void)stopUpdatingSignalStrength;

@end

Expand Down
141 changes: 74 additions & 67 deletions LTSupportAutomotive/LTBTLESerialTransporter.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
#define XLOG(...)
#endif

@implementation LTBTLESerialTransporter {
@implementation LTBTLESerialTransporter
{
CBCentralManager* _manager;
NSUUID* _identifier;
NSArray<CBUUID*>* _serviceUUIDs;
CBPeripheral* _adapter;
CBCharacteristic* _reader;
CBCharacteristic* _writer;

NSMutableArray<CBPeripheral *> *_possibleAdapters;
NSMutableDictionary<NSString *, NSDictionary<NSString *, id> *> *_possibleAdvertisementDataTable;
NSMutableArray<CBPeripheral*>* _possibleAdapters;

dispatch_queue_t _dispatchQueue;

Expand All @@ -40,27 +40,28 @@ @implementation LTBTLESerialTransporter {
NSTimer* _signalStrengthUpdateTimer;
}

#pragma mark - Lifecycle
#pragma mark -
#pragma mark Lifecycle

+ (instancetype)transporterWithIdentifier:(NSUUID *)identifier serviceUUIDs:(NSArray<CBUUID *> *)serviceUUIDs
+(instancetype)transporterWithIdentifier:(NSUUID*)identifier serviceUUIDs:(NSArray<CBUUID*>*)serviceUUIDs
{
return [[self alloc] initWithIdentifier:identifier serviceUUIDs:serviceUUIDs];
}

- (instancetype)initWithIdentifier:(NSUUID*)identifier serviceUUIDs:(NSArray<CBUUID*>*)serviceUUIDs
-(instancetype)initWithIdentifier:(NSUUID*)identifier serviceUUIDs:(NSArray<CBUUID*>*)serviceUUIDs
{
if (!(self = [super init])) {
if ( ! ( self = [super init] ) )
{
return nil;
}

_identifier = identifier;
_serviceUUIDs = serviceUUIDs;

_dispatchQueue = dispatch_queue_create([NSStringFromClass(self.class) UTF8String], DISPATCH_QUEUE_SERIAL);
_dispatchQueue = dispatch_queue_create( [NSStringFromClass(self.class) UTF8String], DISPATCH_QUEUE_SERIAL );
_possibleAdapters = [NSMutableArray array];
_possibleAdvertisementDataTable = [NSMutableDictionary dictionary];

XLOG(@"Created w/ identifier %@, services %@", _identifier, _serviceUUIDs);
XLOG( @"Created w/ identifier %@, services %@", _identifier, _serviceUUIDs );

return self;
}
Expand Down Expand Up @@ -123,32 +124,39 @@ -(void)onSignalStrengthUpdateTimerFired:(NSTimer*)timer
[_adapter readRSSI];
}

#pragma mark - CBCentralManagerDelegate
#pragma mark -
#pragma mark <CBCentralManagerDelegate>

- (void)centralManagerDidUpdateState:(CBCentralManager *)central
-(void)centralManagerDidUpdateState:(CBCentralManager *)central
{
if (central.state != CBCentralManagerStatePoweredOn) {
if ( central.state != CBCentralManagerStatePoweredOn )
{
return;
}

NSArray<CBPeripheral *> *peripherals = [_manager retrieveConnectedPeripheralsWithServices:_serviceUUIDs];
if (peripherals.count) {
LOG(@"CONNECTED (already) %@", _adapter);
if (_adapter.state == CBPeripheralStateConnected) {
NSArray<CBPeripheral*>* peripherals = [_manager retrieveConnectedPeripheralsWithServices:_serviceUUIDs];
if ( peripherals.count )
{
LOG( @"CONNECTED (already) %@", _adapter );
if ( _adapter.state == CBPeripheralStateConnected )
{
_adapter = peripherals.firstObject;
_adapter.delegate = self;
[self peripheral:_adapter didDiscoverServices:nil];
} else {
}
else
{
[_possibleAdapters addObject:peripherals.firstObject];
[self centralManager:central didDiscoverPeripheral:peripherals.firstObject advertisementData:@{} RSSI:@127];
}
return;
}

if (_identifier) {
if ( _identifier )
{
peripherals = [_manager retrievePeripheralsWithIdentifiers:@[_identifier]];
}
if (!peripherals.count) {
if ( !peripherals.count )
{
// some devices are not advertising the service ID, hence we need to scan for all services
[_manager scanForPeripheralsWithServices:nil options:nil];
return;
Expand All @@ -160,23 +168,23 @@ - (void)centralManagerDidUpdateState:(CBCentralManager *)central
[_manager connectPeripheral:_adapter options:nil];
}

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *, id> *)advertisementData RSSI:(NSNumber *)RSSI
-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral*)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI
{
if (_adapter) {
LOG(@"[IGNORING] DISCOVER %@ (RSSI=%@) w/ advertisement %@", peripheral, RSSI, advertisementData);
if ( _adapter )
{
LOG( @"[IGNORING] DISCOVER %@ (RSSI=%@) w/ advertisement %@", peripheral, RSSI, advertisementData );
return;
}

LOG(@"DISCOVER %@ (RSSI=%@) w/ advertisement %@", peripheral, RSSI, advertisementData);
LOG( @"DISCOVER %@ (RSSI=%@) w/ advertisement %@", peripheral, RSSI, advertisementData );
[_possibleAdapters addObject:peripheral];
_possibleAdvertisementDataTable[peripheral.identifier.UUIDString] = advertisementData;
peripheral.delegate = self;
[_manager connectPeripheral:peripheral options:nil];
}

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
-(void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
LOG(@"CONNECT %@", peripheral);
LOG( @"CONNECT %@", peripheral );
[peripheral discoverServices:_serviceUUIDs];
}

Expand All @@ -195,33 +203,38 @@ -(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPer
}
}

#pragma mark - CBPeripheralDelegate
#pragma mark -
#pragma mark <CBPeripheralDelegate>

- (void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(NSError *)error
-(void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(NSError *)error
{
if (error) {
LOG(@"Could not read signal strength for %@: %@", peripheral, error);
if ( error )
{
LOG( @"Could not read signal strength for %@: %@", peripheral, error );
return;
}

_signalStrength = RSSI;
[[NSNotificationCenter defaultCenter] postNotificationName:LTBTLESerialTransporterDidUpdateSignalStrength object:self];
}

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
if (_adapter) {
LOG(@"[IGNORING] SERVICES %@: %@", peripheral, peripheral.services);
if ( _adapter )
{
LOG( @"[IGNORING] SERVICES %@: %@", peripheral, peripheral.services );
return;
}

if (error) {
LOG(@"Could not discover services: %@", error);
if ( error )
{
LOG( @"Could not discover services: %@", error );
return;
}

if (!peripheral.services.count) {
LOG(@"Peripheral does not offer requested services");
if ( !peripheral.services.count )
{
LOG( @"Peripheral does not offer requested services" );

[_manager cancelPeripheralConnection:peripheral];
[_possibleAdapters removeObject:peripheral];
Expand All @@ -230,34 +243,41 @@ - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)err

_adapter = peripheral;
_adapter.delegate = self;
if (_manager.isScanning) {
if ( _manager.isScanning )
{
[_manager stopScan];
}

CBService *atCommChannel = peripheral.services.firstObject;
CBService* atCommChannel = peripheral.services.firstObject;
[peripheral discoverCharacteristics:nil forService:atCommChannel];
}

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
for (CBCharacteristic *characteristic in service.characteristics) {
if (characteristic.properties & CBCharacteristicPropertyNotify) {
for ( CBCharacteristic* characteristic in service.characteristics )
{
if ( characteristic.properties & CBCharacteristicPropertyNotify )
{
LOG( @"Did see notify characteristic" );
_reader = characteristic;

// [peripheral readValueForCharacteristic:characteristic];
//[peripheral readValueForCharacteristic:characteristic];
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
}

if (characteristic.properties & CBCharacteristicPropertyWrite) {
if ( characteristic.properties & CBCharacteristicPropertyWrite )
{
LOG( @"Did see write characteristic" );
_writer = characteristic;
}
}

if (_reader && _writer) {
if ( _reader && _writer )
{
[self connectionAttemptSucceeded];
} else {
}
else
{
[self connectionAttemptFailed];
}
}
Expand Down Expand Up @@ -290,34 +310,21 @@ -(void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBC
[_outputStream characteristicDidWriteValue];
}

#pragma mark - Helper
#pragma mark -
#pragma mark Helpers

- (void)connectionAttemptSucceeded
-(void)connectionAttemptSucceeded
{
_inputStream = [[LTBTLEReadCharacteristicStream alloc] initWithCharacteristic:_reader];
_outputStream = [[LTBTLEWriteCharacteristicStream alloc] initToCharacteristic:_writer];
NSString *serviceUUID = [self getServiceUUIDUsedForConnect];
_connectionBlock(_inputStream, _outputStream, serviceUUID, _adapter.identifier.UUIDString, _adapter.name);
_connectionBlock( _inputStream, _outputStream );
_connectionBlock = nil;
}

- (void)connectionAttemptFailed
-(void)connectionAttemptFailed
{
_connectionBlock(nil, nil, nil, nil, nil);
_connectionBlock( nil, nil );
_connectionBlock = nil;
}

- (NSString *)getServiceUUIDUsedForConnect
{
NSDictionary<NSString *, id> *advertisementData = _possibleAdvertisementDataTable[_adapter.identifier.UUIDString];
NSArray<CBUUID *> *serviceUUIDs = advertisementData[CBAdvertisementDataServiceUUIDsKey];

for (CBUUID *uuid in serviceUUIDs) {
if ([_serviceUUIDs containsObject:uuid]) {
return uuid.UUIDString;
}
}
return nil;
}

@end
12 changes: 4 additions & 8 deletions LTSupportAutomotive/LTOBD2Adapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,16 @@ extern NSString* const LTOBD2AdapterDidReceive;
@property(nonatomic,readonly) NSString* friendlyAdapterType;
@property(nonatomic,readonly) NSString* friendlyAdapterVersion;

@property(nonatomic, readonly) NSString *recognizableServiceUUID;
@property(nonatomic, readonly) NSString *peripheralIdentifier;
@property(nonatomic, readonly) NSString *peripheralName;

@property(strong,nonatomic,readonly) NSArray<LTOBD2ECU*>* visibleECUs;

// configuration
@property(assign,nonatomic,readwrite) NSTimeInterval nextCommandDelay;

// lifecycle
+ (nullable instancetype)adapterWithInputStream:(NSInputStream *)inputStream outputStream:(NSOutputStream *)outputStream serviceUUID:(NSString *)serviceUUID identifier:(NSString *)identifier name:(NSString *)name;
- (nullable instancetype)initWithInputStream:(NSInputStream *)inputStream outputStream:(NSOutputStream *)outputStream serviceUUID:(NSString *)serviceUUID identifier:(NSString *)identifier name:(NSString *)name NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)init NS_UNAVAILABLE;
+ (nullable instancetype)new NS_UNAVAILABLE;
+(nullable instancetype)adapterWithInputStream:(NSInputStream*)inputStream outputStream:(NSOutputStream*)outputStream;
-(nullable instancetype)initWithInputStream:(NSInputStream*)inputStream outputStream:(NSOutputStream*)outputStream NS_DESIGNATED_INITIALIZER;
-(nullable instancetype)init NS_UNAVAILABLE;
+(nullable instancetype)new NS_UNAVAILABLE;

// connection handling
-(void)connect;
Expand Down
Loading

0 comments on commit 955178d

Please sign in to comment.