Home>

With BLE communication between iOS, Central writes the unique ID to Peripheral,
Peripheral wants to build a program that holds it.

Problem i am experiencing

Peripheral is written with ID and didReceiveWrite starts, but the written data cannot be retrieved.
In the program that will be described later, requests seems to be passed by pointer, and I do not know how to handle it.
In requests, 2 bytes of data can be confirmed in _value at the time of Xcode debugging.

Applicable source code
/* Central * /
private var my_peripheral: CBPeripheral? = nil
private var ID_characteristic: CBCharacteristic? = nil
private let ID_UUID: CBUUID = CBUUID (string: "ID_UUID")
private var peripheral_ID: UInt = 1111 // ID to write
...
func peripheral (_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
        print ("didDiscoverCharacteristicsFor")
        guard ((peripheral == my_peripheral!)
            &&(service.characteristics! = nil)) else {
            return
        }
        for characteristic in service.characteristics! {
            / * identification of characteristic * /
            correspond_characteristic (characteristic: characteristic)
        }
    }

func correspond_characteristic (characteristic: CBCharacteristic) {
    if (characteristic.uuid == ID_UUID) {
        / * Write ID to peripheral * /
        if (ID_characteristic == nil) {
            ID_characteristic = characteristic
            write_ID ()
        } else {
            ...
        }
    } else {
        ...
    }
}

func write_ID () {
    guard my_peripheral! = nil else {
        print ("no peripheral")
        return
    }
    guard ID_characteristic! = nil else {
        Log.d ("no characteristic")
        return
    }
    var data: [UInt8] = []
    var write_ID: UInt16 = UInt16 (peripheral_ID)
    data.append (contentsOf: withUnsafeBytes (of:&write_ID) {$0.map {UInt8 ($0)}})
    let write_data = Data (data)
    print ("write_data = \ (String (describing: write_data))" ")
     // write_data = 2byte
    my_peripheral! .writeValue (write_data, for: ID_characteristic !, type: CBCharacteristicWriteType.withResponse)
}
/* Peripheral * /
private var ID_characteristic: CBMutableCharacteristic? = nil
private let ID_UUID: CBUUID = CBUUID (string: "ID_UUID")
private let ID_property: CBCharacteristicProperties = [.notify, .write, .read]
private let ID_permission: CBAttributePermissions = [.readable, .writeable]
...
func peripheralManager (_ peripheral: CBPeripheralManager, didReceiveWrite requests: [CBATTRequest]) {
    guard ID_characteristic! = nil else {
        print ("no chracteristic")
        return
    }
    for request in requests {
        if (request.characteristic.uuid == ID_UUID) {
            / * When ID could not be written normally * /
            if ((request.characteristic.value == nil) || (request.characteristic.value! .isEmpty)) {
                print ("ID not recv")
                print ("request ID = \ (request.characteristic.value)")
                // request ID = nil
                peripheralManager.respond (to: request, withResult: CBATTError.invalidHandle)
                return
            }
            let value = NSData (data: request.characteristic.value!)
            let elements_num = request.value! .count
            var recv_ID: UInt16 = 0
            value.getBytes (&recv_ID, range: NSRange (location: 0, length: elements_num))
            print ("new ID = \ (recv_ID)")
        } else {
            ...
        }
    }
}
Supplemental information such as the version of the tool i am using

Swift 5.0
Xcode 11.0
iOS 12.3
MacOS Mojave 10.14.6

Update is not possible for convenience.
I would appreciate your help.

  • Answer # 1

    Solution

    Self-solved.
    My understanding of CBATTRequest was wrong.
    I thought that there was write data in request.characteristic.value,
    The data is stored in request.value
    It worked fine when I corrected the relevant part.

    Request.characteristic has been changed to understand that it is an object only for specifying the write destination.

    Reference

    https://blog.reinforce-lab.com/2013/09/19/2013-09-19-blebook-ch3-corebluetooth/
    https://developer.apple.com/documentation/corebluetooth/cbattrequest

Related articles