简体   繁体   中英

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: Ble扫描仪Android应用的屏幕截图

iOS app BLE scanner iOS应用BLE扫描仪

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):

连接设备后搜索服务的代码

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()
        }
    })
}

You are using a wrong way for discovering BLE services in you iOS central. (I'm afraid my response is written in Objective-c, however, it gives you the point and conversion to Swift will be easy)

You have to define a global CBPeriphal instance and use it across your code

@interface CentralManager()
property (nonatomic, strong) CBPeripheral *golablePeripheral;
@end

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI {
    self.golablePeripheral = peripheral;
    self.golablePeripheral.delegate = self;
}

Then, you can start discovering your services once you get the connection's delegate:

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
    [self.golablePeripheral discoverServices: _serviceUUID];
}

Then, you will get the following delegate. It is the time to start discovering characteristics:

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
    for (CBService *service in peripheral.services) {
        if ([_serviceUUID containsObject: service.UUID]) {
            [self.golablePeripheral discoverCharacteristics:_serviceCharacteristics forService:service];
            [self.golablePeripheral discoverCharacteristics:_serviceNotifyCharacteristics forService:service];
        }
    }
}

I strongly recommend you to use BluetoothLEManager for your BLE-related projects. I have used it in my several enterprise projects.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM