我正在嘗試編寫一個玩具應用程序來加速BLE。我有一個我想掃描其服務的外圍設備(它是一個Tile)。但是,我不知道它提供的是什麼服務(並且我在產品的技術規範中沒有看到這一點)。所以,我從藍牙規格中獲得了list of services,並試圖掃描所有這些規格。我只是假設這份清單是詳盡的,因爲它沒有說這種或那種方式。BLE掃描未知服務
這裏是我的代碼:
- (void)viewDidLoad {
[super viewDidLoad];
// Scan for all available CoreBluetooth LE devices
_services = @[[CBUUID UUIDWithString:@"1811"],
[CBUUID UUIDWithString:@"1815"],
[CBUUID UUIDWithString:@"180F"],
[CBUUID UUIDWithString:@"1810"],
[CBUUID UUIDWithString:@"181B"],
[CBUUID UUIDWithString:@"181E"],
[CBUUID UUIDWithString:@"181F"],
[CBUUID UUIDWithString:@"1805"],
[CBUUID UUIDWithString:@"1818"],
[CBUUID UUIDWithString:@"1816"],
[CBUUID UUIDWithString:@"180A"],
[CBUUID UUIDWithString:@"181A"],
[CBUUID UUIDWithString:@"1800"],
[CBUUID UUIDWithString:@"1801"],
[CBUUID UUIDWithString:@"1808"],
[CBUUID UUIDWithString:@"1809"],
[CBUUID UUIDWithString:@"180D"],
[CBUUID UUIDWithString:@"1823"],
[CBUUID UUIDWithString:@"1812"],
[CBUUID UUIDWithString:@"1802"],
[CBUUID UUIDWithString:@"1821"],
[CBUUID UUIDWithString:@"1820"],
[CBUUID UUIDWithString:@"1803"],
[CBUUID UUIDWithString:@"1819"],
[CBUUID UUIDWithString:@"1807"],
[CBUUID UUIDWithString:@"1825"],
[CBUUID UUIDWithString:@"180E"],
[CBUUID UUIDWithString:@"1822"],
[CBUUID UUIDWithString:@"1806"],
[CBUUID UUIDWithString:@"1814"],
[CBUUID UUIDWithString:@"1813"],
[CBUUID UUIDWithString:@"1824"],
[CBUUID UUIDWithString:@"1804"],
[CBUUID UUIDWithString:@"181C"],
[CBUUID UUIDWithString:@"181D"]];
CBCentralManager *centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
[centralManager scanForPeripheralsWithServices:_services options:nil];
self.centralManager = centralManager;
}
- (IBAction)scanForDevices:(UIButton *)sender {
[_centralManager scanForPeripheralsWithServices:_services options:nil];
}
// CBCentralManagerDelegate - This is called with the CBPeripheral class as its main input parameter.
// This contains most of the information there is to know about a BLE peripheral.
- (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];
_hRMPeripheral = peripheral;
peripheral.delegate = self;
[self.centralManager connectPeripheral:peripheral options:nil];
}
}
// method called whenever the device state changes.
- (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");
}
}
中僅有的日誌輸出,我得到的是這樣的:
CoreBluetooth BLE硬件上電,並準備
否則,應用程序運行良好。沒有錯誤或警告。但它永遠找不到外圍設備。
我在做什麼錯?是否有我應該掃描的不同服務?
謝謝!
EDIT
使用下面的建議,我使用LightBlue應用掃描的外圍。現在我知道,有一個使用此UUID所通知的服務:
84100CBF-D622-8FF7-C8B8-300FDB52B92D
我改變了我的陣列看起來像這樣:
_services = @[[CBUUID UUIDWithString:@"84100CBF-D622-8FF7-C8B8-300FDB52B92D"]];
但仍然沒有得到日誌中的輸出。我也嘗試通過nil
但仍然沒有。
只需提供'nil'作爲服務來掃描並記錄您從App Store中接收或使用應用程序(如LightBlue)的內容。 – Paulw11
謝謝。我以前曾嘗試使用'nil',但也沒有產生任何結果。 LightBlue是一個好主意。我現在知道廣告數據包的UUID,我把它插入到我的'NSArray'中。但是,仍然沒有日誌輸出。我的代碼在某處存在問題。 – Alex