Java 开关已停止
我下载了一个名为BlueremSwitch的项目,当我运行它时,我发现了这个错误,我不知道是什么问题,我是android开发的初学者,我能寻求帮助吗? 我的电话说,不幸的是BlueRem开关停止了,请我请求帮助..请Java 开关已停止,java,android,eclipse,bluetooth,arduino,Java,Android,Eclipse,Bluetooth,Arduino,我下载了一个名为BlueremSwitch的项目,当我运行它时,我发现了这个错误,我不知道是什么问题,我是android开发的初学者,我能寻求帮助吗? 我的电话说,不幸的是BlueRem开关停止了,请我请求帮助..请 01-05 03:53:52.356: E/AndroidRuntime(27851): FATAL EXCEPTION: main 01-05 03:53:52.356: E/AndroidRuntime(27851): java.lang.NullPointerExceptio
01-05 03:53:52.356: E/AndroidRuntime(27851): FATAL EXCEPTION: main
01-05 03:53:52.356: E/AndroidRuntime(27851): java.lang.NullPointerException
01-05 03:53:52.356: E/AndroidRuntime(27851): at Android.Arduino.Bluetooth.BluetoothTest.openBT(BluetoothTest.java:152)
01-05 03:53:52.356: E/AndroidRuntime(27851): at Android.Arduino.Bluetooth.BluetoothTest$1.onClick(BluetoothTest.java:59)
01-05 03:53:52.356: E/AndroidRuntime(27851): at android.view.View.performClick(View.java:4212)
01-05 03:53:52.356: E/AndroidRuntime(27851): at android.view.View$PerformClick.run(View.java:17476)
01-05 03:53:52.356: E/AndroidRuntime(27851): at android.os.Handler.handleCallback(Handler.java:800)
01-05 03:53:52.356: E/AndroidRuntime(27851): at android.os.Handler.dispatchMessage(Handler.java:100)
01-05 03:53:52.356: E/AndroidRuntime(27851): at android.os.Looper.loop(Looper.java:194)
01-05 03:53:52.356: E/AndroidRuntime(27851): at android.app.ActivityThread.main(ActivityThread.java:5371)
01-05 03:53:52.356: E/AndroidRuntime(27851): at java.lang.reflect.Method.invokeNative(Native Method)
01-05 03:53:52.356: E/AndroidRuntime(27851): at java.lang.reflect.Method.invoke(Method.java:525)
01-05 03:53:52.356: E/AndroidRuntime(27851): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
01-05 03:53:52.356: E/AndroidRuntime(27851): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-05 03:53:52.356: E/AndroidRuntime(27851): at dalvik.system.NativeStart.main(Native Method)
这是Bluethootest.java
package Android.Arduino.Bluetooth;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Button;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;
public class BluetoothTest extends Activity
{
TextView myLabel;
EditText myTextbox;
BluetoothAdapter mBluetoothAdapter;
BluetoothSocket mmSocket;
BluetoothDevice mmDevice;
OutputStream mmOutputStream;
InputStream mmInputStream;
Thread workerThread;
byte[] readBuffer;
int readBufferPosition;
int counter;
volatile boolean stopWorker;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button openButton = (Button)findViewById(R.id.open);
// Button sendButton = (Button)findViewById(R.id.send);
Button closeButton = (Button)findViewById(R.id.close);
Button onButton = (Button)findViewById(R.id.onButton);
Button offButton = (Button)findViewById(R.id.offButton);
myLabel = (TextView)findViewById(R.id.label);
// myTextbox = (EditText)findViewById(R.id.entry);
//Open Button
openButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
try
{
findBT();
openBT();
}
catch (IOException ex) { }
}
});
//Send Button
/*sendButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
try
{
sendData();
}
catch (IOException ex) { }
}
});*/
//ON SWITCH
onButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
onButton();
} catch (Exception e) {
// TODO: handle exception
}
}
});
//OFF SWITCH
offButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
offButton();
} catch (Exception e) {
// TODO: handle exception
}
}
});
//Close button
closeButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
try
{
closeBT();
}
catch (IOException ex) { }
}
});
}
void findBT()
{
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter == null)
{
myLabel.setText("No bluetooth adapter available");
}
if(!mBluetoothAdapter.isEnabled())
{
Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetooth, 0);
}
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if(pairedDevices.size() > 0)
{
for(BluetoothDevice device : pairedDevices)
{
if(device.getName().equals("linvor"))
{
mmDevice = device;
Log.v("ArduinoBT", "findBT found device named " + mmDevice.getName());
Log.v("ArduinoBT", "device address is " + mmDevice.getAddress());
break;
}
}
}
myLabel.setText("Bluetooth Device Found");
}
void openBT() throws IOException
{
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); //Standard SerialPortService ID
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
mmSocket.connect();
mmOutputStream = mmSocket.getOutputStream();
mmInputStream = mmSocket.getInputStream();
beginListenForData();
myLabel.setText("Bluetooth Opened");
}
void beginListenForData()
{
final Handler handler = new Handler();
final byte delimiter = 10; //This is the ASCII code for a newline character
stopWorker = false;
readBufferPosition = 0;
readBuffer = new byte[1024];
workerThread = new Thread(new Runnable()
{
public void run()
{
while(!Thread.currentThread().isInterrupted() && !stopWorker)
{
try
{
int bytesAvailable = mmInputStream.available();
if(bytesAvailable > 0)
{
byte[] packetBytes = new byte[bytesAvailable];
mmInputStream.read(packetBytes);
for(int i=0;i<bytesAvailable;i++)
{
byte b = packetBytes[i];
if(b == delimiter)
{
byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
final String data = new String(encodedBytes, "US-ASCII");
readBufferPosition = 0;
handler.post(new Runnable()
{
public void run()
{
myLabel.setText(data);
}
});
}
else
{
readBuffer[readBufferPosition++] = b;
}
}
}
}
catch (IOException ex)
{
stopWorker = true;
}
}
}
});
workerThread.start();
}
void sendData() throws IOException
{
String msg = myTextbox.getText().toString();
msg += "\0";
//mmOutputStream.write(msg.getBytes());
//mmOutputStream.write(msg.getBytes());
//mmOutputStream.flush();
//mmOutputStream.close();
//mmSocket.close();
myLabel.setText("Data Sent"+msg);
}
void onButton() throws IOException
{
mmOutputStream.write("1".getBytes());
}
void offButton() throws IOException
{
mmOutputStream.write("2".getBytes());
}
void closeBT() throws IOException
{
stopWorker = true;
mmOutputStream.close();
mmInputStream.close();
mmSocket.close();
myLabel.setText("Bluetooth Closed");
}
}
包Android.Arduino.Bluetooth;
导入android.app.Activity;
导入android.bluetooth.BluetoothAdapter;
导入android.bluetooth.bluetooth设备;
导入android.bluetooth.BluetoothSocket;
导入android.content.Intent;
导入android.os.Bundle;
导入android.os.Handler;
导入android.util.Log;
导入android.view.view;
导入android.widget.TextView;
导入android.widget.EditText;
导入android.widget.Button;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.OutputStream;
导入java.util.Set;
导入java.util.UUID;
公共类BluetoothTest扩展了活动
{
文本视图myLabel;
编辑文本myTextbox;
蓝牙适配器mBluetoothAdapter;
蓝牙插座;
蓝牙设备;
输出流mmOutputStream;
输入流mmInputStream;
螺纹加工螺纹;
字节[]读取缓冲区;
int-readBufferPosition;
整数计数器;
易变布尔工;
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
按钮打开按钮=(按钮)findViewById(R.id.open);
//按钮sendButton=(按钮)findViewById(R.id.send);
按钮关闭按钮=(按钮)findViewById(R.id.close);
按钮onButton=(按钮)findViewById(R.id.onButton);
按钮关闭按钮=(按钮)findViewById(R.id.offButton);
myLabel=(TextView)findViewById(R.id.label);
//myTextbox=(EditText)findViewById(R.id.entry);
//打开按钮
openButton.setOnClickListener(新视图.OnClickListener()
{
公共void onClick(视图v)
{
尝试
{
findBT();
openBT();
}
catch(IOException ex){}
}
});
//发送按钮
/*sendButton.setOnClickListener(新视图.OnClickListener()
{
公共void onClick(视图v)
{
尝试
{
sendData();
}
catch(IOException ex){}
}
});*/
//接通开关
onButton.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
试一试{
onButton();
}捕获(例外e){
//TODO:处理异常
}
}
});
//关闭开关
offButton.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
试一试{
offButton();
}捕获(例外e){
//TODO:处理异常
}
}
});
//关闭按钮
closeButton.setOnClickListener(新视图.OnClickListener()
{
公共void onClick(视图v)
{
尝试
{
closeBT();
}
catch(IOException ex){}
}
});
}
void findBT()
{
mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter==null)
{
myLabel.setText(“没有可用的蓝牙适配器”);
}
如果(!mBluetoothAdapter.isEnabled())
{
意图启用Bluetooth=新意图(BluetoothAdapter.ACTION\u REQUEST\u ENABLE);
startActivityForResult(启用蓝牙,0);
}
设置pairedDevices=mBluetoothAdapter.getBondedDevices();
如果(pairedDevices.size()>0)
{
用于(蓝牙设备:pairedDevices)
{
if(device.getName().equals(“linvor”))
{
mmDevice=设备;
Log.v(“ArduinoBT”,“findBT找到名为“+mmDevice.getName()”的设备);
Log.v(“ArduinoBT”,“设备地址为”+mmDevice.getAddress());
打破
}
}
}
myLabel.setText(“找到蓝牙设备”);
}
void openBT()引发IOException
{
UUID UUID=UUID.fromString(“00001101-0000-1000-8000-00805f9b34fb”);//标准SerialPortService ID
mmSocket=mmDevice.createRfcommSocketToServiceRecord(uuid);
mmSocket.connect();
mmOutputStream=mmSocket.getOutputStream();
mmInputStream=mmSocket.getInputStream();
beginListenForData();
myLabel.setText(“蓝牙打开”);
}
void beginListenForData()
{
最终处理程序=新处理程序();
final byte delimiter=10;//这是换行符的ASCII码
stopWorker=false;
readBufferPosition=0;
readBuffer=新字节[1024];
workerThread=新线程(new Runnable())
{
公开募捐
{
而(!Thread.currentThread().isInterrupted()&&!stopWorker)
{
尝试
{
int bytesavable=mmInputStream.available();
如果(字节可用>0)
{
byte[]packetBytes=新字节[bytesAvailable];
mmInputStream.read(packetBytes);
对于(int i=0;发生此“错误”的代码中的一小部分将非常好…请发布BluetoothTest.java class.BlueRem?可能是BlueTerm?因为我知道这一点(使其正常工作)。我还尝试了BlueScript,这是