iOS-SwiftUI-BLE-Project icon indicating copy to clipboard operation
iOS-SwiftUI-BLE-Project copied to clipboard

Writing to a HM-10 module

Open alantrundle opened this issue 3 years ago • 4 comments

Can you provide instructions on how to write data to a connected peripheral please?

func write(value: Data, characteristic: CBCharacteristic) {

self.connectedPeripheral.peripheral.writeValue(value, for: characteristic, type: CBCharacteristicWriteType.withResponse) // OR self.connectedPeripheral.peripheral.writeValue(value, for: characteristic, type: CBCharacteristicWriteType.withoutResponse) }

Button("Turn on LED's") {

     let characteristicID = CBUUID(string: "FFE1") // as reported in BLE Terminal

     let characteristic = CBMutableCharacteristic(type: characteristicID, 
                                                              properties: [.write, .notify, .read],
                                                              value: nil,
                                                              permissions: .writeable)
                                    
                                    bleManager.write(value: "{\"RL\":1023,\"OL\":1023,\"GL\":1023,\"BL\":1023,\"BZ\":0}\r\n".data(using: .utf8)!, characteristic: characteristic)
                                
                                    
                                }

2023-04-03 01:27:59.368121+0100 SwiftUI-BLE-Project[3651:118475] [CoreBluetooth] WARNING: <CBMutableCharacteristic: 0x60000175f790 UUID = FFE1, Value = (null), Properties = 0x1A, Permissions = 0x2, Descriptors = (null), SubscribedCentrals = ( )> is not a valid characteristic for peripheral <CBPeripheral: 0x6000007656c0, identifier = 5066BE56-6F7F-31BA-0A5C-83736979927E, name = ArduinoPro, mtu = 23, state = connected>

alantrundle avatar Apr 02 '23 23:04 alantrundle

In response to your comment, here is a suggestion on how to modify the code to use the discovered characteristics stored in foundCharacteristics. I have updated the write() function and the Button action accordingly:

func write(value: Data, characteristicUUID: CBUUID) {
    guard let characteristic = foundCharacteristics.first(where: { $0.uuid == characteristicUUID }) else {
        print("Characteristic not found")
        return
    }

    self.connectedPeripheral.peripheral.writeValue(value, for: characteristic.characteristic, type: .withResponse)
    // OR
    self.connectedPeripheral.peripheral.writeValue(value, for: characteristic.characteristic, type: .withoutResponse)
}

Button("Turn on LED's") {
    let characteristicID = CBUUID(string: "FFE1") // as reported in BLE Terminal
    let data = "{\"RL\":1023,\"OL\":1023,\"GL\":1023,\"BL\":1023,\"BZ\":0}\r\n".data(using: .utf8)!
    bleManager.write(value: data, characteristicUUID: characteristicID)
}

This modification ensures that the correct discovered characteristic is used when writing data to the connected peripheral.

Kazuya-Ito-N avatar Apr 03 '23 03:04 Kazuya-Ito-N

Kazuya-Ito,

thank you for the quick response. I’ve added the Write function as updated, and I can confirm it’s working. I hope to see Later if I can print responses from my module back, and if I need some help are you able to assist me via discord?

i will leave my email [email protected], hopefully you can email me directly so we can catch up.

It’s incredibly good of you to release such a good “base” of SwiftUI core Bluetooth code.

Regards

Alan

alantrundle avatar Apr 04 '23 14:04 alantrundle

Hi kazuya-ito,

Thank you very much for helping me with the Write function, which does indeed work.

The next job is to populate a window with the incoming text strings from my HM-10 module.

I appreciate there is a time difference between Japan, and the UK, but was wondering if you could assist me with the very last function please?

discord : alantrundle#8757

The plan is to move things about With regards to my view controllers/ navigation, and when I’m happy I’ll submit it for a revision up to your GitHub as perhaps a branch?

Thanks

Alan

Sent from my iPhone

On 3 Apr 2023, at 04:26, kazuya ito @.***> wrote:



In response to your comment, here is a suggestion on how to modify the code to use the discovered characteristics stored in foundCharacteristics. I have updated the write() function and the Button action accordingly:

func write(value: Data, characteristicUUID: CBUUID) { guard let characteristic = foundCharacteristics.first(where: { $0.uuid == characteristicUUID }) else { print("Characteristic not found") return }

self.connectedPeripheral.peripheral.writeValue(value, for: characteristic.characteristic, type: .withResponse)
// OR
self.connectedPeripheral.peripheral.writeValue(value, for: characteristic.characteristic, type: .withoutResponse)

}

Button("Turn on LED's") { let characteristicID = CBUUID(string: "FFE1") // as reported in BLE Terminal let data = "{"RL":1023,"OL":1023,"GL":1023,"BL":1023,"BZ":0}\r\n".data(using: .utf8)! bleManager.write(value: data, characteristicUUID: characteristicID) }

This modification ensures that the correct discovered characteristic is used when writing data to the connected peripheral.

— Reply to this email directly, view it on GitHubhttps://github.com/Kazuya-Ito-B/iOS-SwiftUI-BLE-Project/issues/2#issuecomment-1493591302, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AY5LBIDXHUEREJZ2NP2BWPTW7I7M5ANCNFSM6AAAAAAWQTNHJE. You are receiving this because you authored the thread.Message ID: @.***>

alantrundle avatar Apr 05 '23 12:04 alantrundle

Hello Alan, I apologize for the delayed response!

My Discord username is 【kazuya ito#0313】. If you encounter any difficulties or need further assistance, please don't hesitate to reach out to me on Discord. I'll do my best to help you there.

Looking forward to assisting you.

Best regards,

Kazuya-Ito-N avatar Apr 10 '23 12:04 Kazuya-Ito-N