蓝牙崩溃了应用程序

时间:2015-09-13 06:32:04

标签: android bluetooth

我刚用Arduino完成了一个控制房子灯光的系统。 ON和OFF的序列代码是I和O. 我正在尝试学习android编程,并使用它来处理这个项目与蓝牙。 一切正常,但当按下发送“I”或“O”的按钮时应用程序崩溃。初始化蓝牙连接的按钮也会崩溃。 这是我的代码

package com.macho.projects.lightcontroller;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.*;
import android.bluetooth.*;
import java.util.*;
import android.os.*;
import android.util.*;


public class Physical extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_physical);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_physical, menu);
    return true;
}



private OutputStream outputStream;
private InputStream inStream;

public void Link (View v) throws IOException {
    init();
    v.bringToFront();
}

public void ON (View v) throws IOException
{
    write("I");
}
public void OFF (View v) throws IOException
{
    write("O");
}

private void init() throws IOException {
    BluetoothAdapter blueAdapter = BluetoothAdapter.getDefaultAdapter();
    if (blueAdapter != null) {
        if (blueAdapter.isEnabled()) {
            Set<BluetoothDevice> bondedDevices = blueAdapter.getBondedDevices();

            if(bondedDevices.size() > 0){
                BluetoothDevice[] devices = (BluetoothDevice[]) bondedDevices.toArray();
                BluetoothDevice device = devices[0];
                ParcelUuid[] uuidas = device.getUuids();
                BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuidas[0].getUuid());
                socket.connect();
                outputStream = socket.getOutputStream();
                inStream = socket.getInputStream();
            }

            Log.e("error", "No appropriate paired devices.");
        }else{
            Log.e("error", "Bluetooth is disabled.");
        }
    }
}

public void write(String s) throws IOException {
    outputStream.write(s.getBytes());
}



@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

如果你们可以帮我一些代码,真的非常感谢。 对不起,如果我的英语不好。我不是母语。

日志:

09-13 09:27:46.652  23310-23310/com.macho.projects.lightcontroller D/OpenGLRenderer﹕ Enabling debug mode 0
09-13 09:28:19.179  23310-23310/com.macho.projects.lightcontroller D/AndroidRuntime﹕ Shutting down VM
09-13 09:28:19.179  23310-23310/com.macho.projects.lightcontroller W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x415d9d40)
09-13 09:28:19.197  23310-23310/com.macho.projects.lightcontroller E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.macho.projects.lightcontroller, PID: 23310
    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:3841)
            at android.view.View.performClick(View.java:4456)
            at android.view.View$PerformClick.run(View.java:18465)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at android.view.View$1.onClick(View.java:3836)
            at android.view.View.performClick(View.java:4456)
            at android.view.View$PerformClick.run(View.java:18465)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.macho.projects.lightcontroller.Physical.write(Physical.java:75)
            at com.macho.projects.lightcontroller.Physical.ON(Physical.java:44)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at android.view.View$1.onClick(View.java:3836)
            at android.view.View.performClick(View.java:4456)
            at android.view.View$PerformClick.run(View.java:18465)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案