Bluetooth data transfer between two Android devices

让人想犯罪 __ 提交于 2019-12-02 18:33:40

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

You can also try the tutorial example here

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.

Ankit

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

user3420105
// 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;
        }
    }
};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!