Send Data from CentralManager to PeripheralManager using CoreBluetooth (BLE)

老子叫甜甜 提交于 2019-12-10 10:19:02

问题


I have my app running on two iOS device (both have 6.1.4) where one device acts as the Central and one acts as a Peripheral.

I have been successful in getting notifications (characteristics set up to notify) from the Peripheral over to the Central just fine.

However, I was wanting to write a value from the Central to the Peripheral to a specific writable characteristic but that always fails.

The peripheral:didWriteValueForCharacteristic:error: delegate's method is immediately called where the error description says:

"One or more parameters were invalid"

I have been searching the net for any clues but have yet to come up with what is wrong.

This is how I setup the characteristic on the peripheral side:

[[CBMutableCharacteristic alloc] initWithType:<My CBUUID> properties:CBCharacteristicPropertyWrite value:nil permissions:0];

I see that when I discover characteristics on the Central side that my writeable characteristic is there. I do store away a reference at that point that I later try to use when writing my value. The value that I am writing is just a text string that I convert to a NSData like so:

NSString *payloadMessage = @"Hello";
NSData *payload = [payloadMessage dataUsingEncoding:NSUTF8StringEncoding];
[peripheral writeValue:payload forCharacteristic:<myCharacteristic> type:CBCharacteristicWriteWithResponse];

The only odd thing I can see is that my characteristic's properties is set to 138. It looks like it is both writeable as well as having extended properties. Not sure why that is automatically added by the CoreBluetooth framework. Don't know if it matters.

It seems like something is wrong with the characteristic or the data that I am trying to send but for the life of me I can not figure it out. If anyone out there has any suggestions or ideas I would appreciate the help


回答1:


I did receive an answer in the Bluetooh-dev list that solved my problem.

I didn't create my characteristic with the right permissions.

This is what I originally had:

[[CBMutableCharacteristic alloc] initWithType:<CBUUID> properties:CBCharacteristicPropertyWrite value:nil permissions:0];

This is what it should be:

[[CBMutableCharacteristic alloc] initWithType:<CBUUID> properties:CBCharacteristicPropertyWrite value:nil permissions: **CBAttributePermissionsWriteable**];  



回答2:


I got the same error when I accidentally provided an array of strings instead of CBUUIDs in discoverCharacteristics:forService:.

Incorrect:

[peripheral discoverCharacteristics:@[NOTIFY_CHARACTERISTIC_UUID, WRITE_WO_RESPONSE_CHARACTERISTIC_UUID]
                         forService:service];

Correct:

[peripheral discoverCharacteristics:@[[CBUUID UUIDWithString:NOTIFY_CHARACTERISTIC_UUID],
                                      [CBUUID UUIDWithString:WRITE_WO_RESPONSE_CHARACTERISTIC_UUID]]
                         forService:service]; 



回答3:


  1. Create characteristic

    self.writeableCharacteristic = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:CBUUIDWriteableCharacteristicCodeString] properties:CBCharacteristicPropertyWrite value:nil permissions:CBAttributePermissionsWriteable];
    
  2. Add it to service and to PeripheralManager

    CBMutableService *gameTransferService = [[CBMutableService alloc] initWithType:[CBUUID UUIDWithString:CBUUIDServiceCodeString] primary:YES];
    gameTransferService.characteristics = @[self.notifierCharacteristic, self.writeableCharacteristic];
    [self.peripheralRoleManager addService:gameTransferService];
    
  3. Make sure u are connected to another device

    [self.service connectToPerephiral:self.dataSource[indexPath.row]];
    
  4. Found u characteristic in connected peripheral and write data

    for(CBService *service in self.discoveredPeripheral.services) {
            if([service.UUID isEqual:[CBUUID UUIDWithString:<CBUUIDServiceCodeString>]]) {
                for(CBCharacteristic *charac in service.characteristics) {
                    if([charac.UUID isEqual:[CBUUID UUIDWithString:<CBUUIDWriteableCharacteristicCodeString>]]) {
                        [self.discoveredPeripheral writeValue:data forCharacteristic:charac type:CBCharacteristicWriteWithResponse];
                }
            }
        }
    }
    
  5. Add delegate method - (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteRequests:(NSArray<CBATTRequest *> *)requests and found u'r characteristic like

    for (CBATTRequest *request in requests) {
        if ([request.characteristic.UUID isEqual:[CBUUID UUIDWithString:CBUUIDNotifierCharacteristicCodeString]]) {
    #ifdef DEBUG
            NSString *dataString = [[NSString alloc] initWithData:request.value encoding:NSUTF8StringEncoding];
            NSLog(@"Received from manager - %@", dataString);
    #endif
            if (self.delegate && [self.delegate respondsToSelector:@selector(BLEServiceDidReceiveData:peripheral:service:)]) {
                [self.delegate BLEServiceDidReceiveData:request.value peripheral:nil service:self];
            }
        }
    }
    


来源:https://stackoverflow.com/questions/16602342/send-data-from-centralmanager-to-peripheralmanager-using-corebluetooth-ble

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!