在IOS中的IOS7中检测启用了蓝牙的iPhone设备
问题描述:
我正在我的应用程序中使用Core Bluetooth Framework。
I am using Core Bluetooth Framework in my app.
我知道如何扫描外围设备并从中获取价值(例如心率监视器)
I know how to scan for peripherals and getting values from it.(like Heart Rate Monitor)
但是我要检索的是支持BLE 4.0和Bluetooth Enabled的iPhone设备列表。
But what I want is to retrieve the surrounding iPhone Devices list that supports BLE 4.0 and Bluetooth Enabled ones.
我提到了
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
// I'm not sure how to make this work
NSLog (@"Discovered peripheral: %@", [peripheral name]);
[self.list addObject:peripheral.name]; // add peripheral name to foundarray
NSLog (@"UUID peripheral: %@", [peripheral UUID]);
NSLog (@"peripheral services before connected: %@", [peripheral services]);
NSLog(@"adversting data %@",[NSString stringWithFormat:@"%@",[advertisementData description]]);
NSLog(@"foundArray is %@", self.list);
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
NSLog(@"Central manager's state is updated to: %@", central);
if(central.state == CBCentralManagerStatePoweredOn)
{
//okay your good to go and can now scan
}
else
{
//Unable to use CentralManager methods so print out the central.state and find out why
}
}
我不知道Apple是否提供此功能。
I dont know whether Apple provides this or not..
任何建议或想法都会受到赞赏。
Any Suggestions or Ideas will be appreciated..
谢谢。.
答
尝试下面的代码 checkBluetoothAccess
和 requestBluetoothAccess
方法
- (void)checkBluetoothAccess {
if(!self.cbManager) {
self.cbManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}
/*
We can ask the bluetooth manager ahead of time what the authorization status is for our bundle and take the appropriate action.
*/
CBCentralManagerState state = [self.cbManager state];
if(state == CBCentralManagerStateUnknown) {
[self alertViewWithDataClass:Bluetooth status:NSLocalizedString(@"UNKNOWN", @"")];
}
else if(state == CBCentralManagerStateUnauthorized) {
[self alertViewWithDataClass:Bluetooth status:NSLocalizedString(@"DENIED", @"")];
}
else {
[self alertViewWithDataClass:Bluetooth status:NSLocalizedString(@"GRANTED", @"")];
}
}
- (void)requestBluetoothAccess {
if(!self.cbManager) {
self.cbManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}
/*
When the application requests to start scanning for bluetooth devices that is when the user is presented with a consent dialog.
*/
[self.cbManager scanForPeripheralsWithServices:nil options:nil];
}