So finden Sie Bluetooth-Audiogeräte in iOS

Okay, ich arbeite an einem unterhaltsamen Projekt mit einer Hürde, bei dem ich die Bluetooth-Audiounterstützung für meine iOS-App aktivieren muss.

Die Hürde, die ich habe, ist, dass ich einfach nicht einmal eine Liste der verbundenen Bluetooth-Audiogeräte abrufen kann. Obwohl mein iPhone 5S meine Hörmuschel erkennt (ca. 3-4 Jahre alt)LG HBM-230um genau zu sein) und spielt Audio für Telefonanrufe ab,BEIDE Externes Zubehör und CoreBluetooth bieten mir bei beiden Abfragen nichts Nützliches.

Ich stütze meinen eigenen Code auf Fragen und Antworten, die ich für beide gefunden habeCoreBluetooth undExternes Zubehör Frameworks.

Wenn mein Code einfach versucht "scanForPeripheralsWithServices:nil" zumirgendein Bluetooth-Geräte, von denen Einstellungen-> Bluetooth sagen, dass sie sichtbar und verbunden sind, der folgende Code kommt einfach NICHT mit einem einzigen Treffer über "CBCentralManagerStatePoweredOnmsgstr "Nachricht in der Konsole.

Und diese Zeile in meinem Code (mit einer gültigen EAAccessoryManager-Instanz)

NSArray * connectedDevices = [self.eAAccessoryManager connectedAccessories];

kommt auch mit einem Null-Array zurück.

Was könnte ich falsch machen?

B.T.W.,Ich habe diesen Code als GitHub-Projekt zur Verfügung gestellt.

@implementation BluetoothManager

+ (BluetoothManager *)sharedInstance
{
    static dispatch_once_t pred = 0;
    __strong static id _bluetoothMGR = nil;

    dispatch_once(&pred, ^{
        _bluetoothMGR = [[BluetoothManager alloc] init];
    });

    return _bluetoothMGR;
}

- (id)init
{
    self = [super init];
    if(self)
    {
        dispatch_queue_t centralQueue = dispatch_queue_create("com.yo.mycentral", DISPATCH_QUEUE_SERIAL);

        // whether we try this on a queue of "nil" (the main queue) or this separate thread, still not getting results
        self.cbManager = [[CBCentralManager alloc] initWithDelegate:self queue:centralQueue options:nil];
    }
    return self;
}

// this would hit.... if I instantiated this in a storyboard of XIB file
- (void)awakeFromNib
{
    if(!self.cbManager)
        self.cbManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:nil];
}

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {

    NSLog(@"hey I found %@",[advertisementData description]);
}

- (void)centralManager:(CBCentralManager *)central didRetrieveConnectedPeripherals:(NSArray *)peripherals
{
    NSLog( @"I retrieved CONNECTED peripherals");
}

-(void)centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals{
    NSLog(@"This is it!");
}

- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
    NSString *messtoshow;

    switch (central.state) {
        case CBCentralManagerStateUnknown:
        {
            messtoshow=@"State unknown, update imminent.";
            break;
        }
        case CBCentralManagerStateResetting:
        {
            messtoshow=@"The connection with the system service was momentarily lost, update imminent.";
            break;
        }
        case CBCentralManagerStateUnsupported:
        {
            messtoshow=@"The platform doesn't support Bluetooth Low Energy";
            break;
        }
        case CBCentralManagerStateUnauthorized:
        {
            messtoshow=@"The app is not authorized to use Bluetooth Low Energy";
            break;
        }
        case CBCentralManagerStatePoweredOff:
        {
            messtoshow=@"Bluetooth is currently powered off.";
            break;
        }
        case CBCentralManagerStatePoweredOn:
        {
            messtoshow=@"Bluetooth is currently powered on and available to use.";
            NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], CBCentralManagerScanOptionAllowDuplicatesKey, nil];

            [_cbManager scanForPeripheralsWithServices:nil options:options];

            break;
        }   

    }
    NSLog(@"%@", messtoshow);
}

@end

Antworten auf die Frage(1)

Ihre Antwort auf die Frage