安卓BluetoothChat利用发送接收来自其他类的方法方法、BluetoothChat

2023-09-04 04:44:35 作者:听风念故人

使用,以便与一个Arduino沟通的BluetoothChat示例Hello即时通讯。一切工作至今,现在我要访问的sendMessage()方法,以便能够不将数据从主类,但是从另外一个香港专业教育学院创建的。这里是BluetoothChat发送功能的BluetoothChat.java

 私人无效的sendMessage(字符串消息){
    //检查我们尝试任何事情之前,实际上是在连接
    如果(mChatService.getState()!= BluetoothChatService.STATE_CONNECTED){
        Toast.makeText(这一点,R.string.not_connected,Toast.LENGTH_SHORT).show();
        返回;
    }

    //检查是否有实际上的东西送
    如果(message.length()大于0){
        //获取消息的字节,并告诉BluetoothChatService写
        byte []的发送= message.getBytes();
        mChatService.write(发送);
        //复位输出字符串缓冲区为零,并清除在编辑文本字段
        mOutStringBuffer.setLength(0);
        mOutEditText.setText(mOutStringBuffer);
    }
}
 

现在我想从我的Config.java访问它。以下是我迄今所做的。我改变了私人无效SendMessage函数公众和Config.java香港专业教育学院添加了这些:

  BluetoothChat的Firstclass =新BluetoothChat();
 

和中的onCreate这个

  firstClass.sendMessage(测试);
 
安卓系统的手机怎么显示手机接收离线消息而不是微信接收离线消息

虽然IM没有得到任何错误,当IM切换到Config.java活动的应用程序崩溃.. 谁能帮我这个? 感谢名单!

logcat的

  

10-08 22:49:15.125:D / dalvikvm(1541):GC_EXTERNAL_ALLOC释放98K,47%免费2962K / 5511K,外部3943K / 4883K,暂停23MS   10-08 22:49:15.128:D /的BluetoothSocket(1541):插座已关闭状态:android.bluetooth.BluetoothSocket@40535c88   10-08 22:49:15.128:D /的BluetoothSocket(1541):插座已关闭状态:android.bluetooth.BluetoothSocket@40523b10   10-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):主题ID = 1:螺纹退出与未捕获的异常(组= 0x40015578)   10-08 22:49:20.589:E / AndroidRuntime(1541):致命异常:主要   10-08 22:49:20.589:E / AndroidRuntime(1541):显示java.lang.NullPointerException   10-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(本机方法)   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):在dalvik.system.NativeStart.main(本机方法)

解决方案

您需要螺纹的或AsyncTask的通过蓝牙发送蚂蚁消息的Andr​​oid。 看到这个简单的例子: http://english.cxem.net/arduino/arduino5.php

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

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

and in onCreate this

firstClass.sendMessage("test");

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

logcat

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)

解决方案

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