android BluetoothChat use send receive methods from another class

后端 未结 2 356
小蘑菇
小蘑菇 2021-01-28 12:31

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 t

相关标签:
2条回答
  • 2021-01-28 13:18

    BluetoothChat class is an activity. Activity objects are managed by Android OS (created, onCreate, onResume and other methods are called).

    So, you shoudn't do something like this:

    BluetoothChat firstClass = new BluetoothChat();
    

    There are couple of ways how you can reuse this functionality

    a) Copy from BluetoothChat and paste it to Config (let's for one second forget about code duplication).

    You will need to copy the code from onCreate, onStart and other methods which manages mBluetoothAdaptor method

    b) If you are going to keep both BluetoothChat activity and Config activity, you can move reusable code to some helper class (which isn't activity) and use it from both activities.

    P.S. I would recommend to read up a little bit about activities, their lifecycle, because without this knowledge it will be very hard to develop any android app.

    0 讨论(0)
  • 2021-01-28 13:25

    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

    0 讨论(0)
提交回复
热议问题