CoreBluetooth 중앙 -> 주변 장치
-
21-12-2019 - |
문제
나는 Bluetooth 통신에 아주 새로운 것입니다.첫 번째 프로젝트는 iOS 장치에서 Bleshield (Small Chip)로 데이터를 전송하려고합니다.
중앙 코드를 테스트하려면 iPhone을 주변 장치 (칩에 알려주는 일단 IPAD)와 iPad를 중앙으로 설정하기로 결정했습니다.
i 장치를 연결하고 주변 장치에서 중앙으로 데이터를 전송할 수 있습니다.그래도 쉽습니다 :
- (void)startService {
_readChar = [[CBMutableCharacteristic alloc] initWithType:[CBUUID ...] properties:CBCharacteristicPropertyNotify value:nil permissions:CBAttributePermissionsReadable];
_writeChar = [[CBMutableCharacteristics alloc] initWithType:[CBUUID ...] properties:CBCharacteristicPropertyNotify value:nil permissions:CBAttributePermissionsWriteable];
_service = [[CBMutableService alloc] initWithType:[CBUUID ...] primary:YES];
[_service setCharacteristics:@[_readChar, _writeChar]];
_peripheral = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil];
[_peripheral addService:_service];
[_peripheral startAdvertising:@{CBAdvertisementDataServiceUUIDKey: @[[CBUUID ...]], CBAdvertisementDataLocalNameKey: @"ServiceName"}];
}
- (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didSubscribeToCharacteristic:(CBCharacteristic *)characteristic {
[_peripheral updateValue:[@"HELLO WORLD" dataUsingEncoding:NSUTF8StringEncoding] forCharacteristic:_readChar onSubscribedCentrals:nil];
}
.
그러나 나는 다른 방향을 일할 수 없다.중앙 측면에서 데이터를 보내려면 다음 코드가 있습니다.
[_activePeripheral writeValue:[@"PONG" dataUsingEncoding:NSUTF8StringEncoding] forCharacteristic:_writeChar type:CBCharacteristicWriteWithoutResponse];
.
이 방법 중 하나가 주변 장치에서 호출되어야한다고 가정합니다.
- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveReadRequest:(CBATTRequest *)request
- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteRequests:(NSArray *)requests
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
.
그러나 실제로 아무 일도 일어나지 않습니다.불행히도 내 하드웨어 프로젝트는 주변 모드에서만 작동 할 수있는 칩을 사용하며 끝에서는 제어 신호를위한 송신기이므로 주변 장치에 거의 독점적으로 쓰지 않습니다.
누군가가 나를 도울 수 있기를 바랍니다!
해결책
속성 :
-
_readChar
는CBCharacteristicPropertyRead
이어야합니다.
-
_writeChar
는CBCharacteristicPropertyWriteWithoutResponse
이어야합니다.
여기 여기를 참조하십시오.CBCharacteristicPropertyNotify
는 특성을 쓸 수 있거나 읽을 수 없도록 허용하지 않으며, 통지 할 수 있습니다 (구독 / 탈퇴).
다른 팁
https://github.com/lgbluetooth/lgbluetooth 삶을 더 쉽게 만들 수 있습니다
제휴하지 않습니다 StackOverflow