Хорошо, я работаю над забавным проектом, у которого есть препятствие, когда мне нужно включить поддержку звука Bluetooth для моего приложения для iOS.
Я сталкиваюсь с препятствием в том, что просто не могу начать получать список подключенных аудиоустройств Bluetooth. Несмотря на то, что мой iPhone 5S распознает мой наушник (точнее, 3–4-летний LG HBM-230) и воспроизводит через него звук для телефонных звонков, ОБА Внешний аксессуар и CoreBluetooth не дают мне ничего полезного, когда я запрашиваю оба.
Я основываю свой собственный код на вопросах и ответах, которые я нашел для платформ CoreBluetooth и External Accessory.
Когда мой код просто пытается " scanForPeripheralsWithServices:nil
" для любых устройств Bluetooth, которые Settings-> Bluetooth говорят, что они видимы и подключены, приведенный ниже код просто НЕ подходит ни к одному CBCentralManagerStatePoweredOn
сообщения " CBCentralManagerStatePoweredOn
" в консоли.
И эта строка в моем коде (с действительным экземпляром EAAccessoryManager)
NSArray * connectedDevices = [self.eAAccessoryManager connectedAccessories];
также возвращается с нулевым массивом.
Что я могу делать не так?
Кстати, я сделал этот код доступным как проект GitHub.
@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:
{
[email protected]"State unknown, update imminent.";
break;
}
case CBCentralManagerStateResetting:
{
[email protected]"The connection with the system service was momentarily lost, update imminent.";
break;
}
case CBCentralManagerStateUnsupported:
{
[email protected]"The platform doesn't support Bluetooth Low Energy";
break;
}
case CBCentralManagerStateUnauthorized:
{
[email protected]"The app is not authorized to use Bluetooth Low Energy";
break;
}
case CBCentralManagerStatePoweredOff:
{
[email protected]"Bluetooth is currently powered off.";
break;
}
case CBCentralManagerStatePoweredOn:
{
[email protected]"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