您好我的问题是当我运行该程序时,我的应用程序崩溃了。并且下面发现了一个错误。 这是我下面的代码列表
BLUETOOTH CODE看起来像这样
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) { }
}
}
}*
主要活动代码如下所示:
package com.example.tut;
import android.app.Activity;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.ToggleButton;
import com.example.tut.bluetooth.ConnectedThread;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.GraphView.GraphViewData;
import com.jjoe64.graphview.GraphView.LegendAlign;
import com.jjoe64.graphview.GraphViewSeries;
import com.jjoe64.graphview.GraphViewSeries.GraphViewStyle;
import com.jjoe64.graphview.LineGraphView;
public class MainActivity extends Activity implements
View.OnClickListener {
@Override
public void onBackPressed(){
if (bluetooth.connectedThread !=null)
bluetooth.connectedThread.write("Q");
super.onBackPressed();
}
Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg){
super.handleMessage(msg);
switch(msg.what){
case bluetooth.SUCCESS_CONNECT:
bluetooth.connectedThread = new
bluetooth.ConnectedThread((BluetoothSocket)msg.obj);
Toast.makeText(getApplicationContext(),
"Connected", 0).show();
String s ="Successfully connected";
bluetooth.connectedThread.start();
break;
case bluetooth.MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
String strIncom = new String(readBuf, 0 ,5);
if (strIncom.indexOf('s')==0 && strIncom.indexOf('.')==2){
strIncom = strIncom.replace("s","");
if (isFloatNumber(strIncom)){
Series.appendData(new
GraphViewData(graph2LastXValue,
Double.parseDouble(strIncom)),AutoScrollX);
if( graph2LastXValue >= Xview && Lock == true ){
Series.resetData(new GraphViewData[]{});
graph2LastXValue = 0;
}else graph2LastXValue +=0.1;
if (Lock == true) graphView.setViewPort(0 , Xview);
else graphView.setViewPort(graph2LastXValue-
Xview, Xview);
//refresh
GraphView.removeView(graphView);
GraphView.addView(graphView);
}
}
break;
}}
public boolean isFloatNumber(String num){
try{
Double.parseDouble(num);
}
catch(NumberFormatException nfe){
return false;
}
return true;
}
};
Button bConnect, bDisconnect, bXminus, bXplus;
ToggleButton tblock, tbScroll, tbStream;
static boolean Lock, AutoScrollX, Stream;
//graph init
static LinearLayout GraphView;
static GraphView graphView;
static GraphViewSeries Series;
private static double graph2LastXValue = 0;
private static int Xview = 10;
@Override
protected void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
LinearLayout background = (LinearLayout)findViewById(R.id.bg);
background.setBackgroundColor(Color.BLACK);
init();
Buttoninit();
}
void init(){
bluetooth.gethandler(mHandler);
GraphView = (LinearLayout) findViewById(R.id.Graph);
Series = new GraphViewSeries("Signal",
new GraphViewStyle(Color.YELLOW, 2),
new GraphViewData[]{new GraphViewData(0,0)});
graphView = new LineGraphView(this, "Graph");
graphView.setViewPort(0, Xview);
graphView.setScrollable(true);
graphView.setScalable(true);
graphView.setLegendAlign(LegendAlign.BOTTOM);
graphView.setManualYAxis(true);
graphView.setManualYAxisBounds(5, 0);
graphView.addSeries(Series);
graphView.addView(graphView);
}
void Buttoninit(){
bConnect = (Button)findViewById(R.id.bConnect);
bConnect.setOnClickListener(this);
bDisconnect = (Button)findViewById(R.id.bDisconnect);
bDisconnect.setOnClickListener(this);
bXminus = (Button)findViewById(R.id.bXminus);
bXminus.setOnClickListener(this);
bXplus = (Button)findViewById(R.id.bXPlus);
bXplus.setOnClickListener(this);
tblock = (ToggleButton)findViewById(R.id.tblock);
tblock.setOnClickListener(this);
tbScroll = (ToggleButton)findViewById(R.id.tbScroll);
tbScroll.setOnClickListener(this);
tbStream = (ToggleButton)findViewById(R.id.tbStream);
tbStream.setOnClickListener(this);
Lock= true;
AutoScrollX = true;
Stream =false;
}
@Override
public void onClick(View v){
switch(v.getId()){
case R.id.bConnect:
startActivity(new Intent("android.intent.action.BT2"));
break;
case R.id.bDisconnect:
break;
case R.id.bXminus:
if (Xview>1) Xview--;
break;
case R.id.bXPlus:
if(Xview<30) Xview++;
break;
case R.id.tblock:
break;
case R.id.tbScroll:
if(tbScroll.isChecked()){
AutoScrollX = true;
}else {
AutoScrollX = false;
}
break;
case R.id.tbStream:
if (tbStream.isChecked()){
if(bluetooth.connectedThread !=null)
bluetooth.connectedThread.write("E");
}else{
if(bluetooth.connectedThread !=null)
bluetooth.connectedThread.write("Q");
}
break;
}
}
}
Logcat ERROR如下所示:
02-08 04:45:53.730: D/dalvikvm(891): Not late-enabling CheckJNI (already on)
02-08 04:45:58.230: I/dalvikvm(891): threadid=1: stack overflow on call to Landroid/view/View;.getRawLayoutDirection:I
02-08 04:45:58.230: I/dalvikvm(891): method requires 8+20+0=28 bytes, fp is 0xb2e9f30c (12 left)
02-08 04:45:58.230: I/dalvikvm(891): expanding stack end (0xb2e9f300 to 0xb2e9f000)
02-08 04:45:58.230: I/dalvikvm(891): Shrank stack (to 0xb2e9f300, curFrame is 0xb2ea4ec4)
02-08 04:45:58.240: D/AndroidRuntime(891): Shutting down VM
02-08 04:45:58.240: W/dalvikvm(891): threadid=1: thread exiting with uncaught exception (group=0xb3aa0b90)
02-08 04:45:58.950: D/dalvikvm(891): GC_FOR_ALLOC freed 84K, 5% free 3197K/3348K, paused 107ms, total 119ms
02-08 04:45:59.310: D/dalvikvm(891): GC_FOR_ALLOC freed 302K, 11% free 3309K/3680K, paused 36ms, total 36ms
02-08 04:45:59.330: E/AndroidRuntime(891): FATAL EXCEPTION: main
02-08 04:45:59.330: E/AndroidRuntime(891): Process: com.example.tut, PID: 891
02-08 04:45:59.330: E/AndroidRuntime(891): java.lang.StackOverflowError
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.View.isLayoutDirectionInherited(View.java:12311)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5713)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-08 04:45:59.330: E/AndroidRuntime(891): at android.view.ViewGroup.resetResolvedLayoutDirection(V
02-08 04:45:59.540: D/dalvikvm(891): GC_FOR_ALLOC freed 523K, 16% free 3263K/3860K, paused 63ms, total 63ms
02-08 04:47:31.719: I/Process(891): Sending signal. PID: 891 SIG: 9
布局BLUETOOTH
<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>
布局ACTIVITY_MAIN
<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>
ARDUINO CODES
#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;
}
答案 0 :(得分:0)
在init
的{{1}}方法中,替换
MainActivity
带
graphView.addView(graphView);
您不应该为自己添加视图。
这应该解决问题。