Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to writeValue forCharacteristic with response AND read response BLE

I was told by the bluetooth manufacturer that I need to send the following to the peripheral: 'P'(0x50)

How do I do this with Objective-C and how do I get the response?

A code sample would be preferrable.

This almost answers my question, but doesn't give any code samples: peripheral writeValue: forCharacteristic: type: return null error and value

like image 332
Chris Avatar asked Dec 08 '16 17:12

Chris


1 Answers

Here is some sample code: https://code.tutsplus.com/tutorials/ios-7-sdk-core-bluetooth-practical-lesson--mobile-20741

To send data back and forth it is a multistep process

  1. subscribe to the peripheral, service(s) and characteristic(s) as appropriate. The linked walkthrough should help for that. Keep a reference to the peripheral and the characteristic.
  2. call the function [peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse] For the data you can create it using a hex-string to NSData method (https://opensource.apple.com/source/Security/Security-55471.14.18/libsecurity_transform/NSData+HexString.m).
  3. wait for the callback - (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error once you get that callback you can read the peripheral with [_peripheral readValueForCharacteristic:characteristic]
  4. then wait for the callback - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error at that point you can look at characteristic.value to see the value.
like image 175
Jon Rose Avatar answered Nov 10 '22 02:11

Jon Rose