通过蓝牙从arduino接收android上的数据
嗨,我的问题是我无法从arduino到android获取数据。其概念是在android上显示arduino的价值。你有从arduino接收android数据的代码吗 这是我的密码 包com.example.ardinosensors通过蓝牙从arduino接收android上的数据,android,bluetooth,arduino,Android,Bluetooth,Arduino,嗨,我的问题是我无法从arduino到android获取数据。其概念是在android上显示arduino的价值。你有从arduino接收android数据的代码吗 这是我的密码 包com.example.ardinosensors import java.util.Set; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevic
import java.util.Set;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class DeviceListActivity extends Activity {
// Debugging for LOGCAT
private static final String TAG = "DeviceListActivity";
private static final boolean D = true;
// declare button for launching website
and textview for connection status
Button tlbutton;
TextView textView1;
// EXTRA string to send on to mainactivity
public static String EXTRA_DEVICE_ADDRESS = "device_address";
// Member fields
private BluetoothAdapter mBtAdapter;
private ArrayAdapter<String> mPairedDevicesArrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.device_list);
}
@Override
public void onResume()
{
super.onResume();
//***************
checkBTState();
textView1 = (TextView) findViewById(R.id.connecting);
textView1.setTextSize(40);
textView1.setText(" ");
// Initialize array adapter for paired devices
mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);
// Find and set up the ListView for paired devices
ListView pairedListView = (ListView)
findViewById(R.id.paired_devices);
pairedListView.setAdapter(mPairedDevicesArrayAdapter);
pairedListView.setOnItemClickListener(mDeviceClickListener);
// Get the local Bluetooth adapter
mBtAdapter = BluetoothAdapter.getDefaultAdapter();
// Get a set of currently paired devices and append to '
pairedDevices'
Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();
// Add previosuly paired devices to the array
if (pairedDevices.size() > 0) {
findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE)
;//make title viewable
for (BluetoothDevice device : pairedDevices) {
mPairedDevicesArrayAdapter.add(device.getName() + "\n" +
device.getAddress());
}
} else {
String noDevices =
getResources().getText(R.string.none).toString();
mPairedDevicesArrayAdapter.add(noDevices);
}
}
// Set up on-click listener for the list (nicked this - unsure)
private OnItemClickListener mDeviceClickListener = new
OnItemClickListener() {
public void onItemClick(AdapterView<?> av, View v, int arg2, long
arg3) {
textView1.setText("Connecting...");
// Get the device MAC address, which is the last 17 chars
in the View
String info = ((TextView) v).getText().toString();
String address = info.substring(info.length() - 17);
// Make an intent to start next activity while taking
an extra which is the MAC address.
Intent i = new Intent(DeviceListActivity.this,
MainActivity.class);
i.putExtra(EXTRA_DEVICE_ADDRESS, address);
startActivity(i);
}
};
private void checkBTState() {
// Check device has Bluetooth and that it is turned on
mBtAdapter=BluetoothAdapter.getDefaultAdapter();
// CHECK THIS OUT THAT IT WORKS!!!
if(mBtAdapter==null) {
Toast.makeText(getBaseContext(),
"Device does not support Bluetooth", Toast.LENGTH_SHORT).show();
} else {
if (mBtAdapter.isEnabled()) {
Log.d(TAG, "...Bluetooth ON...");
} else {
//Prompt user to turn on Bluetooth
Intent enableBtIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
}
}
}
}
这是LOGCAT上的错误
02-01 04:45:41.274: W/dalvikvm(1112): threadid=1: thread exiting with uncaught exception (group=0xb3aa8b90)
02-01 04:45:41.304: E/AndroidRuntime(1112): FATAL EXCEPTION: main
02-01 04:45:41.304: E/AndroidRuntime(1112): Process: com.example.arduinosensors, PID: 1112
02-01 04:45:41.304: E/AndroidRuntime(1112):
java.lang.RuntimeException:
Unable to resume activity {com.example.arduinosensors/com.example.arduinosensors.DeviceListActivity}: java.lang.NullPointerException
02-01 04:45:41.304: E/AndroidRuntime(1112): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2769)
02-01 04:45:41.304: E/AndroidRuntime(1112): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2798)
02-01 04:45:41.304: E/AndroidRuntime(1112): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2231)
02-01 04:45:41.304: E/AndroidRuntime(1112): at android.app.ActivityThread.access$700(ActivityThread.java:135)
02-01 04:45:41.304: E/AndroidRuntime(1112): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
02-01 04:45:41.304: E/AndroidRuntime(1112): at android.os.Handler.dispatchMessage(Handler.java:102)
02-01 04:45:41.304: E/AndroidRuntime(1112): at android.os.Looper.loop(Looper.java:137)
02-01 04:45:41.304: E/AndroidRuntime(1112): at android.app.ActivityThread.main(ActivityThread.java:4998)
02-01 04:45:41.304: E/AndroidRuntime(1112): at java.lang.reflect.Method.invokeNative(Native Method)
02-01 04:45:41.304: E/AndroidRuntime(1112): at java.lang.reflect.Method.invoke(Method.java:515)
02-01 04:45:41.304: E/AndroidRuntime(1112): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
02-01 04:45:41.304: E/AndroidRuntime(1112): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
02-01 04:45:41.304: E/AndroidRuntime(1112): at dalvik.system.NativeStart.main(Native Method)
02-01 04:45:41.304: E/AndroidRuntime(1112): Caused by: java.lang.NullPointerException
02-01 04:45:41.304: E/AndroidRuntime(1112): at com.example.arduinosensors.DeviceListActivity.onResume(DeviceListActivity.java:66)
02-01 04:45:41.304: E/AndroidRuntime(1112): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
02-01 04:45:41.304: E/AndroidRuntime(1112): at android.app.Activity.performResume(Activity.java:5322)
02-01 04:45:41.304: E/AndroidRuntime(1112): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2759)
02-01 04:45:41.304: E/AndroidRuntime(1112): ... 12 more
转到您在评论中提到的网页中指定的位置。下载zip并使用它从Eclipse中的现有代码创建一个新的android应用程序项目。它应该不会崩溃。我刚试过
更新:
出现空指针的原因是仿真器不支持蓝牙,而您正试图访问仿真器上的蓝牙适配器。您可以在中阅读有关emulator限制的更多信息 请发布您的源代码并记录cat堆栈跟踪。你不需要多次复制粘贴你的问题来让它看起来笨重:。哦,对不起,我的错误。这实际上是我的问题嗨小姐我的问题是我无法从arduino到android获取数据。其概念是在android上显示arduino的价值。你有从arduino接收android数据的代码吗?我没有很好地编辑它。我的问题是如何通过蓝牙从arduino接收数据到android?你已经编写了android应用程序吗?我使用了Eclipse。我的代码中有一些错误。你想看看密码吗?好的,妈妈,我试试。谢谢你的评论:妈妈,在你试过之后,你的安卓系统能收到一些数据吗?来自arduino?不,我没有用arduino设备测试。我运行了android应用程序,它没有崩溃。哦,我明白了,妈妈,我试着测试它。谢谢。我可以在测试后再问一次问题吗?是的,如果它有效,你可以接受我的答案,然后再问另一个问题。妈妈,我把它运行到模拟器上,再次出现错误,我想我第一次遇到的错误是一样的。错误也:
02-01 04:45:41.274: W/dalvikvm(1112): threadid=1: thread exiting with uncaught exception (group=0xb3aa8b90)
02-01 04:45:41.304: E/AndroidRuntime(1112): FATAL EXCEPTION: main
02-01 04:45:41.304: E/AndroidRuntime(1112): Process: com.example.arduinosensors, PID: 1112
02-01 04:45:41.304: E/AndroidRuntime(1112):
java.lang.RuntimeException:
Unable to resume activity {com.example.arduinosensors/com.example.arduinosensors.DeviceListActivity}: java.lang.NullPointerException
02-01 04:45:41.304: E/AndroidRuntime(1112): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2769)
02-01 04:45:41.304: E/AndroidRuntime(1112): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2798)
02-01 04:45:41.304: E/AndroidRuntime(1112): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2231)
02-01 04:45:41.304: E/AndroidRuntime(1112): at android.app.ActivityThread.access$700(ActivityThread.java:135)
02-01 04:45:41.304: E/AndroidRuntime(1112): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
02-01 04:45:41.304: E/AndroidRuntime(1112): at android.os.Handler.dispatchMessage(Handler.java:102)
02-01 04:45:41.304: E/AndroidRuntime(1112): at android.os.Looper.loop(Looper.java:137)
02-01 04:45:41.304: E/AndroidRuntime(1112): at android.app.ActivityThread.main(ActivityThread.java:4998)
02-01 04:45:41.304: E/AndroidRuntime(1112): at java.lang.reflect.Method.invokeNative(Native Method)
02-01 04:45:41.304: E/AndroidRuntime(1112): at java.lang.reflect.Method.invoke(Method.java:515)
02-01 04:45:41.304: E/AndroidRuntime(1112): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
02-01 04:45:41.304: E/AndroidRuntime(1112): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
02-01 04:45:41.304: E/AndroidRuntime(1112): at dalvik.system.NativeStart.main(Native Method)
02-01 04:45:41.304: E/AndroidRuntime(1112): Caused by: java.lang.NullPointerException
02-01 04:45:41.304: E/AndroidRuntime(1112): at com.example.arduinosensors.DeviceListActivity.onResume(DeviceListActivity.java:66)
02-01 04:45:41.304: E/AndroidRuntime(1112): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
02-01 04:45:41.304: E/AndroidRuntime(1112): at android.app.Activity.performResume(Activity.java:5322)
02-01 04:45:41.304: E/AndroidRuntime(1112): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2759)
02-01 04:45:41.304: E/AndroidRuntime(1112): ... 12 more