使用Core Bluetooth iOS将数据发送到RFDuino

时间:2015-03-08 21:58:29

标签: ios swift bluetooth core-bluetooth rfduino

我在Swift中编写ann应用程序,要求我能够从iOS设备向RFDuino发送和接收数据。在cconway的UBP库之上,我已经能够将我的RFDuino温度测量值显示在我的iPad上,但现在我正在努力让iPad发送一个对RFduino的简单消息。

我使用原始的Objective-C代码作为参考。它可以找到here

以下是我如何寻找特色:

func peripheral(peripheral: CBPeripheral!, didDiscoverCharacteristicsForService service: CBService!, error: NSError!) {
    if let characteristicsArr = service.characteristics  as? [CBCharacteristic]
    {

        println("Discovered characteristics on RFduino");

        for aCharacteristic in characteristicsArr {

                if (aCharacteristic.properties & CBCharacteristicProperties.Notify) == CBCharacteristicProperties.Notify {

                    // Register to be notified whenever the RFduino transmits
                    peripheral.setNotifyValue(true, forCharacteristic: aCharacteristic)
                }

                if (aCharacteristic.properties & CBCharacteristicProperties.Write) == CBCharacteristicProperties.Write {

                    // Register to be notified whenever the RFduino transmits
                    loadedService = true
                    sendCharacteristic = aCharacteristic
                    peripheral.setNotifyValue(true, forCharacteristic: aCharacteristic)
                }

        }      

这是我按下按钮时发送的功能:

func send() {        
    if let service = loadedService{
        if _selectedPeripheral?.state == CBPeripheralState.Connected {
            var parameter = NSInteger(1)
            let data = NSData(bytes: &parameter, length: 1)
            if let characteristic:CBCharacteristic? = sendCharacteristic{
                _selectedPeripheral?.writeValue(data, forCharacteristic: characteristic!, type: CBCharacteristicWriteType.WithResponse)
            }
        }
    }
}

现在,如果我实际按下发送按钮,我将与RFDuino断开连接。我可能做错了什么想法?

修改

这里是你应该如何寻找RFDuino的写特性。如果我想出一个更好的方案,这将被重新发布一半。

    func peripheral(peripheral: CBPeripheral!, didDiscoverCharacteristicsForService service: CBService!, error: NSError!) {
    if let characteristicsArr = service.characteristics  as? [CBCharacteristic]
    {


        println("Discovered characteristics on RFduino");
        for (number, aCharacteristic) in enumerate(characteristicsArr){

                println("charc type: \(_stdlib_getTypeName(aCharacteristic.UUID))")

                if (aCharacteristic.properties & CBCharacteristicProperties.Notify) == CBCharacteristicProperties.Notify {

                    // Register to be notified whenever the RFduino transmits
                    peripheral.setNotifyValue(true, forCharacteristic: aCharacteristic)
                }

            if compareID(aCharacteristic.UUID, secondID: writeUUID()){
                    println("\(aCharacteristic) Found Something!")
                    // Register to be notified whenever the RFduino transmits
                    loadedService = true
                    sendCharacteristic = aCharacteristic
                    peripheral.setNotifyValue(true, forCharacteristic: aCharacteristic)
                }

        }
    }
}

其中compareUUID和writeUUID定义为:

private func compareID(firstID:CBUUID, secondID:CBUUID)->Bool {
    return firstID.UUIDString == secondID.UUIDString

}

func writeUUID() -> CBUUID{
    return CBUUID(string:"2222") //write char' of rfduino - how to determine 'on the fly'?
}

0 个答案:

没有答案