我正在构建一个应用程序来绘制通过蓝牙接收的动态值。由于我是Android新手,我使用了android开发人员指南中的教程,但是应用程序在尝试访问Thread.start()方法时崩溃。这是代码,我无法发布 LogCat,因为我的模拟器不支持蓝牙。这是代码。主要活动.java
package com.example.enose.sensor;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.LinkedList;
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.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.MessageQueue;
import android.widget.TextView;
import android.widget.Toast;
import com.androidplot.xy.BoundaryMode;
import com.androidplot.xy.LineAndPointFormatter;
import com.androidplot.xy.LineAndPointRenderer;
import com.androidplot.xy.SimpleXYSeries;
import com.androidplot.xy.XYPlot;
public class MainActivity extends Activity {
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
private static final int HISTORY_SIZE = 30;
public static final int MESSAGE_READ = 0;
private XYPlot aprHistoryPlot = null;
private SimpleXYSeries alcohol=null;
private SimpleXYSeries lpg=null;
private SimpleXYSeries methane=null;
private SimpleXYSeries quality=null;
private LinkedList<Number> alcoholHistory;
private LinkedList<Number> lpgHistory;
private BluetoothAdapter mBluetoothAdapter;
//private UUID uid;
//Thread to initiate connection
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 {
tmp = device.createRfcommSocketToServiceRecord(UUID.fromString("0000110100001000800000805F9B34FB"));
} catch (IOException e) { }
mmSocket = tmp;
}
public void run() {
// Cancel discovery because it will slow down the connection
mBluetoothAdapter.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)
//manageConnectedSocket(mmSocket);
}
public void manageConnectedSocket(BluetoothSocket socket)
{
ConnectedThread data=new ConnectedThread(socket);
data.start();
}
/** Will cancel an in-progress connection, and close the socket */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
public 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) { }
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte[] buffer = new byte[1024]; // buffer store for the stream
int bytes; // bytes returned from read()
// Keep listening to the InputStream until an exception occurs
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI activity
// h.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(byte[] bytes) {
try {
mmOutStream.write(bytes);
} catch (IOException e) { }
}
/* Call this from the main activity to shutdown the connection */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
private LinkedList<Number> methaneHistory;
{
alcoholHistory = new LinkedList<Number>();
lpgHistory = new LinkedList<Number>();
methaneHistory = new LinkedList<Number>();
alcohol = new SimpleXYSeries("Alcohol");
lpg = new SimpleXYSeries("LPG");
methane = new SimpleXYSeries("Methane");
quality = new SimpleXYSeries("Air Quality");
}
private class MyHandler extends Handler
{
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
int val=msg.arg1;
byte[] d=(byte[]) msg.obj;
Double s1;
s1=Double.longBitsToDouble(val);
alcoholHistory.addLast(s1);
if(alcoholHistory.size()>30)
alcoholHistory.removeFirst();
alcohol.setModel(alcoholHistory,SimpleXYSeries.ArrayFormat.Y_VALS_ONLY);
aprHistoryPlot.redraw();
}
}
//MyHandler h;
private BluetoothDevice selDevice;
private Context context;
private BluetoothSocket tmp;
private ConnectThread connect;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Handler h=new Handler();
context=this;
setContentView(R.layout.activity_main);
aprHistoryPlot = (XYPlot) findViewById(R.id.aprHistoryPlot);
alcoholHistory.addFirst(2);
lpgHistory.addFirst(3);
methaneHistory.addFirst(4);
alcoholHistory.addFirst(3);
lpgHistory.addFirst(2);
methaneHistory.addFirst(1);
alcohol.setModel(alcoholHistory, SimpleXYSeries.ArrayFormat.Y_VALS_ONLY);
lpg.setModel(lpgHistory, SimpleXYSeries.ArrayFormat.Y_VALS_ONLY);
methane.setModel(methaneHistory, SimpleXYSeries.ArrayFormat.Y_VALS_ONLY);
aprHistoryPlot.setRangeBoundaries(0, 5,BoundaryMode.FIXED);
aprHistoryPlot.setDomainBoundaries(0, 30, BoundaryMode.FIXED);
aprHistoryPlot.addSeries(alcohol, LineAndPointRenderer.class, new LineAndPointFormatter(Color.rgb(0, 100, 200), Color.BLACK,null));
aprHistoryPlot.addSeries(lpg, LineAndPointRenderer.class, new LineAndPointFormatter(Color.rgb(100, 200, 100), Color.BLACK,null));
aprHistoryPlot.addSeries(methane, LineAndPointRenderer.class, new LineAndPointFormatter(Color.rgb(200, 100, 100), Color.BLACK,null));
aprHistoryPlot.setDomainStepValue(1);
aprHistoryPlot.setTicksPerRangeLabel(3);
aprHistoryPlot.setDomainLabel("time");
aprHistoryPlot.getDomainLabelWidget().pack();
aprHistoryPlot.setRangeLabel("level");
aprHistoryPlot.getRangeLabelWidget().pack();
aprHistoryPlot.disableAllMarkup();
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
Toast.makeText(context, "Not supported", Toast.LENGTH_LONG).show();
}
else{
//Toast.makeText(context, "test1", Toast.LENGTH_SHORT).show();
if (!mBluetoothAdapter.isEnabled()) {
mBluetoothAdapter.enable();
// Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
// startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
Toast.makeText(context, "test2", Toast.LENGTH_SHORT).show();
Set<BluetoothDevice> devices=mBluetoothAdapter.getBondedDevices();
boolean isDeviceSet=false;
if(devices==null)
{
Toast.makeText(context, "No paired devices", Toast.LENGTH_LONG).show();
}
else
{
//Toast.makeText(context, "test3", Toast.LENGTH_SHORT).show();
//BluetoothDevice dev=mBluetoothAdapter.getRemoteDevice("006645ffly477");
for (BluetoothDevice device : devices) {
// Add the name and address to an array adapter to show in a ListView
//mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
selDevice=device;
isDeviceSet=true;
//TextView tv=(TextView) findViewById(R.id.txtlist);
//tv.setText("Bluetooth device "+selDevice.getName()+" selected");
break;
}
Message msg=h.obtainMessage();
//uid=new UUID(7659464,976328762);
//Toast.makeText(context, "test4", Toast.LENGTH_SHORT ).show();
//connect=new ConnectThread(dev);
//connect.run();
//dev=mBluetoothAdapter.getRemoteDevice("006645ffly477");
//if(dev!=null)
//Toast.makeText(context, "device null", Toast.LENGTH_SHORT).show();
//else
//connect=new ConnectThread(selDevice);
// connect.run();
}
}
}
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.enose.sensor"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="5"
android:targetSdkVersion="10" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<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" >
<com.androidplot.xy.XYPlot
android:id="@+id/aprHistoryPlot"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_marginTop="10px"
android:layout_marginLeft="10px"
android:layout_marginRight="10px"
title="eNose Readings"/>
<TextView
android:id="@+id/txtlist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
小猫
10-04 16:09:01.508: E/InputDispatcher(62): channel '4074d648 com.example.enose.sensor/com.example.enose.sensor.MainActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x8
10-04 16:09:01.508: E/InputDispatcher(62): channel '4074d648 com.example.enose.sensor/com.example.enose.sensor.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
10-04 16:12:09.808: E/InputDispatcher(62): channel '405ecbb8 com.example.enose.sensor/com.example.enose.sensor.MainActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x8
10-04 16:12:09.808: E/InputDispatcher(62): channel '405ecbb8 com.example.enose.sensor/com.example.enose.sensor.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
10-04 21:11:42.588: E/InputDispatcher(62): channel '405a9a88 com.example.enose.sensor/com.example.enose.sensor.MainActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x8
10-04 21:11:42.588: E/InputDispatcher(62): channel '405a9a88 com.example.enose.sensor/com.example.enose.sensor.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
10-04 21:11:57.488: E/InputDispatcher(62): channel '40746548 com.example.enose.sensor/com.example.enose.sensor.MainActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x8
10-04 21:11:57.488: E/InputDispatcher(62): channel '40746548 com.example.enose.sensor/com.example.enose.sensor.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
10-04 21:39:31.018: E/InputDispatcher(62): channel '4061f108 com.example.enose.sensor/com.example.enose.sensor.MainActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x8
10-04 21:39:31.018: E/InputDispatcher(62): channel '4061f108 com.example.enose.sensor/com.example.enose.sensor.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
如果这行不通。有人能发布一个通过蓝牙连接到具有已知MAC地址的设备的简单教程吗?还有,有人能解释一下获得UUID的过程及其用途吗?
您无法在模拟器中使用蓝牙,因此它会崩溃。您需要一个真正的蓝牙设备来测试它.
尝试在您的清单中也添加此权限:
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
问题内容: 这是启动桌面版本时得到的: 我都试过这个政党成员和这一个,我也得到了同样的错误在同一直线上。 编辑: 我只是注意到我的.tmx文件的数据是由Tiled Map Editor自动编码的,所以我只是停用了它,但仍然遇到相同的错误。 如果我复制/粘贴该项目(我的意思是整个项目,不仅是render()方法),它会工作,但是当我尝试在自己的项目中加载自己的地图时,它只会崩溃… 因此,在那之后,我
在以前的代码上,谁医生帮了我 现在,android应用程序在我的手机上运行时崩溃了,这是错误日志 第一个问题是在我用相机扫描二维码后,它不能显示在二维码的结果进入 第二个问题是,我从存储器中选择了一个QRcode图像,然后点击确认,它崩溃了 下面是我认为的问题 类型不匹配:推断的类型是Uri?但乌里是意料之中的 冗余SAM构造函数 'onRequestPermissionsResult(Int,数
我正在尝试将图像添加到我新创建的应用程序中。但是,当我运行我的应用程序时,它会崩溃。我没有在我的应用程序中编写任何代码,因此我提供了XML代码和错误日志。我在以前的应用程序中使用了ImageViews。没有错误。我已经优化了我的图像,但它仍然给我错误。 图像详细信息 错误日志: 09-08 16:03:02.266 15137-15137/ooper.loopE/AndroidRuntime: F
在我的应用程序中,我想在导航抽屉的片段中使用一个gridView,但我的应用程序正在崩溃。这是我的代码:
我正在开发一个Android应用程序,它必须从文件中解析文本。 我的解析器中有以下方法。java类: 每当调用缓冲区时,我都会遇到问题。while循环中的readLine()方法。 我传递以下路径信息File对象是: 现在我已经看了堆栈和在线上的许多帖子,以便尝试解决这个问题,并尝试使用一些解决方案,但没有运气。这是我从错误堆栈跟踪中获得的一个片段。 我确信文件的路径是正确的,因为我在调试时检查了
我是新的Android。我试图将FB登录与我的Android应用程序集成。我已经按照developers.facebook.com的所有说明进行了集成。然而,当我试图运行我的应用程序时,它会在发布时崩溃。 请注意,我的AndroidManifest.xml中已经有Facebook应用程序ID、元数据标签和android活动标签。 布局如下: 以下是my LoginActivity的代码片段: 以下
我想将< code>imageview中的图像旋转为3d图像。因此,我使用列表级别的< code>drawable在< code>imageview中添加了50个图像。当我在< code>images.xml中添加50张图片时,应用程序崩溃,但如果我添加12张图片,它会完美地工作。我不知道为什么会发生这个错误。我还没有找到任何解决方法。帮我摆脱这个问题。 images.xml activity.x
由于某种原因,我的程序在完成线程后崩溃。启动方法。由于这是我第一次使用线程,我完全迷路了,不知道发生了什么。 并称之为踏面: 我会非常感激的! 我的日志显示如下(抱歉弄乱了): 10-22 14:59:43.319:E/AndroidRuntime(961):致命异常:Thread-81 10-22 14:59:43.319:E/AndroidRuntime(961):android。看法View