-1

I am trying to continually iterate through my write/read requests to get live data from the OBD-II bus on my vehicle. I am using CoreBluetooth in Swift to connect and communicate with my OBD-II BLE adapter.

My goal is to constantly update the live data for 4 different sensors while the user is in the LiveDataView() and then stop asking if they leave the view, or the app is set to background/is closed.

I first tried to write requests without response using the built in function below. This is supposed to notify the manager when the adapter is ready to receive write requests.

func peripheralIsReady(toSendWriteWithoutResponse peripheral: CBPeripheral) {
    for item in commands {
        self.write(value: item, characteristic: self.obdSensorCharacteristic!)
        self.read(characteristic: self.obdSensorCharacteristic!)
        print("Sent item \(String(data:item, encoding:.utf8)!) to \(String(describing: obdSensorCharacteristic))\n in the new peripheralIsReady()!")
        
    }
}

This did not work. It just bombarded the adapter with write requests.

Then I tried a timer. I noticed that as I repetitively ask for PID "0105" (Engine Coolant Temperature), everything seemed to work in the debug console.. but it wasn't actually updating my engine coolant temp in the LiveDataView().

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
    for characteristic in service.characteristics ?? [] {
        if (String(describing: characteristic.uuid)) == "BEF8D6C9-9C21-4C9E-B632-BD58C1009F9F" {
            obdSensorCharacteristic = characteristic
            peripheral.setNotifyValue(true, for: characteristic)
            print("Found \(characteristic), waiting on values:")
            print("Characteristic Value: \(String(describing: characteristic.value))")
            
            if stopData {
                Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { timer in
                    self.requestData()
                    
                    if self.stopData == false {
                        timer.invalidate()
                    }
                })
            }
        }
    }
}

func requestData() {
    if ((obdSensorPeripheral?.canSendWriteWithoutResponse) != nil && sendData) {

        if sendData {
            self.write(value: commands[0], characteristic: self.obdSensorCharacteristic!)
            self.read(characteristic: self.obdSensorCharacteristic!)
            
        }
    }
}

Is there a better way to continually write/read information from the OBD-II BLE device in CoreBluetooth than using a timer? Is there a better way to control write/reads to the peripheral characteristic?

bsquires
  • 1
  • 3
  • Please see [ask] including the section in including code as text and not images – jnpdx Aug 01 '23 at 21:27
  • I tried to edit the question down and fixed the code that I was sharing. Is there anything else I need to do? Some of the details I originally had were showing what I have tried, leading to implementing the timer. – bsquires Aug 02 '23 at 16:18

1 Answers1

0

I think you need to set up the time interval in the device to get it to send you the data. you might want to check out some information on how BLE works.