从arduino接收android并显示图形
嗨,我的问题是当我运行程序时,我的应用程序崩溃了。下面发现了一个错误。 下面是我的代码列表 蓝牙代码如下所示从arduino接收android并显示图形,android,graph,bluetooth,arduino,Android,Graph,Bluetooth,Arduino,嗨,我的问题是当我运行程序时,我的应用程序崩溃了。下面发现了一个错误。 下面是我的代码列表 蓝牙代码如下所示 package com.example.tut; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Set; import java.util.UUID; import andro
package com.example.tut;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Set;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class bluetooth extends Activity implements OnItemClickListener {
public static void disconnect(){
if (connectedThread != null){
connectedThread.cancel();
connectedThread = null;
}
}
public static void gethandler(Handler handler){
mHandler = handler;
}
static Handler mHandler = new Handler();
static ConnectedThread connectedThread;
public static final UUID MY_UUID
UUID.fromString("00001101-0000-1000-8000-00805F9834FB");
protected static final int SUCCESS_CONNECT=0;
protected static final int MESSAGE_READ = 1;
ListView listview;
ArrayAdapter<String> listAdapter;
static BluetoothAdapter btAdapter;
Set<BluetoothDevice> devicesArray;
ArrayList<String> pairedDevices;
ArrayList<BluetoothDevice> devices;
IntentFilter filter;
BroadcastReceiver receiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bluetooth);
init();
if (btAdapter==null){
Toast.makeText(getApplicationContext(), "No BT detected",
0).show();
finish();
}else{
if(!btAdapter.isEnabled()){
turnOnBT();
}
getPairedDevices();
startDiscovery();
}
}
private void getPairedDevices(){
btAdapter.cancelDiscovery();
btAdapter.startDiscovery();
}
private void turnOnBT(){
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, 1);
}
private void startDiscovery(){
devicesArray = btAdapter.getBondedDevices();
if(devicesArray.size()>0){
for(BluetoothDevice device:devicesArray){
pairedDevices.add(device.getName());
}
}
}
private void init(){
listview = (ListView)findViewById(R.id.ListView);
listview.setOnItemClickListener(this);
listAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, 0);
btAdapter = BluetoothAdapter.getDefaultAdapter();
pairedDevices = new ArrayList<String>();
filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
devices = new ArrayList<BluetoothDevice>();
receiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent){
String action = intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action)){
BluetoothDevice device =
intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
devices.add(device);
String s = "";
for(int a=0; a<pairedDevices.size();a++){
if(device.getName().equals(pairedDevices.get(a))){
s = "(Paired)";
break;
}
}
listAdapter.add(device.getName()+""+s+""+"\n"+device.getAddress());
}else if
(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){
}else if
(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
}else if
(BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)){
if (btAdapter.getState() == btAdapter.STATE_OFF){
turnOnBT();
}
}
}
};
registerReceiver(receiver, filter);
IntentFilter filter = new
IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
registerReceiver(receiver, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(receiver, filter);
filter = new
IntentFilter(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
}
@Override
protected void onPause(){
super.onPause();
unregisterReceiver(receiver);
}
protected void onActivityResult(int requestCode, int resultCode, Intent
data){
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_CANCELED){
Toast.makeText(getApplicationContext(), "Bluetooth must be enabled
to continue", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long
arg3) {
// TODO Auto-generated method stub
if (btAdapter.isDiscovering()){
btAdapter.cancelDiscovery();
}
if (listAdapter.getItem(arg2).contains("(Paired)")){
BluetoothDevice selectedDevice = devices.get(arg2);
ConnectThread connect =
new ConnectThread(selectedDevice);
connect.start();
}else {
Toast.makeText(getApplicationContext(), "device is not paired",
0).show();
}
}
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device) {
// Use a temporary object that is later assigned to mmSocket,
// because mmSocket is final
BluetoothSocket tmp = null;
mmDevice = device;
// Get a BluetoothSocket to connect with the given
BluetoothDevice
try {
// MY_UUID is the app's UUID string, also used by the server
code
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) { }
mmSocket = tmp;
}
public void run() {
// Cancel discovery because it will slow down the connection
btAdapter.cancelDiscovery();
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect();
} catch (IOException connectException) {
// Unable to connect; close the socket and get out
try {
mmSocket.close();
} catch (IOException closeException) { }
return;
}
// Do work to manage the connection (in a separate thread)
mHandler.obtainMessage(SUCCESS_CONNECT,mmSocket ).sendToTarget();
}
/** Will cancel an in-progress connection, and close the socket */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
static class ConnectedThread extends Thread {
private static final int MESSAGE_READ = 0;
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) { }
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte[] buffer; // buffer store for the stream
int bytes; // bytes returned from read()
// Keep listening to the InputStream until an exception occurs
while (true) {
try {
try{
sleep(30);
} catch(InterruptedException e){
e.printStackTrace();
}
buffer = new byte[1024];
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI activity
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
break;
}
}
}
/* Call this from the main activity to send
* data to the remote device */
public void write(String income) {
try {
mmOutStream.write(income.getBytes());
try {
Thread.sleep(20);
}catch(InterruptedException e){
e.printStackTrace();
}
} catch (IOException e) { }
}
/* Call this from the main activity to shutdown the connection */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
}*
布局蓝牙
<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" >
<TextView
android:id="@+id/tvPd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Paired Devices" >
</TextView>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/bConnectNew"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tvPd" >
</ListView>
</RelativeLayout>
在
MainActivity
的init
方法中,替换
graphView.addView(graphView);
与
您不应该向自身添加视图
这应该可以解决问题。请格式化您的代码,说明它在哪里崩溃,并包括堆栈跟踪?先生,我已经编辑了这个问题。我也已经格式化了它,因为我是android编程的初学者,我不知道我在代码中调试了什么,但是当我运行它的时候。并没有错误,它只是在跟踪的顶部崩溃:`stack overflow on call to Landroid/view/view;。getRawLayoutDirection`表示您的代码有一些无止境的递归。那么这意味着什么呢,先生?我将更改哪一行?作为猜测,请查看
setContentView(R.layout.activity\u蓝牙)代码>我不是安卓黑客。也许有一位大师有一个想法。它解决了应用程序运行时的问题,但我没有测试图表,只是测试了连接。我会更新你妈妈,如果图形也工作。妈妈,我能问一个关于编码的问题吗?:)我不懂一些密码,露丝夫人,非常感谢:)做完了,妈妈:)好的,妈妈:)谢谢你的回答:)你是这里的大师之一吗?:不客气。不,我不是。我只知道一点可以帮助你的东西。这个代码的目的是什么?Log.d(“strIncom”,strIncom);如果(strIncom.indexOf('.')==2和&strIncom.indexOf('s')==0{strIncom=strIncom.replace(“s”),“”);如果(isFloatNumber(strIncom)){Series.appendData(new GraphViewData(graph2LastXValue,Double.parseDouble(strIncom)),AutoScrollX);您可以检查并获取有关使用字符串的详细说明。
<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" >
<TextView
android:id="@+id/tvPd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Paired Devices" >
</TextView>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/bConnectNew"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tvPd" >
</ListView>
</RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
android:orientation="horizontal"
android:weightSum="100" >
<LinearLayout
android:id="@+id/Graph"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="15" />
<LinearLayout
android:id="@+id/LL2"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="85"
android:orientation="vertical" >
<TextView
android:id="@+id/tvBluetooth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Bluetooth"
android:textSize="15dp"
android:textColor="@color/white"/>
<Button
android:id="@+id/bConnect"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:text="Connect"
android:textSize="10dp"
android:textColor="@color/white"/>
<Button
android:id="@+id/bDisconnect"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:text="Disconnect"
android:textSize="10dp"
android:textColor="@color/white"/>
<TextView
android:id="@+id/tvControl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Control"
android:textSize="15dp"
android:textColor="@color/white" />
<ToggleButton
android:id="@+id/tbStream"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:checked="false"
android:textOff="Start Stream"
android:textOn="Start Stream"
android:textColor="@color/white"/>
<ToggleButton
android:id="@+id/tbScroll"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:checked="true"
android:textOff="Auto Scroll X"
android:textOn="Auto Scroll X"
android:textColor="@color/white" />
<ToggleButton
android:id="@+id/tbLock"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:checked="true"
android:textOff="Lock Xaxis"
android:textOn="Lock Xaxis"
android:textColor="@color/white" />
<LinearLayout
android:id="@+id/LLX"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:weightSum="100" >
<Button
android:id="@+id/bXminus"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_weight="50"
android:text="-"
android:textSize="12dp"
android:textColor="@color/white"/>
<Button
android:id="@+id/bXplus"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_weight="50"
android:text="+"
android:textSize="12dp"
android:textColor="@color/white"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
#define sensorPin A0
void setup() {
Serial.begin(115200);
}
void loop() {
if(Serial.available()>0){
char re = Serial.read();
switch(re){
case 'E':
start();
break;
}
}
}
void start(){
while(1){
Serial.print('s');
Serial.print(floatMap(analogRead(sensorPin),0,1023,0,5),2);
delay(10);
if(Serial.available()>0){
if (Serial.read()=='Q') return;
}
}
}
float floatMap(float x, float inMin, float inMax, float outMin, float
outMax){
return (x-inMin)*(outMax-outMin)/(inMax-inMin)+outMin;
}
graphView.addView(graphView);
GraphView.addView(graphView);