Android - 通过蓝牙将 Samsung Galaxy Ace 连接到 NXT

发布于 2024-11-26 14:34:11 字数 1165 浏览 3 评论 0原文

好的,我已经向清单文件添加了权限并配对了我的设备,但我在这里遇到了崩溃: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悲歌长辞 2024-12-03 14:34:11

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!

日裸衫吸 2024-12-03 14:34:11

我的猜测: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.

注定孤独终老 2024-12-03 14:34:11

嗯,在尝试了各种代码之后(没有一个有效......),我设法从它们中提取一些代码并让它在我的 NXT 上工作。

我使用的是固件为 2.2.1 的 Samsung Galaxy Ace(android 操作系统)智能手机,

这是有效的连接方法。如果您愿意,请随意使用它。

声明:

    // This is the NXT Mac Address. Each device has a specific Mac. Find it in the Build output when uploading
    // your NXT app to the brick using a USB cable. MUST USE USB CABLE TO SEE MAC ADDRESS!
    final String nxtMac = "00:16:53:05:3C:F5";
    //Important: This is the data stream used to communicate with the NXT.
    private DataOutputStream nxtDos = null;
    BluetoothAdapter localAdapter;
    BluetoothSocket nxtSocket;
    boolean success = false;

连接方法

    //Connect to NXT
    public boolean connectToNXT() {         
        // get the BluetoothDevice of the NXT
        BluetoothDevice nxt = localAdapter.getRemoteDevice(nxtMac);
        //Try to connect to the nxt
        try {
            nxtSocket = nxt.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
            nxtSocket.connect();
            //Get the Data stream
            nxtDos = new DataOutputStream(nxtSocket.getOutputStream());
            success = true;
        } catch (IOException e) {
            Log.d("Bluetooth", "Err: Device not found or cannot connect");
            success = false;
        }
        return success;
    }

通过电子邮件给我发送电子邮件:[电子邮件受保护](如果您愿意)。

富有的。

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:

    // This is the NXT Mac Address. Each device has a specific Mac. Find it in the Build output when uploading
    // your NXT app to the brick using a USB cable. MUST USE USB CABLE TO SEE MAC ADDRESS!
    final String nxtMac = "00:16:53:05:3C:F5";
    //Important: This is the data stream used to communicate with the NXT.
    private DataOutputStream nxtDos = null;
    BluetoothAdapter localAdapter;
    BluetoothSocket nxtSocket;
    boolean success = false;

Connect Method

    //Connect to NXT
    public boolean connectToNXT() {         
        // get the BluetoothDevice of the NXT
        BluetoothDevice nxt = localAdapter.getRemoteDevice(nxtMac);
        //Try to connect to the nxt
        try {
            nxtSocket = nxt.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
            nxtSocket.connect();
            //Get the Data stream
            nxtDos = new DataOutputStream(nxtSocket.getOutputStream());
            success = true;
        } catch (IOException e) {
            Log.d("Bluetooth", "Err: Device not found or cannot connect");
            success = false;
        }
        return success;
    }

Email me at [email protected] if you want.

Rich.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文