This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How can I send data to peripheral from central when my nRF52832 is central then iPhone is peripheral

Hi everyone

I setting my nRF52832 is central and iPhone(iOS 12.3.2) is peripheral and using "ble_app_uart_c(SDK15.3.0  s132 v6.1.1)" sample program. I print the debug message from central, can see the message "<info> app: Connected to device with Nordic UART Service." But when I send data to peripheral from central, iPhone did not receive any information.

Here is my debug message:

<debug> ble_scan: Added filter on UUID 1

<info> app: BLE UART central example started.

<debug> ble_scan: Scanning

<debug> ble_scan: Connecting

<debug> ble_scan: Connection status: 0

<info> app: Connecting to target BD396002CC50

<debug> nrf_ble_gatt: Requesting to update ATT MTU to 247 bytes on connection 0x0.

<debug> nrf_ble_gatt: Updating data length to 251 on connection 0x0.

<debug> ble_db_disc: Starting discovery of service with UUID 0x1 on connection handle 0x0.

<debug> nrf_ble_gatt: Data length updated to 251 on connection 0x0.

<debug> nrf_ble_gatt: max_rx_octets: 251

<debug> nrf_ble_gatt: max_tx_octets: 251

<debug> nrf_ble_gatt: max_rx_time: 2120

<debug> nrf_ble_gatt: max_tx_time: 2120

<debug> ble_db_disc: Starting discovery of service with UUID 0x1 on connection handle 0x0.

<debug> nrf_ble_gatt: ATT MTU updated to 247 bytes on connection 0x0 (response).

<info> app: ATT MTU exchange completed.

<info> app: Ble NUS max data length set to 0xF4(244)

<debug> ble_db_disc: Found service UUID 0x1.

<debug> ble_db_disc: Discovery of service with UUID 0x1 completed with success on connection handle 0x0.

<info> ble_nus_c: ! nus_c_evt.handles.nus_rx_handle 44 !

<info> ble_nus_c: ! nus_c_evt.handles.nus_tx_handle 48 !

<info> ble_nus_c: ! nus_c_evt.handles.nus_tx_cccd_handle 4A !

<info> app: Discovery complete.

<info> app: Connected to device with Nordic UART Service.

Thanks everyone

Parents
  • So what code have you got running on the iPhone to implement NUS on the phone?

  • Hi awneil

    iPhone code for NUS:

    extension BluetoothManager: CBPeripheralManagerDelegate{
        
        func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
            switch peripheral.state {
            case .unknown:
                print("unknown")
            case .resetting:
                print("resetting")
            case .unsupported:
                print("unsupported")
            case .unauthorized:
                print("unauthorized")
            case .poweredOff:
                print("poweredOff")
            case .poweredOn:
                print("poweredOn")
                startAdvertisting()
            }
        }
        
        func startAdvertisting(){
            if let peripheralManager = self.peripheralManager{
                peripheralManager.stopAdvertising()
                peripheralManager.removeAllServices()
                if peripheralManager.state == .poweredOn{
                    setupServiceAndCharacteristics()
                }
            }
        }
        
        func stopAdvertising(){
            self.peripheralManager?.stopAdvertising()
        }
        
        func writeDataToCentral(){
            let data = "helloWorld".data(using: .utf8)
            self.peripheralManager?.updateValue(data!, for: characteristic!, onSubscribedCentrals: nil)
        }
        
        
    
        func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveRead request: CBATTRequest) {
            request.value = ?elloWorld??data(using: .utf8)
            peripheral.respond(to: request, withResult: .success)
            
            if let delegate = delegate{
                delegate.readCharacteristic()
            }
        }
        
        private func setupServiceAndCharacteristics() {
            let serviceID = CBUUID(string: Service_UUID)
            let service = CBMutableService(type: serviceID, primary: true)
            let characteristicID = CBUUID(string: Characteristic_UUID)
            let characteristicID2 = CBUUID(string: Characteristic_UUID2)
            let characteristic = CBMutableCharacteristic(type: characteristicID, properties: [.read, .write, .notify], value: nil, permissions: [.readable, .writeable])
            let characteristic2 = CBMutableCharacteristic(type: characteristicID2, properties: [.read, .write, .notify], value: nil, permissions: [.readable, .writeable, .readEncryptionRequired, .writeEncryptionRequired])
            service.characteristics = [characteristic, characteristic2]
            self.peripheralManager?.add(service)
        }
        
        func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveWrite requests: [CBATTRequest]) {
            for request in requests{
                let text = String.init(data: request.value!, encoding: String.Encoding.utf8)
                if let text = text, let delegate = self.delegate{
                    UserDefaults.standard.set(text, forKey: "text")
                    delegate.receiveCentralData(text: text)
                }
            }
    
        }
        
        func peripheralManager(_ peripheral: CBPeripheralManager, didAdd service: CBService, error: Error?) {
            if let err = error{
                print("err = \(err)")
                return
            }
            print("add service success")
            self.peripheralManager?.startAdvertising([CBAdvertisementDataServiceUUIDsKey : [CBUUID.init(string: Service_UUID)], CBAdvertisementDataLocalNameKey: "55688"])
        }
        
        func peripheralManagerDidStartAdvertising(_ peripheral: CBPeripheralManager, error: Error?) {
            if let err = error{
                print("err = \(err)")
                return
            }
        }
    }
      

    Service_UUID = 6E400001-B5A3-F393-E0A9-E50E24DCCA9E

    Create two Characteristics RX(Characteristic_UUID) and TX(Characteristic_UUID2), have set up "read", "write", "notify"

    Characteristic_UUID =  6E400002-B5A3-F393-E0A9-E50E24DCCA9E

    Characteristic_UUID2 =  6E400003-B5A3-F393-E0A9-E50E24DCCA9E

    Thank you

  • So what debugging have you done on the iPhone end to see what's happening?

  • Hi awneil

    I am trying to debug from iPhone

    Thank you

Reply Children
No Data
Related