Android - 通过蓝牙将 Samsung Galaxy Ace 连接到 NXT
好的,我已经向清单文件添加了权限并配对了我的设备,但我在这里遇到了崩溃:SetpairedDevices = btAdapter.getBondedDevices();
我尝试通过单击按钮进行连接:
private OnClickListener myListener = new OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.main_btnYes:
connectToNXT(); // connect to NXT
myIntent = new Intent(v.getContext(), SelectSession.class);
startActivityForResult(myIntent, 0);
break;
case R.id.main_btnNo:
myIntent = new Intent(v.getContext(), ExitScreen.class);
startActivityForResult(myIntent, 0);
break;
}
}
};
这是 connectToNXT() 方法: 崩溃发生在此处:Set bondDevices = btAdapter.getBondedDevices(); private void connectToNXT() {
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
**Set<BluetoothDevice> bondedDevices = btAdapter.getBondedDevices();**
BluetoothDevice nxtDevice = null;
}
有人知道为什么这会导致崩溃吗?
另外,由于我对 android 和蓝牙还很陌生(2 天:D),有人可以让我知道一个关于 android 蓝牙的好教程吗?
谢谢,
里奇。
Ok so I've added the permission to the manifest file and paired my devices but I am getting a crash right here: Set pairedDevices = btAdapter.getBondedDevices();
I attempt to connect via a button click:
private OnClickListener myListener = new OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.main_btnYes:
connectToNXT(); // connect to NXT
myIntent = new Intent(v.getContext(), SelectSession.class);
startActivityForResult(myIntent, 0);
break;
case R.id.main_btnNo:
myIntent = new Intent(v.getContext(), ExitScreen.class);
startActivityForResult(myIntent, 0);
break;
}
}
};
Here is the connectToNXT() method:
The crash occurs here: Set bondedDevices = btAdapter.getBondedDevices();
private void connectToNXT() {
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
**Set<BluetoothDevice> bondedDevices = btAdapter.getBondedDevices();**
BluetoothDevice nxtDevice = null;
}
Anyone know why this would cause a crash?
Also, since I'm pretty new to android and bluetooth(2 days :D), could someone be kind enough to let me know of a good tutorial for android bluetooth?
Thanks,
Rich.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
NXT的MAC地址可以在设置菜单/NXT版本下找到。在此选项下,ID号就是MAC地址。无需USB!
The NXT's MAC Address can be found under the setting menu /NXT version. under this option, the ID number is the MAC Address .No need for USB!
我的猜测:NullPointerException。您的 btadapter 变量为 null,并且您尝试从中调用方法,这会导致 NullPointerException。
但是你不能提供堆栈跟踪或其他东西吗?
没有日志就很难知道发生了什么。
如果您使用 eclipse,请转到 Window/Show/Android/Logcat。
您还可以在调试模式下运行应用程序,并在应用程序崩溃的行之前放置一个断点,然后查看 btadapter 是否有值。
My guess : a NullPointerException. Your btadapter variable is null and you try to call a method from it, which causes a NullPointerException.
But can't you provide a stacktrace or something ?
Without a log it's hard to know what hapenned.
If you use eclipse, go to Window/Show/Android/Logcat.
You can also run your application in debug mode and put a breakpoint just before the line where your app crash and see if btadapter has a value.
嗯,在尝试了各种代码之后(没有一个有效......),我设法从它们中提取一些代码并让它在我的 NXT 上工作。
我使用的是固件为 2.2.1 的 Samsung Galaxy Ace(android 操作系统)智能手机,
这是有效的连接方法。如果您愿意,请随意使用它。
声明:
连接方法
通过电子邮件给我发送电子邮件:[电子邮件受保护](如果您愿意)。
富有的。
Well, after trying out various pieces of code(none of which worked...), I managed to take bits from each of them and get it to work on my NXT.
I'm using the Samsung Galaxy Ace(android OS) smartphone on firmware 2.2.1
Here is the connect method that works. Feel free to use it if you want.
Declarations:
Connect Method
Email me at [email protected] if you want.
Rich.