私はこの問題に頭を悩ませています。BLEデバイスに接続する、以下のコードで何が間違っているのかわかりません。
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_cm = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
+ (NSString*)UUIDString:(CFUUIDRef)uuid {
CFStringRef string = CFUUIDCreateString(NULL, uuid);
return (__bridge_transfer NSString*)string;
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
if (central.state == CBCentralManagerStatePoweredOn) {
[self scanForPeripherals];
}
}
- (void)centralManager:(CBCentralManager *)central
didDiscoverPeripheral:(CBPeripheral *)peripheral
advertisementData:(NSDictionary *)advertisementData
RSSI:(NSNumber *)RSSI {
// NSLog(@"Received peripheral : \n%@", peripheral);
// NSLog(@"Adv data : %@", advertisementData);
[peripheral setDelegate:self];
[central connectPeripheral:peripheral options:nil];
[peripheral readRSSI];
}
- (int)scanForPeripherals {
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:NO], CBCentralManagerScanOptionAllowDuplicatesKey,
nil];
[_cm scanForPeripheralsWithServices:nil options:options];
return 0;
}
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
NSLog(@"didConnectPeripheral");
}
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
NSLog(@"didDisconnectPeripheral");
}
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
NSLog(@"failed to connect");
}
- (void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(NSError *)error {
NSLog(@"didReadRSSI");
}
これらのデバイスは私のものではありません。近接 UUID はわかりませんが、私の知る限り、CoreBluetooth 経由で接続する際には必要ないのではないでしょうか?
すべてのデバイスが で検出されdidDiscoverPeripheral:
、セレクターで接続しようとしました。 しかし、その後は何も起こりません。
に電話したときに、ペアリング パスワード要求のダイアログが表示されるはずですかdidDiscoverPeripheral:
? その場合、ダイアログが表示されません。なぜでしょうか?
Apple のドキュメントには、デバイスに接続しようとした後、 またはdidConnectPeripheral
のいずれかに呼び出されるはずであると明記されていましたが、didFailToConnectPeripher
何も呼び出されませんでした。
何かご意見はありますか? ほぼ 1 週間試しています。どんな助言でも構いませんので、よろしくお願いします。
ベストアンサー1
peripheral
配信先のオブジェクトを何らかの方法で保持しないとdidDiscoverPeripheral
、このデリゲート メソッドが終了するとオブジェクトが解放され、接続が確立されません。
検出された周辺機器を追跡するためのプロパティを追加することをお勧めします
@property (strong,nonatomic) NSMutableArray *peripherals;
これを初期化するviewDidLoad
かinit
self.peripherals=[NSMutableArray new];
そして周辺機器を追加しますdidDiscoverPeripheral
-(void) centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
NSLog(@"Discovered peripheral %@",peripheral.identifier.UUIDString);
[self.peripherals addObject:peripheral];
[central connectPeripheral:peripheral options:nil];
}