问题
I am using OBD2 device to get data from different vehicles. Communication between the app and OBD2 is done using Bluetooth LE. When I use an OBD2 device I get the service UUID = FFE0 and characteristic UUID = FFE1. I can use this characteristic to write PID and I am getting data from my car without any problem. But when I use a different OBD2 I get service UUID = FFF0 and two characteristics with UUID FFE1(with property .write) and FFE2(with property .read). When I try to write a PID to the characteristic with write property I am getting error as
Error Domain=CBATTErrorDomain Code=10 "The attribute could not be found."
What will be going wrong?
回答1:
Although BLE itself is standardized, there is no official standard for OBD2 services in BLE. In order to work with multiple adapters, your application should incorporate a proper device scan.
For this to work, you probe for devices, connect to everything that you find, then launch a service scan and iterate through all services and their characteristics.
If you find a characteristic that is writable, then this will be the channel where you can write to. If you find a characteristic that is readable, then this will be channel where to read from. This will not necessarily lead to two characteristics, some adapters use the same for both reading and writing.
Use the first service that meet your expectations, then try to send an ATZ\r\n
(or whatever you support on transport level) and see whether this is a valid OBD2 adapter response.
That way you don't need to know the service UUID up front and can make your application work with most BLE adapters.
(Of course, you can remember the service UUID for next time, so you don't have to probe again unless the user tells you to.)
回答2:
As you already wrote "when I use a different OBD2 [device]"... different devices = different implementations of the BLE protocol.
So it depends on the ODB2 device you use. There is a wide range of quality on the market. Some may only read values, some may write them too. Some are certified for the base markets, some not. And to make it even worse, there are different implementations of the ODB2 protocol standard too (over car manufacturers and sometimes even car types). Only the base values defined in the standard are mostly identical. The car manufacturers implemented it for car maintenance only, they do not consider other use cases like yours.
Also note: Newer electro-cars, like a Tesla, even have no ODB2 ports any more.
I'm not an expert with ODB2 and ODB2 devices, but I know some problems from discussions with two colleagues that investigated several ODB2 BLE Devices and different cars for our business use cases. We changed our scope to avoid ODB2 devices and opening Pandora box.
Please share some code. Maybe you didn't requested the characteristic from CPPeripheral with discoverCharacteristics correctly.
来源:https://stackoverflow.com/questions/44720593/trouble-reading-data-from-obd-2