Android app开发:蓝牙socket通信

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值