У меня есть приложение для iPhone, и я хочу подключиться к устройству Bluetooth, чтобы получить измерения уровня глюкозы. Вы можете найти устройство на здесь.
Прочитав документацию Apple о Core Bluetooth, я начал читать это руководство< /а>. Также я получаю идентификаторы служб для устройств Bluetooth по этой ссылке здесь
Поэтому я начал кодировать, как в учебнике, после понимания основ.
И это мой код:
#define POLARH7_HRM_DEVICE_INFO_SERVICE_UUID @"180A" // for Device Information service.
#define POLARH7_HRM_HEART_RATE_SERVICE_UUID @"1808" // For Glucose service.
NSArray *services = @[[CBUUID UUIDWithString:POLARH7_HRM_HEART_RATE_SERVICE_UUID], [CBUUID UUIDWithString:POLARH7_HRM_DEVICE_INFO_SERVICE_UUID]];
CBCentralManager *centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
[centralManager scanForPeripheralsWithServices:services options:nil];
self.centralManager = centralManager;
И я реализовал делегатов для CBCentralManagerDelegate и CBPeripheralDelegate.
Я получаю уведомление для centralManagerDidUpdateState
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
// Determine the state of the peripheral
if ([central state] == CBCentralManagerStatePoweredOff) {
NSLog(@"CoreBluetooth BLE hardware is powered off");
}
else if ([central state] == CBCentralManagerStatePoweredOn) {
NSLog(@"CoreBluetooth BLE hardware is powered on and ready");
}
else if ([central state] == CBCentralManagerStateUnauthorized) {
NSLog(@"CoreBluetooth BLE state is unauthorized");
}
else if ([central state] == CBCentralManagerStateUnknown) {
NSLog(@"CoreBluetooth BLE state is unknown");
}
else if ([central state] == CBCentralManagerStateUnsupported) {
NSLog(@"CoreBluetooth BLE hardware is unsupported on this platform");
}
}
Мои журналы NSLog: CoreBluetooth BLE hardware is powered on and ready.
Но я не получил уведомление для central didDiscoverPeripheral
.
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
NSString *localName = [advertisementData objectForKey:CBAdvertisementDataLocalNameKey];
if ([localName length] > 0) {
NSLog(@"Found the heart rate monitor: %@", localName);
[self.centralManager stopScan];
self.polarH7HRMPeripheral = peripheral;
peripheral.delegate = self;
[self.centralManager connectPeripheral:peripheral options:nil];
}
}
Этот метод не вызывается.
Таким образом, центральный (мой iPhone) не смог обнаружить периферийное устройство, которым является мое устройство с глюкозой.
Я не смог найти устройство, когда искал его в меню «Настройки» -> «Bluetooth».
[centralManager scanForPeripheralsWithServices:nil options:nil];
, тогда посмотрим. Я не видел, действительно ли ваше устройство было BLE или нет с вашей ссылкой, и сделалcentralManagerDidUpdateState
когда доCBCentralManagerStatePoweredOn
, а затем вы сделали сканирование? - person Larme   schedule 03.03.2014