简体   繁体   中英

Bluetooth LE can't send data from central to peripheral

I am dealing with BluetoothLow Energy problem. I find my peripheral BLE device with iPhone, connect to it, also find service and the characteristic which is in write mode. But when I try to send some data, nothing happens - device doesn't receive anything and also - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error is not called.

My writeValue method is here:

-(void) writeValue:(int)serviceUUID characteristicUUID:(int)characteristicUUID p:(CBPeripheral *)p data:(NSData *)data {

    CBUUID *UUIDService = [CBUUID UUIDWithString:[NSString stringWithFormat:@"%@",TRANSFER_SERVICE_UUID]];
    CBUUID *UUIDCharacteristic = [CBUUID UUIDWithString:[NSString stringWithFormat:@"%@",TRANSFER_CHARACTERISTIC_UUID]];

    NSLog(@"ALERT");
    CBService *service = [self findServiceFromUUID:UUIDService p:p];
    if (!service)
        {
            NSLog(@"Could not find service with UUID %@ on peripheral with UUID %@",
                  [self CBUUIDToString:UUIDService],
                  p.identifier.UUIDString);

            return;
        }

    CBCharacteristic *characteristic = [self findCharacteristicFromUUID:UUIDCharacteristic service:service];
    if (!characteristic)
    {
        NSLog(@"Could not find characteristic with UUID %@ on service with UUID %@ on peripheral with UUID %@",
              [self CBUUIDToString: UUIDCharacteristic],
              [self CBUUIDToString:UUIDService],
              p.identifier.UUIDString);

        return;
    }

    NSString *message = @"AB";
    NSData *dataMsg= [message dataUsingEncoding:NSUTF8StringEncoding];

    [p writeValue:dataMsg forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];

}

What could be the problem?

Try to this. swift 3 This code for enable notify for CBCharacteristic(0x12 for notify property).

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {

    for newChar: CBCharacteristic in service.characteristics!{

        peripheral.readValue(for: newChar)

        if newChar.properties.rawValue == 0x12{

            peripheral.setNotifyValue(true, for: newChar)
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM