CoreBluetooth:尝试写入设备 [英] CoreBluetooth: Attempt to write to device

查看:235
本文介绍了CoreBluetooth:尝试写入设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已连接&配对的蓝牙LE手镯(服务ID:FF20),具有2个特征:

I have a connected & paired up Bluetooth LE bracelet (Service ID: FF20), which have 2 characteristics:


  • 0xFF21:无响应写入

  • 0xFF22:通知

现在,我尝试使用以下代码通过CoreBluetooth Framework将数据写入0xFF21:

Now I try to write data via CoreBluetooth Framework to 0xFF21 with the following code:

我在头文件中定义了2个常量:

I defined 2 constants at header file:

#define TRANSFER_SERVICE_UUID @"FF20"
#define TRANSFER_SERVICE_CHARACTERISTIC_UUID @"FF21"

.m

- (void)viewDidLoad {
    [super viewDidLoad];
    _centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}

- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
    if (central.state != CBCentralManagerStatePoweredOn) {
        return;
    }

    if (central.state == CBCentralManagerStatePoweredOn) {
        NSArray* connectedDevices = [_centralManager retrieveConnectedPeripheralsWithServices:@[[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]]];
        for (CBPeripheral *peripheral in connectedDevices) {
            NSLog(@"Device Found. CBPeripheral = %@", peripheral);
            peripheral.delegate = self;
            if(peripheral.services.count == 0) {
                NSLog(@"No service found");
            }
            for (CBService *service in peripheral.services) {
                if(service.characteristics != nil) {
                    for (CBCharacteristic *characteristic in service.characteristics) {
                        if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID]]) {
                            // check notifying ?
                            NSData *data = [@"Testing" dataUsingEncoding:NSUTF8StringEncoding];
                            [peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
                        } else {
                            NSLog(@"Specific Characteristic Not found");
                        }
                    }
                } else {
                    NSLog(@"Characteristic is NULL");
                }
            }
        }
    }
}

日志出现:

Device Found. CBPeripheral = <CBPeripheral: 0x1740f5080, identifier = 4EFF694C-017A-536F-9301-2EB2CC316CBE, name = Bracelet-0366, state = disconnected>

No service found

我错过了什么?

推荐答案

您需要与发现的外围设备建立连接,并致电您的 didConnectPeripheral 委托方法,然后才能发出读/写请求。

You need to issue a connect to the discovered peripheral and get a call to your didConnectPeripheral delegate method before you can issue read/write requests.

达到 poweredOn 状态您应该发出 scanForDevicesWithServices 调用,等待返回给您的委托的调用,然后连接,发现服务和特征,然后可以发出写操作。

When you reach the poweredOn state you should issue a scanForDevicesWithServices call, wait for the call back to your delegate, then connect, discover the service and characteristics, then you can issue a write.

-(void) centralManagerDidUpdateState:(CBCentralManager *)central {
    switch (central.state) {
        case CBCentralManagerStatePoweredOn:
            [central scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]] options:nil];
            break;
    }
}

-(void) centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
    NSLog(@"Discovered peripheral %@ (%@)",peripheral.name,peripheral.identifier.UUIDString);

    if (self.connectedPeripheral == nil) {
        [central connectPeripheral:peripheral options:nil];
    }
}

-(void) centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
    self.connectedPeripheral=peripheral;
    NSLog(@"Connected to %@(%@)",peripheral.name,peripheral.identifier.UUIDString);
    peripheral.delegate=self;
    [peripheral discoverServices:@[[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]];
}

-(void) peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
    for (CBService *service in peripheral.services) {
        NSLog(@"Discovered service %@",service.description);
        [peripheral discoverCharacteristics:nil forService:service];
    }
}

-(void) peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
    for (CBCharacteristic *characteristic in service.characteristics ) {
        NSLog(@"Discovered characteristic %@(%@)",characteristic.description,characteristic.UUID.UUIDString);
        if ([characteristic.UUID.UUIDString isEqualToString:TRANSFER_SERVICE_CHARACTERISTIC_UUID) {
            NSData *data = [@"Testing" dataUsingEncoding:NSUTF8StringEncoding];
            [peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithoutResponse];
        }
    }
}

请注意,与您以前的问题,该特征仅支持写而无响应,因此您不能使用 CBCharacteristicWriteWithResponse

Note, that from your previous question the characteristic only supports write without response, so you can't use CBCharacteristicWriteWithResponse

这篇关于CoreBluetooth:尝试写入设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆