Ios CoreBluetooth框架:如何在不知道服务ID的情况下获取连接的设备?
我有一个蓝牙手镯,通过蓝牙连接到我的iPhone 5s;它附带了一个名为Zeroner的应用程序。现在,我想在不使用应用程序的情况下,从连接并配对的手镯获取信息。以下是我试图做的:Ios CoreBluetooth框架:如何在不知道服务ID的情况下获取连接的设备?,ios,bluetooth,core-bluetooth,Ios,Bluetooth,Core Bluetooth,我有一个蓝牙手镯,通过蓝牙连接到我的iPhone 5s;它附带了一个名为Zeroner的应用程序。现在,我想在不使用应用程序的情况下,从连接并配对的手镯获取信息。以下是我试图做的: 安装CBCentralManager 使用检索连接的外围设备SwithServices:获取连接的设备 代码如下: CBConnectedDevicesVC.h #import <UIKit/UIKit.h> #import <CoreBluetooth/CoreBluetooth.h> #i
CBCentralManager
检索连接的外围设备SwithServices:
获取连接的设备#import <UIKit/UIKit.h>
#import <CoreBluetooth/CoreBluetooth.h>
#import SERVICE_ID @"FB694B90-F49E-4597-8306-171BBA78F846"
@interface CBConnectedDevicesVC : UIViewController <CBCentralManagerDelegate, CBPeripheralDelegate>
@property (strong, nonatomic) CBCentralManager *centralManager;
@property (strong, nonatomic) CBPeripheral *discoveredPeripheral;
@end
对于上述代码,我必须在service\u UUID
中指定服务ID,我不知道手镯的值是多少。是否有其他方法可从连接的手镯获取信息
更新关于LightBlue应用程序的测试结果 在从Zeroner应用程序取消配对并在“设置>蓝牙>选择名为“手镯-0366”的设备中“忘记此设备”后,LightBlue应用程序发现了该设备(最终!) 以下是结果屏幕截图: 这里有几个值,但我不确定应该使用哪些值 进一步测试结果: 如果我将LightBlue中的UUID(以4EFF开头)放入
服务\u ID
,则不会使用上述代码调用任何委托
我尝试的另一段代码是(从中获得):
使用上述代码,定义了两个常量
TRANSFER\u SERVICE\u ID
和TRANSFER\u CHARACTERISTIC\u ID
。根据本教程,应将TRANSFER\u SERVICE\u ID
设置为以4EFF
开头的ID,并将TRANSFER\u CHARACTERISTIC\u ID
设置为0xFF20
或FF20
。然而,这段代码根本检测不到手镯,即使手镯没有配对和断开连接。这次我错过了什么?您可以使用两种类型的蓝牙设备:
- 设备-无需(通常不能)将它们与iPhone配对
- “标准”蓝牙设备-使用前需要将其与iPhone配对
coreblutooth
只能用于BLE
对于已经配对的外部附件,您应该使用framework,不要担心以
4EFF
开头的UUID-这是设备的UUID,每个UUID都会有所不同
服务id是FF20
-您可以在扫描中使用它,用于外围设备服务-这是上面代码中的服务id
然后您有两个特征-FF21
和FF22
您可以在cbperipal
实例上使用writeValue
方法写入FF21
使用FF22
,您可以使用setNotify
cbperipal
方法订阅通知。然后,当设备更改值时,您将调用didUpdateValueForCharacteristic
CBPeripheralDelegate
方法。您尝试过LightBlue应用程序吗?它将向您展示该手镯正在尝试的任何服务。根本检测不到手镯。也许它已经连接了?在尝试淡蓝色之前,你应该确保你已经关闭了Zeroner应用程序。@Paulw11谢谢你的建议。以上问题已更新为LightBlue App.Oops的测试结果。你提醒我我找错方向了。尝试了外部附件
框架,但[[EAAccessoryManager sharedAccessoryManager]connectedAccessories]
未返回任何内容,尽管手镯已连接(我也有有效权限),但我想我的手镯正在使用蓝牙LE。我在您的指导下遇到了进一步的问题,你能给我一些新问题的提示吗?
#import "CBConnectedDevicesVC.h"
@implementation CBConnectedDevicesVC
- (void)viewDidLoad {
[super viewDidLoad];
_centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
if (central.state != CBCentralManagerStatePoweredOn) {
return;
}
if (central.state == CBCentralManagerStatePoweredOn) {
NSArray* connectedDevices = [_centralManager retrieveConnectedPeripheralsWithServices:@[[CBUUID UUIDWithString:SERVICE_UUID]]];
for (CBUUID *uuid in connectedDevices) {
NSLog(@"Device Found. UUID = %@", uuid);
}
}
}
@end
NSArray *serviceID;
@implementation CBCentralManagerViewController
- (void)viewDidLoad {
[super viewDidLoad];
serviceID = @[[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]];
_centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
_data = [[NSMutableData alloc] init];
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
// You should test all scenarios
if (central.state != CBCentralManagerStatePoweredOn) {
return;
}
if (central.state == CBCentralManagerStatePoweredOn) {
// Scan for devices
[_centralManager scanForPeripheralsWithServices:serviceID options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];
NSLog(@"Scanning started");
}
}
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
NSLog(@"Discovered %@ at %@", peripheral.name, RSSI);
if (_discoveredPeripheral != peripheral) {
// Save a local copy of the peripheral, so CoreBluetooth doesn't get rid of it
_discoveredPeripheral = peripheral;
// And connect
NSLog(@"Connecting to peripheral %@", peripheral);
[_centralManager connectPeripheral:peripheral options:nil];
}
}
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
NSLog(@"Failed to connect");
[self cleanup];
}
- (void)cleanup {
// See if we are subscribed to a characteristic on the peripheral
if (_discoveredPeripheral.services != nil) {
for (CBService *service in _discoveredPeripheral.services) {
if (service.characteristics != nil) {
for (CBCharacteristic *characteristic in service.characteristics) {
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID]]) {
if (characteristic.isNotifying) {
[_discoveredPeripheral setNotifyValue:NO forCharacteristic:characteristic];
return;
}
}
}
}
}
}
[_centralManager cancelPeripheralConnection:_discoveredPeripheral];
}
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
NSLog(@"Connected");
[_centralManager stopScan];
NSLog(@"Scanning stopped");
[_data setLength:0];
peripheral.delegate = self;
[peripheral discoverServices:serviceID];
}
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
if (error) {
[self cleanup];
return;
}
for (CBService *service in peripheral.services) {
[peripheral discoverCharacteristics:@[[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID]] forService:service];
}
// Discover other characteristics
}
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
if (error) {
[self cleanup];
return;
}
for (CBCharacteristic *characteristic in service.characteristics) {
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID]]) {
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
}
}
}
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
if (error) {
NSLog(@"Error");
return;
}
NSString *stringFromData = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];
// Have we got everything we need?
if ([stringFromData isEqualToString:@"EOM"]) {
[_textview setText:[[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding]];
[peripheral setNotifyValue:NO forCharacteristic:characteristic];
[_centralManager cancelPeripheralConnection:peripheral];
}
[_data appendData:characteristic.value];
}
- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
if (![characteristic.UUID isEqual:[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID]]) {
return;
}
if (characteristic.isNotifying) {
NSLog(@"Notification began on %@", characteristic);
} else {
// Notification has stopped
[_centralManager cancelPeripheralConnection:peripheral];
}
}
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
_discoveredPeripheral = nil;
//
[_centralManager scanForPeripheralsWithServices:serviceID options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];
}