繁体   English   中英

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

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

零件编号:CC2640R2F

工具/软件:Code Composer Studio

我正在做一个项目,在我的项目中CC2640R2F正在执行广告和扫描。 我正在使用自定义BLE服务从应用程序(Android或iOS)接收数据到CC2640R2F。 每当我将设备与android中的BLE扫描仪连接时,我都会获得所有服务和所有特征,并且我也能够毫无问题地发送和接收数据。 这也可以在我的自定义android应用中使用。

但是,每当我在iOS应用程序中将CC2640R2F设备与BLE扫描仪连接时,该设备都已连接,但应用程序中没有任何服务或特性。 同样的情况也发生在我们开发的自定义ios应用程序中。 为什么会这样呢? 如果我在Android上获得所有东西,那么这也应该在iOS上运行。

BLE扫描仪Android应用的屏幕截图: Ble扫描仪Android应用的屏幕截图

iOS应用BLE扫描仪 iOS应用BLE扫描仪

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

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

连接设备后搜索服务的代码(如果找到服务,则它将中断timer(),否则将再次搜索服务):

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

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

您使用错误的方式在iOS中心中发现BLE服务。 (恐怕我的回答是用Objective-c编写的,但是,这很重要,并且转换为Swift很容易)

您必须定义一个全局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;
}

然后,一旦获得连接的委托,就可以开始发现服务:

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

我强烈建议您对与BLE相关的项目使用BluetoothLEManager 我已经在几个企业项目中使用过它。

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

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