ios - 在 BLE 中读取和写入数据以快速打开/关闭 LED

标签 ios swift bluetooth-lowenergy core-bluetooth

我正在使用 iOS 开发 BLE。 我正在使用 BLE 服务打开/关闭 LED。我能够读取数据,但无法将数据发送到 BLE 设备。 当我向 BLE 发送 00 时,LED 应该关闭,当我发送 01 时,BLE 设备的 LED 应该打开。

这是我的代码片段。

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

        if let characterArray = service.characteristics as [CBCharacteristic]! {

            for cc in characterArray {

                if(cc.uuid.uuidString == "FEE1") { //properties: read, write
                                                   //if you have another BLE module, you should print or look for the characteristic you need.

                    myCharacteristic = cc //saved it to send data in another function.
                    //writeValue()

                    peripheral.readValue(for: cc) //to read the value of the characteristic
                }

            }
        }

    }

    func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {

        if (characteristic.uuid.uuidString == "FEE1") {

            let readValue = characteristic.value
            print(readValue as Any)
            let value = (readValue! as NSData).bytes.bindMemory(to: Int.self, capacity: readValue!.count).pointee //used to read an Int value

            print (value)
        }
    }


    //if you want to send an string you can use this function.
    func writeValue() {

        if isMyPeripheralConected { //check if myPeripheral is connected to send data
            let dataToSend: Data = "01".data(using: String.Encoding.utf8)!
            print(dataToSend)
            myBluetoothPeripheral.writeValue(dataToSend as Data, for: myCharacteristic, type: CBCharacteristicWriteType.withoutResponse)    //Writing the data to the peripheral

        } else {
            print("Not connected")
        }
    }

我该怎么做?

最佳答案

swift 3

  var arrayReadWriteChar = [CBCharacteristic]()


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

        for newChar: CBCharacteristic in service.characteristics!{

            if newChar.uuid.uuidString == "FEE1"{

                self.arrayReadWriteChar.append(newChar)

                periphreal.readValue(for: newChar)

            }
        }
    }

    func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {

        print("didUpdateValueForChar", characteristic)

        if let error1 = error{

            print(error1)
        }
        else{

            let value = [UInt8](characteristic.value!)

            print(value)    //whole array

            print(value[0]) //array object of index 0
        }
    }

    func writeValue(){

        if isMyPeripheralConected {

            let dataToSend: Data = "01".data(using: String.Encoding.utf8)!

            print(dataToSend)

            let command:[UInt8] = [0x01]        

            let sendData:Data = Data(command)

            myBluetoothPeripheral.writeValue(sendData, for: self.arrayReadWriteChar[0], type: .withResponse)
        } 
        else {

            print("Not connected")
        }
    }

关于ios - 在 BLE 中读取和写入数据以快速打开/关闭 LED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47139946/

相关文章:

ios - 在 iOS 上自动清除(清空/清除)缓存文件夹

swift - swift 中的下标和协议(protocol)

android - 我想从我们开发的移动应用程序的 11 字节数据中识别 BLE 车轮和曲柄传感器数据

java - 如何在 Xamarin 表单中为 BLE 拆分和发送 >20 字节的数据?

android - 你能在Android 5.0中唯一标识一个BLE MAC地址吗?

ios - 如何在 iOS 中获得 C 函数崩溃?

ios - 在 swift/objective-c 中将任意二进制数据编码/解码为字符串

javascript - 如何删除、清除、删除保存到 iPad 主屏幕的 Web 应用程序的 HTML 5 本地存储?

ios - iOS 8 中的 NavigationBar 栏、色调和标题文本颜色

可选 UI 元素的快速性能