SwiftNinja95
SwiftNinja95

Reputation: 157

Data sent to peripheral not working using Corebluetooth

I am trying to sent data to a peripheral using CoreBluetooth , the frame that I sent to device is as follows after encryption :

[12, 25, 26, 178, 186, 127, 127, 133, 144, 93, 144, 188, 104, 52, 119, 160]

can I send the command in signed array. Corresponding command from android is as follows :

[12, 25, 26, -78, -70, 127, 127, -123, -112, 93, -112, -68, 104, 52, 119, -96]

The code which I use to send data is as follows:

let command : [UInt8] = [0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00]
guard let commandEncrpt = AESEncrypt(data: command) else{
      return
 }
let resultCommand = Data(commandEncrpt)
self.writeCommand(command: resultCommand)

Write command function is as follows :

internal func writeCommand(command:Data) {
    
    if let chara = self.deviceModel.writeChara {
        print("writing started")
        self.deviceModel.peripheral.writeValue(command, for: chara, type: .withResponse)
    }
    
}

Encryption function is as follows :

func AESEncrypt(data: [UInt8]) -> [UInt8]?  {
    
    do {
        let key : [UInt8] = [
            64,215,215,156,115,47,26,19,115,127,109,10,195,65,36,185
        ]
        let encrypted = try AES(key: key, blockMode: ECB(),padding: .noPadding).encrypt(data)
        print(encrypted.toHexString())
        return encrypted
    } catch {
        print(error)
    }
    return nil
    
}

The key provided is sample doesn't reflect the actual one. I am also using CryptoSwift library for this.

Upvotes: 0

Views: 51

Answers (1)

Rajeev Udayan
Rajeev Udayan

Reputation: 11

Both the answers from android and iOS are correct. In Android, it is showing signed 2's complement result. In iOS, it is showing corresponding decimal values.

For Eg: - b2 The decimal value of b2 is 186 2's complement of b2 is -70

Upvotes: 1

Related Questions