簡體   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