Home Articles

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

Asked
Viewed 760 times
0

产品编号:CC2640R2F

工具/软件:Code Composer Studio

我正在研究一个项目,在我的项目中,CC2640R2F正在进行广告和扫描 . 我正在使用自定义BLE服务从应用程序(Android或iOS)接收数据到CC2640R2F . 每当我在Android中使用BLE扫描仪连接设备时,我都能获得所有服务和所有特性,而且我也能够毫无问题地发送和接收数据 . 这也适用于我的自定义Android应用程序 .

但每当我在iOS应用程序中将CC2640R2F设备与BLE扫描仪连接时,设备就会连接,但我没有在应用程序中获得任何服务或特性 . 我们开发的自定义ios应用程序也是如此 . 为什么会这样?如果我在Android上获得所有内容,那么这也应该适用于iOS .

BLE scanner Android应用程序的屏幕截图:
Screenshot of Ble scanner Android app

iOS应用程序BLE扫描仪
iOS app BLE scanner

用于检查蓝牙是否已打开并扫描外围设备的代码:[![用于检查蓝牙是否已打开并扫描外围设备的代码] [3]] [3]

如果找到外围设备则连接到设备的代码:[![如果找到外围设备则连接到设备的代码] [4]] [4]

设备连接后搜索服务的代码(如果找到服务则会中断计时器(),否则再次搜索服务):

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

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

2 Answers

  • 1

    您正在使用错误的方式在iOS中心发现BLE服务 . (我担心我的回答是用Objective-c编写的,但是,它给你的意思,转换到Swift很容易)

    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;
    }
    

    然后,您可以在获得连接的委托后开始发现您的服务:

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

    然后,您将获得以下委托 . 现在是开始发现特征的时候了:

    - (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];
            }
        }
    }
    

    我强烈建议您将BluetoothLEManager用于与BLE相关的项目 . 我在我的几个企业项目中使用过它 .

  • 0

    经过长时间的搜索,我发现了设备端的问题,我们通过增加设备CC2640R2F的堆内存并设置ATT MTU大小来解决问题,之后我们设法接收服务,大约6或7次连接断开与外围设备核心蓝牙连设备都找不到服务 . 最终的解决方案是增加外围设备的缓冲区大小 .

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

Related