Getting Issues while connecting device with serial Bluetooth

情到浓时终转凉″ 提交于 2019-12-10 01:49:42

问题


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


回答1:


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
        }
    }
}



回答2:


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.



来源:https://stackoverflow.com/questions/33388153/getting-issues-while-connecting-device-with-serial-bluetooth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!