0

我已经搜索了这个网站和其他网站,尝试了许多不同的建议,但我现在似乎被我的试错法困住了。这是我在我的 Android HTC Desire X (Jelly Bean) 上尝试以下操作的简单代码,以便连接到我的 OBDII 蓝牙适配器(称为“超级 OBD”):

1) 使用来自http://developer.android.com/guide/topics/connectivity/bluetooth.html的示例

public class BTConnector {
private final UUID MY_UUID = UUID.randomUUID();  // our applications UUID for BT
    // but recommended by <http://www.socialledge.com/sjsu/index.php?title=F12:_OBD-II_Android_Monitor>
    // UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");  

public void setupBluetoothCommunication() {
   BluetoothAdapter mBluetoothAdapter = null;
   BluetoothDevice mBluetoothDevice = null;

   // Step_1: Get BluetoothAdapter
   mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
   if (mBluetoothAdapter == null) {
     // Device does not support Bluetooth
   }
   // Step_2: Enable Bluetooth
   if (!mBluetoothAdapter.isEnabled()) {
      // Request the default Android Settings dialog to start for enabling BT
      // done manually for now
   }
   // Step_3: Find our to be paired device (e.g. OBDII device)
   Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
   if (pairedDevices.size() > 0) {
      for (BluetoothDevice device : pairedDevices) {
         if (device.getName().compareToIgnoreCase("OBDII") == 0) {
            mBluetoothDevice = device; // Found our device
         }
      }
   }

  // 3b. OPTIONAL: Discover devices
  // Step_4: Connecting to the device
  BluetoothSocket mmSocket = null;
   // 1) Using suggested Bluetooth socket -> is not workin
   // mmSocket = mBluetoothDevice.createRfcommSocketToServiceRecord(MY_UUID);
   // mmSocket.connect();
   // UUID.fromString("00001101-0000-1000-8000-00805F9B34FB") causes IOException: Unable to start Service Discovery
   // UUID.randomUUID() causes IOException: Service discovery failed

   // 2) Using suggestion from <http://stackoverflow.com/questions/12384833/android-bluetooth-software-caused-connection-abort> can possibly connect:
  Method m = null;
  m = mBluetoothDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
  mmSocket = (BluetoothSocket) m.invoke(mBluetoothDevice, 1);

  OutputStream tmpOut = null; 
  // Get the input and output streams, using temp objects because member streams are final
  tmpOut = mmSocket.getOutputStream();
  OutputStream mmOutStream = tmpOut;

  // Write out data      
  String ati_command = "ATZ" + "\r";  // also tried: "ATZ\\r", "ATSP0\r" , "ATI\r", or bytes: speed = "010D\r", temp = "0105\r"
  mmOutStream.write(ati_command.getBytes());  // THROWS IOEXCEPTION "Software Caused Connection Abort"
     ...
  }
  ...
}

我尝试了不同的安卓软件:

这些信息有提示吗?我真的很期待一些提示。

非常感谢您!

4

0 回答 0