Objective c 无法连接到可扩展外围设备
我似乎无法从OSX连接到BLE外围设备。我可以扫描的很好,但连接从未发生过。我正在保存Objective c 无法连接到可扩展外围设备,objective-c,macos,core-bluetooth,cbcentralmanager,Objective C,Macos,Core Bluetooth,Cbcentralmanager,我似乎无法从OSX连接到BLE外围设备。我可以扫描的很好,但连接从未发生过。我正在保存CBPeripheral*对象的本地副本-事实上,我可以查询它,连接状态为cbperipheralstateconnection,如果取消连接,它将变回cbperipheralstateconnected。但是它从来没有真正连接过-状态不会更改为CBPeripheralStateConnected,并且我的连接回调都不会被调用 我可以使用LightBlue OSX应用程序连接、扫描服务和读取特征,因此我知道我的
CBPeripheral*
对象的本地副本-事实上,我可以查询它,连接状态为cbperipheralstateconnection
,如果取消连接,它将变回cbperipheralstateconnected
。但是它从来没有真正连接过-状态不会更改为CBPeripheralStateConnected
,并且我的连接回调都不会被调用
我可以使用LightBlue OSX应用程序连接、扫描服务和读取特征,因此我知道我的加密狗很好。我的代码基于其他人的代码,我知道代码对他有用。我的代码在iOS上也能正常工作,但在MacBook上不行。我继续前进,并升级到El Capitan,但这没有帮助
我尝试过各种方法:在数组中保存发现的外围对象,在属性中保存连接到的外围设备,将对connectperipal
的调用分派到主线程。。。不走运。代码的相关部分如下所示,以及
您不应该首先为服务创建UUID数组吗?@ElTomato用于扫描?没有必要;这样做只是过滤外围设备。我可以扫描并找到我的外围设备,但当我尝试连接(
connectPeripheral:
method)时,它永远不会脱离连接状态。好的。有趣的是,我昨天也试过同样的方法,结果几乎是一样的。我求助于核心蓝牙的介绍:构建一个心率监视器,他们的项目似乎也无法连接设备。你不应该首先为服务创建一个UUID阵列吗?@ElTomato用于扫描?没有必要;这样做只是过滤外围设备。我可以扫描并找到我的外围设备,但当我尝试连接(connectPeripheral:
method)时,它永远不会脱离连接状态。好的。有趣的是,我昨天也试过同样的方法,结果几乎是一样的。我求助于核心蓝牙的介绍:建立一个心率监视器,他们的项目似乎也没有连接到设备。
- (id) init {
if (self = [super init]) {
_peripherals = [NSMutableArray new];
_connecting = nil;
eventlock = [[NSLock alloc] init];
events = [[NSMutableArray alloc] init];
central = [[CBCentralManager alloc] initWithDelegate:self
queue:dispatch_get_main_queue()];
}
return self;
}
- (void) addPeripheral:(CBPeripheral *)peripheral {
if (![self.peripherals containsObject:peripheral]) {
NSLog(@"saving peripheral");
[self.peripherals addObject:peripheral];
}
}
- (void) connectPeripheral:(CBPeripheral *)peripheral {
if (![self.peripherals containsObject:peripheral]) {
NSLog(@"error: connect to invalid peripheral");
return;
}
__weak cbcentral *this = self;
NSLog(@"dispatch connect peripheral");
self.connecting = peripheral;
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"connect peripheral");
//this.connecting = peripheral;
[central connectPeripheral:this.connecting
options:nil];
});
}
#pragma mark CBCentralManagerDelegate
- (void) centralManager:(CBCentralManager *)central
didDiscoverPeripheral:(CBPeripheral *)peripheral
advertisementData:(NSDictionary *)advertisementData
RSSI:(NSNumber *)RSSI {
NSLog(@"did discover peripheral");
[self addPeripheral:peripheral];
NSArray* event = [[NSArray alloc] initWithObjects:@"centralManager_didDiscoverPeripheral_advertisementData_RSSI_",
central,
peripheral,
advertisementData,
RSSI,
nil];
[self queueEvent:event];
}
- (void) centralManager:(CBCentralManager *)central
didConnectPeripheral:(CBPeripheral *)peripheral {
NSLog(@"did connect peripheral");
[self addPeripheral:peripheral];
NSArray* event = [[NSArray alloc] initWithObjects:@"centralManager_didConnectPeripheral_",
central,
peripheral,
nil];
[self queueEvent:event];
}