jenavarro / obdlogger

Mobile app created using Ionic Framework readin OBD car engine metrics
Apache License 2.0
26 stars 6 forks source link

review #31

Open caglarsarikaya opened 3 years ago

caglarsarikaya commented 3 years ago

hi there, its really good project. i am looking something like this. but i am new about this topics. i amm seeking an app for implament an ble obd app. I found yours if its possible i want to change the protocols from bl to ble. Could you give me some tricks about that. thank you .

dudutaulois commented 3 years ago

Hello, I'm on the same boat here. I'm working on this code to make it work with iOS ble but I'm ending up with some errors to collect device UUID and connect. It seems like Android has a different approach to listen and connect to Bluetooth devices. I'm also new to this so would be great to get some advice on that topic. I just need to basic information like car model, mileage, maybe model year, and services dates. Thank you

jenavarro commented 3 years ago

Hello thankd for reaching out. Can: The project as it is right now is interfacing with the ELM device, which is Bluetooth Serial (a.k.a Bluetooth Classic). That device does not support BLE, since BLE -as far as I know- is more a hardware type rather than just software. If there are BT devices similar to the ELM, then porting this would involve changing from bluetooth-serial to cordova-plugin-ble-central plugin.

Hi Eduardo, same issue there, this will not work with BLE, unless you have a BLE device to connect to. Also, it seems BLE does not support serial protocol, so the communication method should be changed from the current implementation that sends commands and wait for responses. Can you clarify which BLE OBD device are you using?

dudutaulois commented 3 years ago

Hello Javier! Thanks for the response. My device is an ELM327 BLE OBD2 reader. A very cheap one I guess. But it's working fine with other scan apps.

I'm tweaking your code to use ble-central. Not sure how much of the logic will work for BLE and iOS.

Now I'm struggling to modify the listPairedDevices() function. The this.bluetoothSerial.list().then I believe can be replaced with this.ble.scan([],5).subscribe but it is not feeding the pairedList array.

This is how I'm trying to scan for devices:

 listPairedDevices() { 
    this.ble.scan([],5).subscribe((device) => {
      this.pairedList.push(device);
      console.log('Paired List' , this.pairedList);

      this.pairedList.forEach(item => item.isSelected=false);
      this.listToggle = true;
          console.log('Reading default device data: ' +  this.globalconfig.bluetoothDeviceToUse.devicename);
        if (this.globalconfig.bluetoothDeviceToUse==null || this.globalconfig.bluetoothDeviceToUse.devicename== "" ) return;
        let i = this.pairedList.findIndex(item => item.address === this.globalconfig.bluetoothDeviceToUse.address) ;
        if (i>-1) {
          this.pairedList[i].isSelected=true
        }

    }, error => {
      this.showError("Please Enable Bluetooth")
      this.listToggle = false;
    }); 
  }
caglarsarikaya commented 3 years ago

hello, First of all, thanks for your time. I created the circuits of the device. My first purpose makes smaller this device. The way going in ble because ble is really smaller than normal Bluetooth. I changed the protocols in your app but doest work. if you cant give ideas for this probably I will all the codes from the beginning

jenavarro commented 3 years ago

Eduardo, Are you sure your ELM device es BLE? I was not able to find any ELM327 BLE in amazon... maybe it is the classic bluetooth (the one the app works with already). That might explain why you are not getting any callback. In case it is BLE, you can check this page: https://github.com/don/cordova-plugin-ble-central It would be the one to check... Sadly I do not have any BLE device with me to try.

Eduardo, Can, sorry I am not being of much help here without a device. HEre is another page with BLE examples: https://github.com/don/cordova-plugin-ble-central/blob/master/examples/metawear/www/js/index.js

Let me know if you have been succesful or not.

dudutaulois commented 3 years ago

Hey Javier, Thank you for your advice.

I can't tell for sure since my device is completely white-labeled, but I collected info logging and using other apps to figure out. At least it is a Bluetooth 4.0 device since that is the protocol I selected to connect it with Car Scanner app.

Yesterday I manage to connect and collect some data. Although I got how to communicate with my device it is still complicated for me to understand the codes to read the data. I'm literally doing try and error doing tons of builds since the code can only be tested on a real iOS device. I never thought I would be coding at the garage..lol.

This are the steps I taked:

1 - I setup the service and characteristic values:

const OBD_SERVICE = 'FFF0';
const OBD_CHARACTERISTIC = 'FFF1';

2 - Find the device associated with the service:

this.ble.scan([OBD_SERVICE], 5).subscribe(device =>
      this.onDeviceDiscovered(device),
      error => this.scanError(error))

3 - Than I connect to it:

  this.ble.connect(device.id).subscribe(
      peripheral => this.onConnected(peripheral),
      peripheral => this.showAlert('The peripheral unexpectedly disconnected')
    );

4 - Finally I can read some data based on the characterist

   this.ble.read(this.peripheral.id, OBD_SERVICE, OBD_CHARACTERISTIC ).then(
      data => this.onValueChange(data),
      () => this.showAlert('Failed to get value')
    )

I tried to apply this logic in to your code by replacing the parts where you use bluetoothserial with the ble equivalent and changing some parameters but I always end up without actually get the device in the list to be selected.

So today I will try to collect any useful data using my logic above. I debug the connection with the OBD from the other app and now I have some codes that are very hard for me to understand what they do:

default 16:19:02.843493-0300    bluetoothd  statedump: 0x0001 Primary Service      [ serviceUUID: 0x1800, endHandle: 0x0007, discoveredCharacteristics: none ]
default 16:19:02.844013-0300    bluetoothd  statedump: 0x0010 Primary Service      [ serviceUUID: 0x180A, endHandle: 0x0020, discoveredCharacteristics: all ]
default 16:19:02.844420-0300    bluetoothd  statedump: 0x0011 Characteristic       [ valueUUID: 0x2A29, valueHandle: 0x0012, properties: read, discoveredDescriptors: none ]
default 16:19:02.844716-0300    bluetoothd  statedump: 0x0012 Characteristic Value [ 49 53 53 43 ]
default 16:19:02.844932-0300    bluetoothd  statedump: 0x0013 Characteristic       [ valueUUID: 0x2A24, valueHandle: 0x0014, properties: read, discoveredDescriptors: none ]
default 16:19:02.845227-0300    bluetoothd  statedump: 0x0014 Characteristic Value [ 42 54 35 30 35 30 ]
default 16:19:02.845895-0300    bluetoothd  statedump: 0x0015 Characteristic       [ valueUUID: 0x2A25, valueHandle: 0x0016, properties: read, discoveredDescriptors: none ]
default 16:19:02.846151-0300    bluetoothd  statedump: 0x0016 Characteristic Value [ 30 30 30 30 ]
default 16:19:02.846395-0300    bluetoothd  statedump: 0x0017 Characteristic       [ valueUUID: 0x2A27, valueHandle: 0x0018, properties: read, discoveredDescriptors: none ]
default 16:19:02.846724-0300    bluetoothd  statedump: 0x0018 Characteristic Value [ 35 30 35 30 5F 53 50 50 00 00 00 00 00 ]
default 16:19:02.846956-0300    bluetoothd  statedump: 0x0019 Characteristic       [ valueUUID: 0x2A26, valueHandle: 0x001A, properties: read, discoveredDescriptors: none ]
default 16:19:02.847124-0300    bluetoothd  statedump: 0x001A Characteristic Value [ 32 30 33 30 30 33 30 ]
default 16:19:02.847455-0300    bluetoothd  statedump: 0x001B Characteristic       [ valueUUID: 0x2A28, valueHandle: 0x001C, properties: read, discoveredDescriptors: none ]
default 16:19:02.847747-0300    bluetoothd  statedump: 0x001C Characteristic Value [ 30 30 30 30 ]
default 16:19:02.848448-0300    bluetoothd  statedump: 0x001D Characteristic       [ valueUUID: 0x2A23, valueHandle: 0x001E, properties: read, discoveredDescriptors: none ]
default 16:19:02.848644-0300    bluetoothd  statedump: 0x001E Characteristic Value [ 00 00 00 00 00 00 00 00 ]
default 16:19:02.848815-0300    bluetoothd  statedump: 0x001F Characteristic       [ valueUUID: 0x2A2A, valueHandle: 0x0020, properties: read, discoveredDescriptors: none ]
default 16:19:02.849008-0300    bluetoothd  statedump: 0x0020 Characteristic Value [ 00 01 00 04 00 00 00 00 ]
default 16:19:02.849498-0300    bluetoothd  statedump: 0x0030 Primary Service      [ serviceUUID: 49535343-FE7D-4AE5-8FA9-9FAFD205E455, endHandle: 0x0035, discoveredCharacteristics: all ]
default 16:19:02.849828-0300    bluetoothd  statedump: 0x0031 Characteristic       [ valueUUID: 49535343-6DAA-4D02-ABF6-19569ACA69FE, valueHandle: 0x0032, properties: read write, discoveredDescriptors: none ]
default 16:19:02.849978-0300    bluetoothd  statedump: 0x0033 Characteristic       [ valueUUID: 49535343-ACA3-481C-91EC-D85E28A60318, valueHandle: 0x0034, properties: write notify, discoveredDescriptors: all ]
default 16:19:02.850176-0300    bluetoothd  statedump: 0x0035 Client Configuration [ none ]
default 16:19:02.850433-0300    bluetoothd  statedump: 0x0050 Primary Service      [ serviceUUID: 0xFFF0, endHandle: 0x0055, discoveredCharacteristics: all ]
default 16:19:02.850568-0300    bluetoothd  statedump: 0x0051 Characteristic       [ valueUUID: 0xFFF1, valueHandle: 0x0052, properties: notify indicate, discoveredDescriptors: all ]
default 16:19:02.851007-0300    bluetoothd  statedump: 0x0052 Characteristic Value [ 0D 3E ]
default 16:19:02.851165-0300    bluetoothd  statedump: 0x0053 Client Configuration [ none ]
default 16:19:02.851289-0300    bluetoothd  statedump: 0x0054 Characteristic       [ valueUUID: 0xFFF2, valueHandle: 0x0055, properties: responseless-writes write, discoveredDescriptors: none ]

Do you believe your logic to collect data can be used with the BLE? Where should I start now?

jenavarro commented 3 years ago

Hi, I will read your post later today and reply back. Anyway, there is an OBD Simulator you can use for development, it helps a lot so you do not have to code in your car. I wrote about that one a few years ago (when I was developing an older version of this app using Intel XDK), check it here: https://medium.com/@jnvrro/using-obd-protocol-to-read-live-metrics-from-a-car-engine-9e08ad5b1df9

dudutaulois commented 3 years ago

Thank you. I tried to install (Mac) the OBD sim but it's not compiling. I guess the problem is the gpsd version used (it gives an error about expected arguments) in the OBD sim. I will try to run it on a Windows VM. I will check your article.

jenavarro commented 3 years ago

I tried to install the mac version but it is too old. This is how I run it: on a windows machine you create a virtual COM port (e.g. COM5) mapped to bluetooth. THen you open obdsim mapped to that virtual COM port. Then on your phone, you will see the windows maching showing up as a bluetooth device... you connect to it and then the mobile app on the phone will start sending and receiving messages. With Ionic you can even debug the app running on the phone, connected to the windows system. Takes some effort to make it work but when it does it is like magic

jenavarro commented 3 years ago

In terms of interpreting the received codes from the OBD device, I tried but it was impossible. There is an ObdInfo.js file that does all the encoding/query/decoding. On a side note, I've read an article saying BLE does not support natively the bluetooth serial protocol...but there are workarounds. So might be tricky. Sadly I do not have any BLE device to try.

caglarsarikaya commented 3 years ago

@dudutaulois you should review this

constructor(private ble: BLE,private service: SerialMessageConstructor, private ngZone: NgZone) { this.checkBluetooth(); }

Scan() { console.info("Scaning") this.devices = []; this.ble .scan([], 15) .subscribe((device) => this.onDeviceDiscovered(device)); }

onDeviceDiscovered(device) { this.ngZone.run(() => { this.devices.push(device); window["devices"] = this.devices; }); }

connectToDevice(id) { alert(id); this.ble.connect(id).subscribe( (peripheralData) => { console.log(peripheralData); localStorage.setItem("device", peripheralData.id); this.ble .startNotification(localStorage.getItem("device"), "ffe0", "ffe1") .subscribe((b) => { console.log("Gelen Cevap", b); (this.statusDevices += "Gelen Mesaj; "), b[0]; }); }, (peripheralData) => { console.log("disconnected"); } ); }

checkBluetooth() { this.ble .enable() .then((h) => {

  })
  .catch((error) => {
    console.log(error);
  });

}