Tesla 开发者 API 指南:BLE 发送车辆命令

前言

特斯拉提供两种与汽车通信的方式。一种是使用 API 通过互联网,另一种是使用 BLE 连接。BLE 连接有助于锁定和解锁汽车的最重要部分之一,即使汽车和手机或任何其他兼容 BLE 的钥匙扣上没有网络连接。这在两种主要用例中很有帮助,即使在互联网非常不稳定的情况下也是如此。

  1. 离开汽车时锁好车
  2. 当你走近汽车时解锁汽车

汽车中低功耗蓝牙 (BLE) 的使用场景不仅限于锁定和解锁车辆。大多数其他命令也可以通过 BLE 直接发送到汽车,而无需完全通过互联网路由命令。但是,需要考虑的一件事是 BLE 在汽车一定距离内的操作范围。

BLE 功能的代码库可在此处找到 --https://github.com/teslamotors/vehicle-command.git

go 环境搭建自行搜索教程

注意:本工具只能运行于 mac 或者 linux, win下不支持。

1. 克隆项目到本地

https://github.com/teslamotors/vehicle-command.git

2. 项目根目录下执行命令

go get ./...
go build ./...
go install ./...
cd cmd
cd tesla-control 
go build 

3. 生成密钥

生成私钥

openssl ecparam -genkey -name prime256v1 -noout > private.pem

生成公钥

openssl ec -in private.pem -pubout > public.pem

3. 添加汽车公钥

vin码就是车架识别号,类似 7ABCGDEE123ABC555。 在前玻璃地下那里能看到,手机app也能找到。以下用 {youvin} 代替,实际替换为你自己的字符串,{ } 这两个符号也去掉。

./tesla-control -vin {youvin} -ble add-key-request public.pem owner cloud_key

发出命令后,成功的请求将返回以下响应。

向 {youvin} 发送添加密钥请求。通过在中央控制台上轻触 NFC 卡进行确认。

当 NFC 卡在特斯拉汽车的中央控制台上触碰时,汽车显示控制台上会出现一条消息。

特斯拉触摸屏显示一个确认按钮,用于将公钥添加到汽车。

添加钥匙后,它将出现在钥匙列表中,如果您的手机上安装了特斯拉应用程序,也会收到通知。

列出汽车配对的所有钥匙

./tesla-control -vin {youvin} -ble -debug list-keys 

4. 解锁/上锁

./tesla-control -ble -vin {youvin} -key-name private.pem -key-file private.pem unlock
./tesla-control -ble -vin {youvin} -key-name private.pem -key-file private.pem lock

6. 后备箱:开,关,开关切换

./tesla-control -ble -vin {youvin} -key-name private.pem -key-file private.pem trunk-open
./tesla-control -ble -vin {youvin} -key-name private.pem -key-file private.pem trunk-close
./tesla-control -ble -vin {youvin} -key-name private.pem -key-file private.pem trunk-move

7. 前备箱开

./tesla-control -ble -vin {youvin} -key-name private.pem -key-file private.pem frunk-open

8. 闪灯

./tesla-control -ble -vin {youvin} -key-name private.pem -key-file private.pem flash-lights

9. 命令帮助

./tesla-control -h

Usage: ./tesla-control [OPTION...] COMMAND [ARG...]

Run ./tesla-control help COMMAND for more information. Valid COMMANDs are listed below.

 * Commands sent to a vehicle over the internet require a VIN and a token.
 * Commands sent to a vehicle over BLE require a VIN.
 * Account-management commands require a token.

Available OPTIONs:
  -ble
    	Force BLE connection even if OAuth environment variables are defined
  -command-timeout duration
    	Set timeout for commands sent to the vehicle. (default 5s)
  -connect-timeout duration
    	Set timeout for establishing initial connection. (default 20s)
  -debug
    	Enable verbose debugging messages
  -domain value
    	Domains to connect to (can be repeated; omit for all)
  -key-file file
    	A file containing private key. Defaults to $TESLA_KEY_FILE.
  -key-name name
    	System keyring name for private key. Defaults to $TESLA_KEY_NAME.
  -keyring-debug
    	Enable keyring debug logging
  -keyring-file-dir directory
    	keyring directory for file-backed keyring types (default "~/.tesla_keys")
  -keyring-type type
    	Keyring type (file|keychain|pass). Defaults to $TESLA_KEYRING_TYPE.
  -session-cache file
    	Load session info cache from file. Defaults to $TESLA_CACHE_FILE.
  -token-file File
    	File containing OAuth token. Defaults to $TESLA_TOKEN_FILE.
  -token-name name
    	System keyring name for OAuth token. Defaults to $TESLA_TOKEN_NAME.
  -vin string
    	Vehicle Identification Number. Defaults to $TESLA_VIN.

Available COMMANDs:
  add-key                  Add PUBLIC_KEY to vehicle whitelist with ROLE and FORM_FACTOR
  add-key-request          Request NFC-card approval for a enrolling PUBLIC_KEY with ROLE and FORM_FACTOR
  auto-seat-and-climate    Turn on automatic seat heating and HVAC
  autosecure-modelx        Close falcon-wing doors and lock vehicle. Model X only.
  body-controller-state    Fetch limited vehicle state information. Works over BLE when infotainment is asleep.
  charge-port-close        Close charge port
  charge-port-open         Open charge port
  charging-schedule        Schedule charging to MINS minutes after midnight and enable daily scheduling
  charging-schedule-cancel Cancel scheduled charge start
  charging-set-amps        Set charge current to AMPS
  charging-set-limit       Set charge limit to PERCENT
  charging-start           Start charging
  charging-stop            Stop charging
  climate-off              Turn off climate control
  climate-on               Turn on climate control
  climate-set-temp         Set temperature (Celsius)
  drive                    Remote start vehicle
  erase-guest-data         Erase Guest Mode user data
  flash-lights             Flash lights
  frunk-open               Open vehicle frunk. Note that there's no frunk-close command!
  get                      GET an owner API http ENDPOINT. Hostname will be taken from -config.
  honk                     Honk horn
  list-keys                List public keys enrolled on vehicle
  lock                     Lock vehicle
  media-set-volume         Set volume
  media-toggle-playback    Toggle between play/pause
  ping                     Ping vehicle
  post                     POST to ENDPOINT the contents of FILE. Hostname will be taken from -config.
  product-info             Print JSON product info
  remove-key               Remove PUBLIC_KEY from vehicle whitelist
  rename-key               Change the human-readable metadata of PUBLIC_KEY to NAME, MODEL, KIND
  seat-heater              Set seat heater at POSITION to LEVEL
  sentry-mode              Set sentry mode to STATE ('on' or 'off')
  session-info             Retrieve session info for PUBLIC_KEY from DOMAIN
  software-update-cancel   Cancel a pending software update
  software-update-start    Start software update after DELAY
  steering-wheel-heater    Set steering wheel mode to STATE ('on' or 'off')
  tonneau-close            Close Cybertruck tonneau.
  tonneau-open             Open Cybertruck tonneau.
  tonneau-stop             Stop moving Cybertruck tonneau.
  trunk-close              Closes vehicle trunk. Only available on certain vehicle types.
  trunk-move               Toggle trunk open/closed. Closing is only available on certain vehicle types.
  trunk-open               Open vehicle trunk. Note that trunk-close only works on certain vehicle types.
  unlock                   Unlock vehicle
  wake                     Wake up vehicle
  windows-close            Close all windows
  windows-vent             Vent all windows

参考资料

https://shankarkumarasamy.blog/2024/01/28/tesla-developer-api-guide-ble-key-pair-auth-and-vehicle-commands-part-3/

https://github.com/teslamotors/vehicle-command/blob/main/README.md