Android app开发:蓝牙socket通信

本文展示了Android平台下使用BluetoothSocket进行蓝牙通信的Server和Client端核心代码实现。Server端通过实现Runnable接口,监听并接收来自Client的数据,接收到数据后进行处理并回传消息。Client端则负责连接指定设备,并发送数据到Server。在异常处理中,两端均包含重新启动蓝牙服务的逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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());
        }
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值