Iphone检测蓝牙设备

问题描述:

我正在做一个iPhone应用程序,我需要得到附近的蓝牙设备。 我正在使用CoreBluetooth框架,并且不起作用。我知道这个框架只适用于低能耗蓝牙,所以我购买了支持低能耗的蓝牙4.0 usb适配器。我在我的iPhone测试这个程序5.这是我的代码Iphone检测蓝牙设备

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

switch (central.state) { 
    case CBCentralManagerStateUnknown: 
    { 
     messtoshow=[NSString stringWithFormat:@"State unknown, update imminent."]; 
     break; 
    } 
    case CBCentralManagerStateResetting: 
    { 
     messtoshow=[NSString stringWithFormat:@"The connection with the system service was momentarily lost, update imminent."]; 
     break; 
     } 
    case CBCentralManagerStateUnsupported: 
     { 
     messtoshow=[NSString stringWithFormat:@"The platform doesn't support Bluetooth Low Energy"]; 
     break; 
     } 
    case CBCentralManagerStateUnauthorized: 
     { 
     messtoshow=[NSString stringWithFormat:@"The app is not authorized to use Bluetooth Low Energy"]; 
     break; 
     } 
    case CBCentralManagerStatePoweredOff: 
     { 
     messtoshow=[NSString stringWithFormat:@"Bluetooth is currently powered off."]; 
     break; 
     } 
    case CBCentralManagerStatePoweredOn: 
     { 
     messtoshow=[NSString stringWithFormat:@"Bluetooth is currently powered on and available to use."]; 
     [mgr scanForPeripheralsWithServices:[NSArray arrayWithObject:[CBUUID UUIDWithString:@"180A"]] options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:CBCentralManagerScanOptionAllowDuplicatesKey]]; 

     [mgr retrieveConnectedPeripherals]; 


     break; 
    }    
} 
NSLog(messtoshow); 
} 

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral: 
(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI 
{ 
NSLog(@"Did discover peripheral. peripheral: %@ rssi: %@, UUID: %@ advertisementData: %@ ", peripheral, RSSI, peripheral.UUID, advertisementData); 
[mgr retrievePeripherals:[NSArray arrayWithObject:(id)peripheral.UUID]]; 
} 

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

本刊“蓝牙正在通电并可以使用。”,但没有找到我的USB蓝牙或其他附近的蓝牙设备。我需要一个解决方案,我会变得疯狂。 非常感谢。

+0

我结束了类似的问题。你有没有在这里找到解决方案? – Abhinav

您确定180A是正确的服务吗?

如果不尝试:

[mgr scanForPeripheralsWithServices:nil options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:CBCentralManagerScanOptionAllowDuplicatesKey]]; 
+0

我试过所有的posibles服务,我试着将服务设置为零。它不起作用。我放弃了,但是谢谢。 – Arturo18

+0

您是否检查过此内容:https://developer.apple.com/library/ios/#samplecode/BTLE_Transfer/Introduction/Intro.html#//apple_ref/doc/uid/DTS40012927? – Aboelseoud

您的代码似乎罚款。 Search the app store for TI BLE for their free iPhone app.

如果他们的应用无法发现您的蓝牙设备,请确保您的蓝牙设备处于开启状态并且正在进行广告宣传。