簡體   English   中英

藍牙LE無法從中央向外圍設備發送數據

[英]Bluetooth LE can't send data from central to peripheral

我正在處理BluetoothLow Energy問題。 我找到了帶有iPhone的外圍BLE設備,連接到它,還找到了服務以及處於寫入模式的特性。 但是當我嘗試發送一些數據時,什么也沒發生-設備什么也沒收到,並且- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error is not - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error

我的writeValue方法在這里:

-(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];

}

可能是什么問題呢?

試試這個。 swift 3此代碼用於啟用CBCharacteristic的通知(notify屬性為0x12)。

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)
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM