0
点赞
收藏
分享

微信扫一扫

Android app开发:蓝牙socket通信

思考的鸿毛 2022-04-08 阅读 37
androidjava

server端核心代码:

public class BluetoothServer implements Runnable {
    private static final String TAG = "BluetoothServer";
    private final Handler mHandler;
    private final BluetoothSocket mSocket;
    private InputStream mInputStream;
    private OutputStream mOutputStream;

    public BluetoothServer(Handler handler, BluetoothSocket bluetoothSocket) {
        mHandler = handler;
        mSocket = bluetoothSocket;
    }

    @Override
    public void run() {
        try {
            mInputStream = mSocket.getInputStream();
            mOutputStream = mSocket.getOutputStream();
            int len;
            String content;
            while (mSocket.isConnected()) {
                byte[] bt = new byte[100];
                len = mInputStream.read(bt);
                content = new String(bt, 0, len, StandardCharsets.UTF_8);
                Log.d(TAG, "wjz debug run: 收到客户端数据: " + content);
                sendMsg();
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                mSocket.close();
                mInputStream.close();
                mOutputStream.close();
            } catch (Exception ee) {
                ee.printStackTrace();
                Log.e(TAG, "wjz debug run: 111 error is " + e.getMessage());
            }
            mHandler.sendEmptyMessage(MSG_RESTART_BT_SERVER_SOCKET);
            Log.e(TAG, "wjz debug run: 222 error is " + e.getMessage());
        }
    }

    private void sendMsg() {
        try {
            mOutputStream.write("data from bt server".getBytes(StandardCharsets.UTF_8));
            mOutputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
            Log.e(TAG, "wjz debug sendMsg: error is " + e.getMessage());
        }
    }
}

注意server初始化放在子线程中。

client端核心代码:

public class BluetoothClient implements Runnable {
    private static final String TAG = "BluetoothClient";
    private final Handler mHandler;
    private BluetoothSocket mSocket;
    private OutputStream mOutputStream;

    public BluetoothClient(Handler handler, String remoteAddress) {
        mHandler = handler;
        BluetoothDevice remoteDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(remoteAddress);
        try {
            mSocket = remoteDevice.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        try {
            mSocket.connect();
            InputStream inputStream = mSocket.getInputStream();
            mOutputStream = mSocket.getOutputStream();
            int len;
            String content;
            while (mSocket.isConnected()) {
                sendMsg();
                byte[] bt = new byte[100];
                len = inputStream.read(bt);
                content = new String(bt, 0, len, StandardCharsets.UTF_8);
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG, "wjz debug run: error is " + e.getMessage());
        }
    }

    private void sendMsg() {
        try {
            mOutputStream.write("data from bt client".getBytes(StandardCharsets.UTF_8));
            mOutputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
            Log.e(TAG, "wjz debug sendMsg: error is " + e.getMessage());
        }
    }
}
举报

相关推荐

0 条评论