Bluetooth data transfer between two Android devices

后端 未结 5 1039
盖世英雄少女心
盖世英雄少女心 2021-02-01 10:19

I have been following this Android guide for Bluetooth communication

To explain exactly what I want to do, when the two devices are paired, two different activities ope

相关标签:
5条回答
  • 2021-02-01 10:24
    // Enter code here
    
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            byte[] writeBuf = (byte[]) msg.obj;
            int begin = (int)msg.arg1;
            int end = (int)msg.arg2;
    
            switch(msg.what) {
                case 1:
                    String writeMessage = new String(writeBuf);
                    writeMessage = writeMessage.substring(begin, end);
                    break;
            }
        }
    };
    
    0 讨论(0)
  • 2021-02-01 10:27

    Check out the BluetoothChat example that Google provides in the SDK. It'll show you how to implement basic sending of text over bluetooth.

    0 讨论(0)
  • 2021-02-01 10:28

    Can you please describe the error as seen by you?

    As informed by Ankit and Addy, BlueToothChat is the best code for you to refer. Conduct an experiment by loading it on 2 android devices - use one as server other as client to exchange the messages between them. Such experiment will help you to understand it's code and decide your coding logic.

    0 讨论(0)
  • 2021-02-01 10:29

    mHandler is used for passing message from your BluetoothHandle.java to your Activity. This will help you to update messages on your screen which are returned by BluetoothHandler.

    you have to create mHandler from your activity and call your handler like this -

    mBluetoothHandler = new BluetoothHandler(this, mHandler);

    and your BluetoothHandler.java has constructor like this -

    public class BluetoothHandler { 
    
        public BluetoothHandler(Context context, Handler handler) {
                mAdapter = BluetoothAdapter.getDefaultAdapter();
                mState = STATE_NONE;
                mHandler = handler;
                mcontext = context;
       }
    
    }
    

    For more details, please refer Android sample project of Bluetooth Chat . You can also use this link : http://myandroidappdevelop.blogspot.in/2013/05/bluetooth-chat-example.html

    0 讨论(0)
  • 2021-02-01 10:45

    You can also try the tutorial example here

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