gpt4 book ai didi

ios - 外围设备服务在所有 BLE Scanner iOS 应用程序中显示为零,但在安卓代码中不显示

转载 作者:行者123 更新时间:2023-11-28 14:44:14 24 4
gpt4 key购买 nike

部件号:CC2640R2F

工具/软件:Code Composer Studio

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

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

BLE scanner 安卓应用截图: 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()
}
})
}

最佳答案

您在 iOS Central 中使用错误的方式发现 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;
}

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

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

然后,您将获得以下委托(delegate)。是时候开始发现特征了:

- (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 相关项目。我已经在我的几个企业项目中使用过它。

关于ios - 外围设备服务在所有 BLE Scanner iOS 应用程序中显示为零,但在安卓代码中不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50374075/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com