CoreBluetooth:尝试写入设备

CoreBluetooth: Attempt to write to device

我有一个已连接并配对的蓝牙 LE 手环(服务 ID:FF20),它具有 2 个特征:

现在我尝试使用以下代码通过 CoreBluetooth 框架将数据写入 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 委托方法,然后才能发出 read/write 请求。

当你到达 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];
        }
    }
}

注意,从你之前的问题来看,该特性只支持write without response,所以你不能使用CBCharacteristicWriteWithResponse