简体   繁体   English

外围设备服务在所有BLE Scanner iOS App中显示为nil,但在android代码中不显示

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

Part Number: CC2640R2F 零件编号:CC2640R2F

Tool/software: Code Composer Studio 工具/软件:Code Composer Studio

I am working on a project and in my project the CC2640R2F is performing advertising and scanning. 我正在做一个项目,在我的项目中CC2640R2F正在执行广告和扫描。 I am using a custom BLE service to receive data from an app (Android or iOS) to CC2640R2F. 我正在使用自定义BLE服务从应用程序(Android或iOS)接收数据到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. 每当我将设备与android中的BLE扫描仪连接时,我都会获得所有服务和所有特征,并且我也能够毫无问题地发送和接收数据。 This is also working in my custom android app. 这也可以在我的自定义android应用中使用。

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. 但是,每当我在iOS应用程序中将CC2640R2F设备与BLE扫描仪连接时,该设备都已连接,但应用程序中没有任何服务或特性。 The same situation is with our developed custom ios app also. 同样的情况也发生在我们开发的自定义ios应用程序中。 Why is this happening? 为什么会这样呢? If I am getting all the things on Android, then this should also work iOS. 如果我在Android上获得所有东西,那么这也应该在iOS上运行。

Screenshot of BLE scanner Android app: BLE扫描仪Android应用的屏幕截图: Ble扫描仪Android应用的屏幕截图

iOS app BLE scanner iOS应用BLE扫描仪 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] 用于检查蓝牙是否已打开电源并扫描外围设备的代码:[![用于检查蓝牙是否已打开电源并扫描外围设备的代码] [3]] [3]

Code to connect to device if peripheral is found: [![Code to connect to device if peripheral is found][4]][4] 如果找到外围设备则连接到设备的代码:[![如果找到外围设备则连接到设备的代码] [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): 连接设备后搜索服务的代码(如果找到服务,则它将中断timer(),否则将再次搜索服务):

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

func centralManagerDidUpdateState(_ central: CBCentralManager) { print("--- centralManagerDidUpdateState") 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 // 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 //在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. 您使用错误的方式在iOS中心中发现BLE服务。 (I'm afraid my response is written in Objective-c, however, it gives you the point and conversion to Swift will be easy) (恐怕我的回答是用Objective-c编写的,但是,这很重要,并且转换为Swift很容易)

You have to define a global CBPeriphal instance and use it across your code 您必须定义一个全局CBPeriphal实例并在代码中使用它

@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. 我强烈建议您对与BLE相关的项目使用BluetoothLEManager 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. 经过长时间的搜索后,我发现了设备方面的问题,我们通过增加设备CC2640R2F的堆内存来解决该问题,并设置了ATT MTU大小,此后我们设法接收服务,大约6或7次与外围设备连接断开连接核心蓝牙连设备都找不到服务。 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 https://github.com/NordicPlayground/ANT-Shared-Channel-Demo/issues/1 https://forums.developer.apple.com/thread/73871

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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