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)