IOS Core Bluetooth : Writing NSData for Characteristic

后端 未结 5 969
花落未央
花落未央 2021-02-11 01:48

I am using the following code to write the 0xDE value for a Bluetooth Caracteristic (Reset Device) using the IOS Core Bluetooth :

...
NSData *bytes = [@\"0xDE\"          


        
5条回答
  •  心在旅途
    2021-02-11 02:20

    The answer by bensarz is almost correct. Except one thing: you shouldn't use sizeof(int) as the length for NSData. The size of int is 4 or 8 bytes (depending on the architecture). As you want to send 1 byte, use uint8_t or Byte instead:

    uint8_t byteToWrite = 0xDE;
    NSData *data = [[NSData alloc] initWithBytes:&byteToWrite length:sizeof(&byteToWrite)];
    [peripheral writeValue:data
         forCharacteristic:characteristic
                      type:CBCharacteristicWriteWithResponse];
    

    Of courser you could also use int as the variable's type, but you have to initialize NSData with the length of 1.

提交回复
热议问题