Android13 BluetoothSocket connect流程分析

BluetoothSocket是一个用于在Android设备之间进行蓝牙通信的类。它提供了一种通过蓝牙连接进行数据传输的方法,BluetoothSocket的connect方法代码如下:

//packages/modules/Bluetooth/framework/java/android/bluetooth/BluetoothSocket.java
public final class BluetoothSocket implements Closeable {
    public void connect() throws IOException {
        if (mDevice == null) throw new IOException("Connect is called on null device");


        try {
            if (mSocketState == SocketState.CLOSED) throw new IOException("socket closed");
            IBluetooth bluetoothProxy =
                    BluetoothAdapter.getDefaultAdapter().getBluetoothService();
            if (bluetoothProxy == null) throw new IOException("Bluetooth is off");
            IBluetoothSocketManager socketManager = bluetoothProxy.getSocketManager(); //取得IBluetoothSocketManager 
            if (socketManager == null) throw new IOException("bt get socket manager failed");
            mPfd = socketManager.connectSocket(mDevice, mType, mUuid, mPort, getSecurityFlags()); //调用IBluetoothSocketManager 的connectSocket连接Socket
            synchronized (this) {
                if (DBG) Log.d(TAG, "connect(), SocketState: " + mSocketState + ", mPfd: " + mPfd);
                if (mSocketState == SocketState.CLOSED) throw new IOException("socket closed");
                if (mPfd == null) throw new IOException("bt socket connect failed");
                FileDescriptor fd = mPfd.getFileDescriptor();
                mSocket = new LocalSocket(fd); 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值