android arduino 通信,android - 与Android + Arduino的蓝牙通信。 (仅将数据发送到Arduino) - 堆栈内存溢出...

我试图将字节从我的android发送到我的arduino,以使其关闭/打开连接到它的led。 当我打开我的android应用程序时,它立即转到“不响应消息”并关闭。 张贴LogCat以防万一。

这是android代码:

package com.example.arduino;

import java.io.IOException;

import java.io.OutputStream;

import java.util.UUID;

import android.os.Bundle;

import android.annotation.SuppressLint;

import android.app.Activity;

import android.bluetooth.BluetoothAdapter;

import android.bluetooth.BluetoothDevice;

import android.bluetooth.BluetoothSocket;

import android.view.Menu;

import android.view.View;

import android.widget.Button;

import android.widget.Toast;

public class MainActivity extends Activity {

Button turnOn , turnOff;

BluetoothAdapter btAdapter;

OutputStream outStream;

BluetoothSocket btSocket;

byte one;

byte two;

BluetoothDevice btDevice;

String address = "40:98:4E:37:4E:51";

UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

@SuppressWarnings("static-access")

@SuppressLint("ShowToast") @Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

turnOn = (Button) findViewById(R.id.On);

turnOff = (Button) findViewById(R.id.Off);

one = 0;

two = 1;

btAdapter = BluetoothAdapter.getDefaultAdapter();

try {

outStream = btSocket.getOutputStream();

} catch (IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

btDevice = btAdapter.getRemoteDevice(address);

try {

btSocket = btDevice.createRfcommSocketToServiceRecord(uuid);

btSocket.connect();

} catch (IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

if(btDevice.ACTION_ACL_CONNECTED != null){ //Suppressed.

Toast.makeText(getBaseContext(), "Connected!", Toast.LENGTH_SHORT).show(); //Suppressed.

}

turnOn.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

ledOn();

}

private void ledOn() {

// TODO Auto-generated method stub

try {

outStream.write(one);

outStream.flush();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

outStream.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

});

turnOff.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

ledOff();

}

private void ledOff() {

// TODO Auto-generated method stub

try {

outStream.write(two);

outStream.flush();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

outStream.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

});

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

}

logcat的:

10-17 23:32:05.269: W/dalvikvm(21770): threadid=1: thread exiting with uncaught exception (group=0x40c7ea08)

10-17 23:32:05.269: E/AndroidRuntime(21770): FATAL EXCEPTION: main

10-17 23:32:05.269: E/AndroidRuntime(21770): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.arduino/com.example.arduino.MainActivity}: java.lang.NullPointerException

10-17 23:32:05.269: E/AndroidRuntime(21770): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2463)

10-17 23:32:05.269: E/AndroidRuntime(21770): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)

10-17 23:32:05.269: E/AndroidRuntime(21770): at android.app.ActivityThread.access$600(ActivityThread.java:162)

10-17 23:32:05.269: E/AndroidRuntime(21770): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366)

10-17 23:32:05.269: E/AndroidRuntime(21770): at android.os.Handler.dispatchMessage(Handler.java:99)

10-17 23:32:05.269: E/AndroidRuntime(21770): at android.os.Looper.loop(Looper.java:158)

10-17 23:32:05.269: E/AndroidRuntime(21770): at android.app.ActivityThread.main(ActivityThread.java:5751)

10-17 23:32:05.269: E/AndroidRuntime(21770): at java.lang.reflect.Method.invokeNative(Native Method)

10-17 23:32:05.269: E/AndroidRuntime(21770): at java.lang.reflect.Method.invoke(Method.java:511)

10-17 23:32:05.269: E/AndroidRuntime(21770): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)

10-17 23:32:05.269: E/AndroidRuntime(21770): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)

10-17 23:32:05.269: E/AndroidRuntime(21770): at dalvik.system.NativeStart.main(Native Method)

10-17 23:32:05.269: E/AndroidRuntime(21770): Caused by: java.lang.NullPointerException

10-17 23:32:05.269: E/AndroidRuntime(21770): at com.example.arduino.MainActivity.onCreate(MainActivity.java:44)

10-17 23:32:05.269: E/AndroidRuntime(21770): at android.app.Activity.performCreate(Activity.java:5165)

10-17 23:32:05.269: E/AndroidRuntime(21770): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1103)

10-17 23:32:05.269: E/AndroidRuntime(21770): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2419)

10-17 23:32:05.269: E/AndroidRuntime(21770): ... 11 more

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值