2015-06-18 193 views
0

我對使用Swift進行編碼非常陌生,所以我希望有人可以用簡單的答案幫助我。BLE更改要寫入的UUID特徵

我基本上試圖發送一個特徵UUID到一個寫功能,並切換寫功能寫入該UUID。它會繼續使用我原來的那個。是否有捷徑可尋?我已經發現了所有的外圍設備,服務,特性,並且已經連接到了正確的一個。我只是無法讓它切換到正確的特徵UUID。任何幫助都是極好的!我是一個固件/硬件開發人員,所以Swift和Xcode對我來說並不是一件普通的事情。

func writePosition(position: UInt16, characteristicUUID: CBUUID) { 

    var position_low: UInt8 = 0 
    var position_high: UInt8 = 0 
    var position16: UInt16 = 0; 

    position_low = (UInt8) (position) // (position) 
    position_high = (UInt8)((position >> 8)) 


    // See if characteristic has been discovered before writing to it 
    if self.positionCharacteristic == nil { 
     return 
    } 
    var bytes:[UInt8] = [0x00, 0x00] 

    bytes[1] = 0x01 //low byte is [1], high byte is [0] so reversed from LightBlue app 

    bytes[1] = position_low //low byte is [1], high byte is [0] so reversed from LightBlue app 
    bytes[0] = position_high //low byte is [1], high byte is [0] so reversed from LightBlue app 



    let uuidsForBTService: [CBUUID] = [characteristicUUID] 
    //new code to try and set the correct UUID automatically 
    for service in peripheral!.services 
    { 
     if service.UUID == BLEServiceUUID 
     { 
      peripheral!.discoverCharacteristics(uuidsForBTService, forService: service as! CBService) 
     } 

    } 


    //find the correct characteristic to set to and then write to it 
    //go through each characteristic and look at its UUID then if it matches, set us to that one so we write to it later on 
    for characteristic in self.peripheral!.services { 
     if characteristic.UUID == characteristicUUID//Position1PWMCharUUID 
     { 
      self.positionCharacteristic = (characteristic as! CBCharacteristic) 
      peripheral!.setNotifyValue(true, forCharacteristic: characteristic as! CBCharacteristic) 

      // Send notification that Bluetooth is connected and all required characteristics are discovered 
      self.sendBTServiceNotificationWithIsBluetoothConnected(true) 
     } 
    } 

    //end of code to set the UUID 


    // Need a mutable var to pass to writeValue function 
    //var positionValue = position 
    //let data = NSData(bytes: &positionValue, length: sizeof(UInt16)) 
    let data = NSData(bytes: bytes, length: 2) 
    self.peripheral?.writeValue(data, forCharacteristic: self.positionCharacteristic, type: CBCharacteristicWriteType.WithResponse) 



} 
+0

糾正我,如果我不明白你的問題。 基本上你需要寫入任何你得到的參數uuid。我對嗎? –

回答

0

記住,在第二for循環,在那裏你正在尋找一個CBCharacteristic你仍然從您的BLE服務周邊,而不是特徵迭代服務(外圍具有服務和服務有特色)。

以下代碼是獲取您需要的CBCharacteristic

var theService: CBService? 

// Find service 
for service in peripheral.services as! [CBService] { 
    if service.UUID == BLEServiceUUID { 
     theService = service 

     break 
    } 
} 

var theCharacteristic: CBCharacteristic? 

// Find characteristic 
for characteristic in theService?.characteristics as! [CBCharacteristic] { 
    if characteristic.UUID == characteristicUUID { 
     theCharacteristic = characteristic 

     break 
    } 
} 

// Write 
if let characteristic = theCharacteristic { 
    peripheral.writeValue(data, forCharacteristic: characteristic, type: .WithResponse) 
} 

在您的代碼中,您總是寫信給self.positionCharacteristic。只需將您的數據寫入您剛發現的CBCharacteristic即可。

另外,如果您需要更多幫助,我寫了一個簡單的swift class,管理中央連接,讀取和寫入任務。

希望它有幫助!