Getting Issues while connecting device with serial Bluetooth
Asked Answered
R

2

12

I am facing 2 problems related to regular Bluetooth.Here is my code.

- (void)viewDidLoad {
    [super viewDidLoad];
    [NSTimer scheduledTimerWithTimeInterval:3.0 target:self    selector:@selector(showElements) userInfo:nil repeats:NO]; 
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(accessoryConnected:) name:EAAccessoryDidConnectNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(accessoryDisconnected:) name:EAAccessoryDidConnectNotification object:nil];    
    [[EAAccessoryManager sharedAccessoryManager]registerForLocalNotifications];
}

-(void)showElements{
    [[EAAccessoryManager sharedAccessoryManager] showBluetoothAccessoryPickerWithNameFilter:nil completion:^(NSError *error) {
        if (error) {
            NSLog(@"error :%@", error);
        }
        else{
            NSLog(@"Its Working");
        }
    }];    
}

- (void)accessoryConnected:(NSNotification *)notification
{    
    EAAccessory *connectedAccessory = [[notification userInfo] objectForKey:EAAccessoryKey];

}

1) I am getting this error after connection got established.

error :Error Domain=EABluetoothAccessoryPickerErrorDomain Code=1 "(null)"

Here is the full log:-

BTM: attaching to BTServer
BTM: setting pairing enabled
BTM: found device "ESGAA0010" 00:04:3E:95:BF:82
BTM: disabling device scanning
BTM: connecting to device "ESGAA0010" 00:04:3E:95:BF:82
BTM: attempting to connect to service 0x00000080 on device "ESGAA0010" 00:04:3E:95:BF:82
BTM: connection to service 0x00000080 on device "ESGAA0010" 00:04:3E:95:BF:82 succeeded
BTM: setting pairing disabled
error :Error Domain=EABluetoothAccessoryPickerErrorDomain Code=1 "(null)"

you can see the last line of log, its showing error. As i searched and found that apple documentation says the error means device not found(EABluetoothAccessoryPickerResultNotFound), but how come in log it shows its connected if its not found.

2) accessoryConnected: method not getting called. Its most probably because of first issue. But i thought its worth mentioning here.

I have added ExternalAccessory framework and device is MFI compliant. Help me to fix these. Thanks

Rezzani answered 28/10, 2015 at 10:8 Comment(5)
If you try with EAAccessorySelectedKey instead of EAAccessoryKey. Don't know what should be the correct key, according to the doc, one is when the picker is shown to connect. Also, the error output you get is before of after the EAAccessory *connectedAccessory line?Wapentake
@Wapentake thanks for replying but the method itself is not getting called..then this doesn't seems to be the problem.Rezzani
On what device it's tested? What's is iOS version?Wapentake
I am testing it on iPhone 6 and iPhone 5 with iOS 9.1 and iOS 8.4 resp.Rezzani
@Shivaay I am getting this error error :Error Domain=EABluetoothAccessoryPickerErrorDomain Code=2 "(null)"Centring
U
15

I met the same problem today. Solution is easy, you need to add extra row to your .plist file.

<key>UISupportedExternalAccessoryProtocols</key>
<array>
    <string>YOUR_DEVICE_PROTOCOL</string>
</array>

If device is added to MFi Program it should have own protocol. Check your device documentation or ask device creators.

Edit

[[EAAccessoryManager sharedAccessoryManager] showBluetoothAccessoryPickerWithNameFilter:nil completion:^(NSError *error) {
    if (error) {
        NSLog(@"error :%@", error);
    }
    else{
        NSLog(@"Its Working");
    }
}]; 

The error is instance of EABluetoothAccessoryPickerError. There is a possibility values:

public enum Code : Int {
    public typealias _ErrorType = EABluetoothAccessoryPickerError

    case alreadyConnected
    case resultNotFound
    case resultCancelled
    case resultFailed
}

Your error code is 1 so resultNotFound. Note that when you fix .plist file showBluetoothAccessoryPickerWithNameFilter sometimes return error code = 0. Then there is no error because your device is case alreadyConnected. I add this information because I lost a lot of time before I detected this. :)

Good luck.

Edit (Swift 3.0)

EAAccessoryManager.shared().showBluetoothAccessoryPicker(withNameFilter: nil) { (error) in
    if let error = error {
        switch error {
        case EABluetoothAccessoryPickerError.alreadyConnected:
            break
        default:
            break
        }
    }
}
Universalism answered 22/2, 2017 at 12:42 Comment(4)
I wonder do you have this in Swift 3.0? .I'm struggling to show user this Error in UIAlertView, how are you calling the case? shouldn't the cases have int 0 to 3 ?Nominalism
I updated my answer with Swift 3.0 version with error handling.Universalism
UIAlert​View has been deprecated since iOS 9, use UIAlertController insteadUniversalism
Thanks guys for your help.Nominalism
S
1

Try going into the iOS Bluetooth settings and unpairing the device and pairing it again. I got this "305" error before and the problem was that I had paired the device and then updated the device's firmware. After that it would not connect again until I removed the device from my iPhone and then re-paired it after the device's firmware update.

This may not work for you but there is not much out there on the interwebs regarding the 305 error so hopefully this will at least help someone out.

Snapshot answered 23/9, 2016 at 18:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.