Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Issues while connecting device with serial Bluetooth

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

like image 309
Kundan Avatar asked Oct 28 '15 10:10

Kundan


People also ask

Why is my Bluetooth not connecting properly?

If your Bluetooth devices won't connect, it's likely because the devices are out of range, or aren't in pairing mode. If you're having persistent Bluetooth connection problems, try resetting your devices, or having your phone or tablet "forget" the connection.

Why does my Bluetooth keep connecting and disconnecting?

There may be too many apps running in the background of the device the Bluetooth is attempting to pair with. Certain applications cause interference with the connection, and some devices are limited in the number of applications that can run concurrently. If in doubt, check with the headset manufacturer.


2 Answers

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
        }
    }
}
like image 153
Kamil Harasimowicz Avatar answered Sep 26 '22 22:09

Kamil Harasimowicz


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.

like image 26
gonzobrains Avatar answered Sep 24 '22 22:09

gonzobrains