我想将一个字符串发送到OBD2适配器,以读取一些信息,例如电池电压。所以我写了这个小程序:
public class MainActivity extends Activity {
private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter
.getDefaultAdapter();
private ConnectedThread connected;
private boolean isConnected = false;
// private final BluetoothSocket mmSocket;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// Button ruft die Methode verbindung auf!!!!
public void verbinden(View view) {
verbindung();
Toast.makeText(getApplicationContext(), "Verbunden!", Toast.LENGTH_LONG).show();
}
// Senden Button
public void senden(View view) {
String message;
EditText Message = (EditText) findViewById(R.id.TFKonsole);
message = (Message.getText().toString());
Toast.makeText(getApplicationContext(), "Nachricht gesendet!", Toast.LENGTH_LONG).show();
for (int i = 0; i < message.length(); i++) {
char c = message.charAt(i);
String bereit = Integer.toHexString(c);
System.out.println(bereit);
byte[] send = bereit.getBytes();
connected.write(send);
//connected.write(abschluss);
}
}
// Trennen Butto
public void trennen(View view) {
connected.cancel();
}
// Verbindung wird ausgeführt
private void verbindung() {
String address = "AA:BB:CC:11:22:33"; // Adresse vom Dongle(ITEM)
// 00:0D:18:06:00:00 ADRESSE VOM
// DONGEL (ANDREAS)
// 00:0D:18:3A:67:89
// B8:E8:56:41:74:09marco
// 00:09:DD:42:5B:AA adresse
// bluetoothstick
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
ConnectThread connect = new ConnectThread(device);
connect.start();
// connected.write(null);
}
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device) {
// mBluetoothAdapter.cancelDiscovery();
// Use a temporary object that is later assigned to mmSocket,
// because mmSocket is final
BluetoothSocket tmp = null;
mmDevice = device;
ParcelUuid[] i = device.getUuids();
// Holt sich ein BluetoothSocket, um sich mit dem BluetoothDevice zu
// verbinden
try {
// MY_UUID is the app's UUID string, also used by the server
// code
Method m = device.getClass().getMethod("createRfcommSocket",
new Class[] { int.class });
tmp = (BluetoothSocket) m.invoke(device, 1);
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mmSocket = tmp;
}
public void run() {
// Trenne die Verbindung, weil eine bestehende Verbindung viel
// Speicher in anspruch nimmt
// mBluetoothAdapter.cancelDiscovery();
try {
mBluetoothAdapter.cancelDiscovery();
// Verbinde das Gerät über den Socket. Dies wird geblockt,
// bis die Verbindung erfolgt oder eine Exception fliegt
mmSocket.connect();
isConnected = true;
boolean k = mmSocket.isConnected();
System.out.println(k + " Verbindung erfolgreich");
} catch (IOException connectException) {
connectException.printStackTrace();
// Verbinden unmöglich; Schließe den Socket und beende die App
boolean a = mmSocket.isConnected();
System.out.println(a + " Verbindung fehlgeschlagen a");
try {
mmSocket.close();
} catch (IOException closeException) {
boolean b = mmSocket.isConnected();
System.out.println(b + " Verbindung fehlgeschlagen b");
}
return;
}
connected = new ConnectedThread(mmSocket);
connected.run();
}
/** Will cancel an in-progress connection, and close the socket */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
}
}
}
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte[] buffer = new byte[1024]; // buffer store für den Stream
int bytes; // bytes, die von read() zurückgegeben werden
// Achte auf den InputStream, bis alles übertragen wurde
// oder eine Exception fliegt
while (true) {
try {
// Lese von dem InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI activity
// mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
// .sendToTarget();
// Ausgabe im Textfeld
// setContentView(R.layout.activity_main);
final String sausgabe = new String(buffer);
final TextView ausgabe = (TextView) findViewById(R.id.textView1);
runOnUiThread(new Runnable() {
//Methode, um einen Thread aus einem anderen aufzurufen
@Override
public void run() {
// TODO Auto-generated method stub
ausgabe.setText("\n" + sausgabe);
Log.e("", "Aufruf");
}
// do something on UI thread Update UI
});
} catch (IOException e) {
break;
}
}
}
/* Rufen sie diese Methode auf, um Daten zum Remote Gerät zu senden */
public void write(byte[] bytes) {
try {
mmOutStream.write(bytes);
// connected.write(null);
} catch (IOException e) {
e.printStackTrace();
System.out.println("Senden fehlgeschlagen");
}
}
/* Rufen sie diese Methode auf, um die Verbindung zu trennen */
public void cancel() {
try {
Toast.makeText(getApplicationContext(), "Getrennt!", Toast.LENGTH_LONG).show();
mmSocket.close();
System.out.println("Getrennt");
} catch (IOException e) {
System.out.println("Trennen fehlgeschlagen");
}
}
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.blue_final.MainActivity$PlaceholderFragment" >
<Button
android:id="@+id/BtnVerbinden"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:onClick="verbinden"
android:text="verbinden" />
<EditText
android:id="@+id/TFKonsole"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/BtnVerbinden"
android:layout_alignRight="@+id/BtnSenden"
android:layout_below="@+id/BtnVerbinden"
android:ems="10" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/BtnVerbinden"
android:layout_alignBottom="@+id/BtnVerbinden"
android:layout_alignParentRight="true"
android:onClick="trennen"
android:text="@string/gui_trennen" />
<Button
android:id="@+id/BtnSenden"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/TFKonsole"
android:layout_toLeftOf="@+id/button1"
android:onClick="senden"
android:text="senden" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/TFKonsole" />
</RelativeLayout>
如果我按下发送按钮,我会得到一个? (问号)。我认为我的for循环有问题。加密狗只读取十六进制,但我只能发送字节。 我希望你能帮助我=)
答案 0 :(得分:0)
我认为你的脚环有问题
for (int i = 0; i < message.length(); i++) {
char c = message.charAt(i);
String bereit = Integer.toHexString(c);
System.out.println(bereit);
byte[] send = bereit.getBytes();
connected.write(send);
//connected.write(abschluss);
}
您正在为每个消息位置发送整个消息。
答案 1 :(得分:0)
for (int i = 0; i < message.length(); i++) {
char c = message.charAt(i);
String bereit = Integer.toHexString(c);
System.out.println(bereit);
byte[] send = bereit.getBytes();
connected.write(send);
//connected.write(abschluss);
}
正如您所看到的,每次为每个角色调用您的写入。那封写道:
mmOutStream.write(bytes);
我不确定,但可能每次写入都以\ r结束吗? '\ r'是回车符(cr),实际上是十六进制的0D。
或许看起来你的命令永远不被理解,因为它没有收到cr。也许它会给出超时或其他什么,因为它一直在等待你的cr? 无论如何,尝试在循环后发送一个'\ r'。
for (int i = 0; i < message.length(); i++) {
char c = message.charAt(i);
String bereit = Integer.toHexString(c);
System.out.println(bereit);
byte[] send = bereit.getBytes();
connected.write(send);
}
//Send your '\r' here.
PS:你说你需要用十六进制写入OBD-II加密狗吗?您可能希望以ASCII格式尝试,因为大多数文档都是ASCII格式,ELM327协议理解ASCII格式。 让我知道你的进展!