gpt4 book ai didi

android - 与 Arduino + Android 的蓝牙通信

转载 作者:行者123 更新时间:2023-11-29 21:31:17 24 4
gpt4 key购买 nike

我试图将字节从我的 android 发送到我的 arduino 以使其关闭/打开连接到它的 led。当我打开我的 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;
}
}

日志:

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

最佳答案

您正在使用初始化之前的 btSocket 变量。在你的 onCreate 方法上,尝试把这个:

 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();
}

关于android - 与 Arduino + Android 的蓝牙通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19437310/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com