Search code examples
iosiphoneswiftbluetooth-lowenergycore-bluetooth

Peripheral device services showing nil in all BLE Scanner iOS App but not in android code


Part Number: CC2640R2F

Tool/software: Code Composer Studio

I am working on a project and in my project the CC2640R2F is performing advertising and scanning. I am using a custom BLE service to receive data from an app (Android or iOS) to CC2640R2F. Whenever I am connecting the device with BLE Scanner in android I am getting all the services and all the characteristics, and I am also able to send and receive the data without any problem. This is also working in my custom android app.

But whenever I am connecting the CC2640R2F device with BLE scanner in iOS app, the device gets connected but I am not getting any services or characteristics in the app. The same situation is with our developed custom ios app also. Why is this happening? If I am getting all the things on Android, then this should also work iOS.

Screenshot of BLE scanner Android app: Screenshot of Ble scanner Android app

iOS app BLE scanner iOS app BLE scanner

Code to check if bluetooth is powered on and scan for peripherals: [![Code to check if bluetooth is powered on and scan for peripherals][3]][3]

Code to connect to device if peripheral is found: [![Code to connect to device if peripheral is found][4]][4]

Code to search for services after the device is connected (if services found then it will break the timer() else search for service again):

Code to search for services after the device is connected

func centralManagerDidUpdateState(_ central: CBCentralManager) { print("--- centralManagerDidUpdateState")

    if central.state == CBManagerState.poweredOn {
        debugPrint("poweredOn")

        let serviceUUIDs:[AnyObject] = [serviceCBUUID_READ]
        let lastPeripherals = centralManager.retrieveConnectedPeripherals(withServices: serviceUUIDs as! [CBUUID])

        if lastPeripherals.count > 0{
            let device = lastPeripherals.last! as CBPeripheral;
            deviceX = device;
            centralManager.connect(deviceX, options: nil)
            if(device.state == .disconnected){
                self.alertShowing(msg: "Device is disconnected restart the device and connect again.")
            }
        }
        else {
            centralManager.scanForPeripherals(withServices: nil, options: nil)
        }

        debugPrint(lastPeripherals)
    } else if(central.state == CBManagerState.poweredOff) {
        self.alertShowing(msg: "Make sure that your bluetooth is turned on.")
    }
    else if(central.state == CBManagerState.unsupported) {
        self.alertShowing(msg: "This device is unsupported.")
    }else{
        self.alertShowing(msg: "Try again after restarting the device.")
    }
}

// DidDiscoverPeripheral

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
    if(peripheral.name == "Peripheral Observer") || (peripheral.name == "BLE Device"){
        if let services = peripheral.services{
            for service in services{
                debugPrint(service)
            }
        }
        debugPrint("advertisementData :\(advertisementData)")
        deviceX = peripheral
        peripheral.delegate = self
        centralManager.stopScan()
        centralManager.connect(peripheral)
    }
}

// inside didConnect

func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
    debugPrint("Connected")
    var i = 0
    timerForService = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { (timer) in
        if(self.service_Read == nil) && (self.service_Write == nil){
            i += 1

            if(i%2 == 1){
                debugPrint("loop1")
                peripheral.discoverServices(nil)
                self.deviceX!.discoverServices(nil)
            }else if( i&2 == 0){
                debugPrint("loop0")
                self.deviceX!.discoverServices([self.serviceCBUUID_READ, self.serviceCBUUID_Write])
            }

        }else{
            self.timerForService.invalidate()
        }
    })
}

Solution

  • after long search I found the problem which is on the device side, we fixed the issue by increase heap memory of device CC2640R2F and set the ATT MTU size, after that we manage to receive service, Approx 6 or 7 time connect disconnect with the peripheral core bluetooth can't find service not even the device. the final solution for this to increase the buffer size of peripheral device.

    https://github.com/NordicPlayground/ANT-Shared-Channel-Demo/issues/1 https://forums.developer.apple.com/thread/73871