Search code examples
iosiphonebluetoothbluetooth-lowenergycore-bluetooth

iOS app that collects data from "Accu-Chek Aviva Connect" BG meter. (Bluetooth Low Energy)


I'm trying to create an iOS app that will collect data from Accu-Chek Aviva Connect.

To pair with BG meter I send a write request to Record Access Control Point Characteristic:

- (void)peripheral:(CBPeripheral *)aPeripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {

    if ([service.UUID isEqual:[CBUUID UUIDWithString:@"1808"]]) {
        for (CBCharacteristic *aChar in service.characteristics) {

            // Read Glucose Measurement...

            // Read Glucose Measurement Context...

            // Read Glucose Feature...

            // Read Date Time...

            // Read Record Access Control Point
            if ([aChar.UUID isEqual:[CBUUID UUIDWithString:@"2A52"]]) {
                [aPeripheral readValueForCharacteristic:aChar];
                [aPeripheral setNotifyValue:YES forCharacteristic:aChar];

                uint8_t bytes[] = {0x04, 0x01, 0x00};
                NSData *data = [NSData dataWithBytes:(void*)&bytes length:sizeof(bytes)];

                [aPeripheral writeValue:data forCharacteristic:aChar type:CBCharacteristicWriteWithResponse];
            }

        }
    }
}

And on iPhone I see a UIAlert with a field to enter the security code. And I'm able to pair iPhone with Accu-Chek Aviva Connect. But the next time I send some write request to Record Access Control Poin Characteristic (after device disconnected) I get "Error: Authentication is insufficient" and no UIAlert on iPhone.

I have a feeling that I'm doing everything wrong.

I want to read records from bluetooth device's log. AFAIU I write bytes to Read Record Access Control Point ([aPeripheral writeValue:data forCharacteristic:aChar type:CBCharacteristicWriteWithResponse];) and get answer in peripheral:didWriteValueForCharacteristic:error:. But I can't do it because "Error: Authentication is insufficient" stands in my way!


Solution

  • The way to work with Accu-Chek Aviva Connect is to pair it the first time by going to SettingsWirelessPairingPair Device. Then you get a screen with number code and a message: "Enter code on device".

    On iPhone you discover Accu-Chek device and write a value to Read Record Access Control Point characteristic. For example request a Number of records:

    - (void)peripheral:(CBPeripheral *)aPeripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
    
        if ([service.UUID isEqual:[CBUUID UUIDWithString:@"1808"]]) {
            for (CBCharacteristic *aChar in service.characteristics) {
    
                // Read Record Access Control Point
                if ([aChar.UUID isEqual:[CBUUID UUIDWithString:@"2A52"]]) {
                    [aPeripheral readValueForCharacteristic:aChar];
                    [aPeripheral setNotifyValue:YES forCharacteristic:aChar];
    
                    self.readAccessControlPointCharacteristic = aChar;
    
                    NSMutableData *mutableData = [NSMutableData data];
                    uint8_t opCode = 0x04; // Report number of stored records
                    uint8_t operator = 0x01; // All records
    
                    [mutableData appendData:[NSData dataWithBytes:(void*)&opCode length:sizeof(opCode)]];
                    [mutableData appendData:[NSData dataWithBytes:(void*)&operator length:sizeof(operator)]];
    
                    [aPeripheral writeValue:mutableData forCharacteristic:self.readAccessControlPointCharacteristic type:CBCharacteristicWriteWithResponse];
                }
    
            }
        }
    

    This leads to an UIAlertView on iPhone where it asks you to enter a code which is displayed on Accu-Chek screen. When you do it, your iPhone will be successfully paired with Accu-Chek Aviva Connect.

    Now. When you want to read all records from Bluetooth device - you will have to go to My DataData TransferWireless → and if your Accu-Chek device has a lot of pairings, select your iPhone name. Note: iPhone must be scaning for BT devices and connect to a discovered device automatically.

    Bluetooth connection will be established and you can send any requests from your iPhone to Bluetooth device without "Error: Authentication is insufficient"!