android BluetoothChat 使用来自另一个类的发送接收方法 [英] android BluetoothChat use send receive methods from another class

查看:30
本文介绍了android BluetoothChat 使用来自另一个类的发送接收方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我使用 BluetoothChat 示例与 arduino 通信.到目前为止一切正常,现在我想访问 sendMessage() 方法,以便能够不从主类而是从我创建的另一个类发送数据.这里是BluetoothChat.java中的BluetoothChat发送函数

Hello im using the BluetoothChat example in order to communicate with an arduino. Everything works so far and now i want to access the sendMessage() method in order to be able to send data not from the main class but from another that ive created. Here is the BluetoothChat send function in the BluetoothChat.java

private void sendMessage(String message) {
    // Check that we're actually connected before trying anything
    if (mChatService.getState() != BluetoothChatService.STATE_CONNECTED) {
        Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT).show();
        return;
    }

    // Check that there's actually something to send
    if (message.length() > 0) {
        // Get the message bytes and tell the BluetoothChatService to write
        byte[] send = message.getBytes();
        mChatService.write(send);
        // Reset out string buffer to zero and clear the edit text field
        mOutStringBuffer.setLength(0);
        mOutEditText.setText(mOutStringBuffer);
    }
}

现在我想从我的 Config.java 访问它.这是我到目前为止所做的.我将 private void sendMessage 更改为 public 并在 Config.java 中添加了这些:

Now i want to access it from my Config.java . Here is what i have done so far. I changed the private void sendMessage to public and in the Config.java ive added these:

BluetoothChat firstClass = new BluetoothChat();

并在 onCreate this

and in onCreate this

firstClass.sendMessage("test");

虽然当我切换到 Config.java 活动时我没有收到任何错误,但应用程序崩溃了..
谁能帮我这个?谢谢!

Although im not getting any errors when im switching to the Config.java activity the app crashes..
Can anyone help me with this? Thanx!

日志猫

10-08 22:49:15.125: D/dalvikvm(1541): GC_EXTERNAL_ALLOC 释放 98K,47% 释放 2962K/5511K,外部 3943K/4883K,暂停 23 毫秒10-08 22:49:15.128:D/BluetoothSocket(1541):socket 已经处于关闭状态:android.bluetooth.BluetoothSocket@40535c8810-08 22:49:15.128:D/BluetoothSocket(1541):socket 已经处于关闭状态:android.bluetooth.BluetoothSocket@40523b1010-08 22:49:15.296: E/BluetoothChat(1541): -- 停止 --10-08 22:49:20.585:D/AndroidRuntime(1541):关闭虚拟机10-08 22:49:20.585: W/dalvikvm(1541): threadid=1: 线程退出时出现未捕获的异常 (group=0x40015578)10-08 22:49:20.589:E/AndroidRuntime(1541):致命异常:主要10-08 22:49:20.589: E/AndroidRuntime(1541): java.lang.NullPointerException10-08 22:49:20.589: E/AndroidRuntime(1541): 在 com.example.android.BluetoothChat.BluetoothChat.sendMessage(BluetoothChat.java:218)10-08 22:49:20.589: E/AndroidRuntime(1541): 在 com.example.android.BluetoothChat.Config.onItemSelected(Config.java:45)10-08 22:49:20.589: E/AndroidRuntime(1541): 在 android.widget.AdapterView.fireOnSelected(AdapterView.java:871)10-08 22:49:20.589: E/AndroidRuntime(1541): 在 android.widget.AdapterView.access$200(AdapterView.java:42)10-08 22:49:20.589: E/AndroidRuntime(1541): 在 android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:837)10-08 22:49:20.589: E/AndroidRuntime(1541): 在 android.os.Handler.handleCallback(Handler.java:587)10-08 22:49:20.589: E/AndroidRuntime(1541): 在 android.os.Handler.dispatchMessage(Handler.java:92)10-08 22:49:20.589: E/AndroidRuntime(1541): 在 android.os.Looper.loop(Looper.java:130)10-08 22:49:20.589: E/AndroidRuntime(1541): 在 android.app.ActivityThread.main(ActivityThread.java:3687)10-08 22:49:20.589: E/AndroidRuntime(1541): 在 java.lang.reflect.Method.invokeNative(Native Method)10-08 22:49:20.589: E/AndroidRuntime(1541): 在 java.lang.reflect.Method.invoke(Method.java:507)10-08 22:49:20.589: E/AndroidRuntime(1541): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)10-08 22:49:20.589: E/AndroidRuntime(1541): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)10-08 22:49:20.589: E/AndroidRuntime(1541): at dalvik.system.NativeStart.main(Native Method)

10-08 22:49:15.125: D/dalvikvm(1541): GC_EXTERNAL_ALLOC freed 98K, 47% free 2962K/5511K, external 3943K/4883K, paused 23ms 10-08 22:49:15.128: D/BluetoothSocket(1541): socket already in closing state:android.bluetooth.BluetoothSocket@40535c88 10-08 22:49:15.128: D/BluetoothSocket(1541): socket already in closing state:android.bluetooth.BluetoothSocket@40523b10 10-08 22:49:15.296: E/BluetoothChat(1541): -- ON STOP -- 10-08 22:49:20.585: D/AndroidRuntime(1541): Shutting down VM 10-08 22:49:20.585: W/dalvikvm(1541): threadid=1: thread exiting with uncaught exception (group=0x40015578) 10-08 22:49:20.589: E/AndroidRuntime(1541): FATAL EXCEPTION: main 10-08 22:49:20.589: E/AndroidRuntime(1541): java.lang.NullPointerException 10-08 22:49:20.589: E/AndroidRuntime(1541): at com.example.android.BluetoothChat.BluetoothChat.sendMessage(BluetoothChat.java:218) 10-08 22:49:20.589: E/AndroidRuntime(1541): at com.example.android.BluetoothChat.Config.onItemSelected(Config.java:45) 10-08 22:49:20.589: E/AndroidRuntime(1541): at android.widget.AdapterView.fireOnSelected(AdapterView.java:871) 10-08 22:49:20.589: E/AndroidRuntime(1541): at android.widget.AdapterView.access$200(AdapterView.java:42) 10-08 22:49:20.589: E/AndroidRuntime(1541): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:837) 10-08 22:49:20.589: E/AndroidRuntime(1541): at android.os.Handler.handleCallback(Handler.java:587) 10-08 22:49:20.589: E/AndroidRuntime(1541): at android.os.Handler.dispatchMessage(Handler.java:92) 10-08 22:49:20.589: E/AndroidRuntime(1541): at android.os.Looper.loop(Looper.java:130) 10-08 22:49:20.589: E/AndroidRuntime(1541): at android.app.ActivityThread.main(ActivityThread.java:3687) 10-08 22:49:20.589: E/AndroidRuntime(1541): at java.lang.reflect.Method.invokeNative(Native Method) 10-08 22:49:20.589: E/AndroidRuntime(1541): at java.lang.reflect.Method.invoke(Method.java:507) 10-08 22:49:20.589: E/AndroidRuntime(1541): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842) 10-08 22:49:20.589: E/AndroidRuntime(1541): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 10-08 22:49:20.589: E/AndroidRuntime(1541): at dalvik.system.NativeStart.main(Native Method)

推荐答案

您需要 Thread 或 AsyncTask 才能通过蓝牙从 Android 发送蚂蚁消息.看这个简单的例子:http://english.cxem.net/arduino/arduino5.php

You need Thread's or AsyncTask for send ant message from Android via Bluetooth. See this simple example: http://english.cxem.net/arduino/arduino5.php

这篇关于android BluetoothChat 使用来自另一个类的发送接收方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆