CoreBluetooth:尝试写入设备

我有一个连接和配对的蓝牙LE手镯(服务ID:FF20),有两个特点:

  • 0xFF21:写入没有响应
  • 0xFF22:通知

现在我尝试通过CoreBluetooth Framework将数据写入0xFF21,代码如下:

我在头文件中定义了2个常量:

#define TRANSFER_SERVICE_UUID @"FF20" #define TRANSFER_SERVICE_CHARACTERISTIC_UUID @"FF21" 

.M

 - (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:TRANSFER_SERVICE_UUID]]]; for (CBPeripheral *peripheral in connectedDevices) { NSLog(@"Device Found. CBPeripheral = %@", peripheral); peripheral.delegate = self; if(peripheral.services.count == 0) { NSLog(@"No service found"); } for (CBService *service in peripheral.services) { if(service.characteristics != nil) { for (CBCharacteristic *characteristic in service.characteristics) { if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID]]) { // check notifying ? NSData *data = [@"Testing" dataUsingEncoding:NSUTF8StringEncoding]; [peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse]; } else { NSLog(@"Specific Characteristic Not found"); } } } else { NSLog(@"Characteristic is NULL"); } } } } } 

日志出现:

 Device Found. CBPeripheral = <CBPeripheral: 0x1740f5080, identifier = 4EFF694C-017A-536F-9301-2EB2CC316CBE, name = Bracelet-0366, state = disconnected> No service found 

我错过了什么?

在发出读/写请求之前,您需要发出连接到已发现的外设,并调用您的didConnectPeripheral委托方法。

当您到达poweredOn状态时,您应该发出一个scanForDevicesWithServices调用,等待调用回您的委托,然后连接,发现服务和特性,然后您可以发出一个写。

 -(void) centralManagerDidUpdateState:(CBCentralManager *)central { switch (central.state) { case CBCentralManagerStatePoweredOn: [central scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]] options:nil]; break; } } -(void) centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI { NSLog(@"Discovered peripheral %@ (%@)",peripheral.name,peripheral.identifier.UUIDString); if (self.connectedPeripheral == nil) { [central connectPeripheral:peripheral options:nil]; } } -(void) centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral { self.connectedPeripheral=peripheral; NSLog(@"Connected to %@(%@)",peripheral.name,peripheral.identifier.UUIDString); peripheral.delegate=self; [peripheral discoverServices:@[[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]]; } -(void) peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error { for (CBService *service in peripheral.services) { NSLog(@"Discovered service %@",service.description); [peripheral discoverCharacteristics:nil forService:service]; } } -(void) peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error { for (CBCharacteristic *characteristic in service.characteristics ) { NSLog(@"Discovered characteristic %@(%@)",characteristic.description,characteristic.UUID.UUIDString); if ([characteristic.UUID.UUIDString isEqualToString:TRANSFER_SERVICE_CHARACTERISTIC_UUID) { NSData *data = [@"Testing" dataUsingEncoding:NSUTF8StringEncoding]; [peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithoutResponse]; } } } 

请注意,从您以前的问题,该特性只支持没有响应的写入,所以你不能使用CBCharacteristicWriteWithResponse