Android 蓝牙开发(五)OPP接收文件

本文详细介绍了Android蓝牙接收文件的过程,从打开蓝牙到创建BluetoothServerSocket,监听连接,接收文件,处理用户确认,直到文件接收完成的整个流程。涉及到BluetoothOppReceiver、BluetoothOppService、BluetoothOppObexServerSession等多个组件及其交互。同时提到了文件存储路径、用户确认通知以及超时处理机制。

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

转载请注明出处:http://blog.youkuaiyun.com/vnanyesheshou/article/details/70340027

本文已授权微信公众号 fanfan程序媛独家发布 扫一扫文章底部的二维码或在微信搜索 fanfan程序媛 即可关注

这篇文章主要说一下手机是如何通过蓝牙接收文件的。


1 创建rfcomm层sever

要想通过蓝牙接收文件,首先要打开蓝牙。所以先从打开蓝牙进行分析。
BluetoothOppReceiver在AndroidManifest.xml文件中进行了注册,其中action包括”android.bluetooth.adapter.action.STATE_CHANGED”,也就是它会监听蓝牙状态的改变。

f (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)){
    if (BluetoothAdapter.STATE_ON == intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) {
    context.startService(new Intent(context, BluetoothOppService.class));
    //......
    }
}

监听到蓝牙开启,BluetoothOppReceiver会打开服务BluetoothOppService。接着看BluetoothOppService。

mAdapter = BluetoothAdapter.getDefaultAdapter();  //创建蓝牙适配器
mSocketListener = new BluetoothOppRfcommListener(mAdapter);
mObserver = new BluetoothShareContentObserver(); //监听数据库的变化
getContentResolver().registerContentObserver(BluetoothShare.CONTENT_URI, true, mObserver);
mNotifier = new BluetoothOppNotification(this); // 创建BluetoothOppNotification对象
mNotifier.mNotificationMgr.cancelAll();  //取消所有状态栏通知
mNotifier.updateNotification();
IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mBluetoothReceiver, filter); //注册广播接受者,监听蓝牙状态的改变synchronized (BluetoothOppService.this) {
   
   
    if (mAdapter == null) {
  
  //检查蓝牙是否可用。
        Log.w(TAG, "Local BT device is not enabled");
        } else {
         startListener();
    }
}

在BluetoothOppService的onCreate()函数中,创建了BluetoothOppRfcommListener对象和BluetoothOppNotification对象。
BluetoothOppRfcommListener类主要用来创建BluetoothServerSocket,接收其他设备的连接。
BluetoothOppNotification则用来弹状态栏通知,显示发送、接收文件,及发送和接收进度等。
BluetoothOppService中还注册了observer,监测数据库的变化。
注册广播接受者,监听蓝牙状态的改变,开启蓝牙时,调用startSocketListener->mSocketListener.start,创建socket并开始监听其他设备的连接。关闭蓝牙时,调用mSocketListener.stop,关闭BluetoothServerSocket。
接着看startListener();

private void startListener() {
    if (!mListenStarted) {
  
  //mListenStarted初始值为false
        if (mAdapter.isEnabled()) {
  
  //检查蓝牙是否开启
              mHandler.sendMessage(mHandler.obtainMessage(START_LISTENER));
              mListenStarted = true;
        }
    }
}

handler接收到消息START_LISTENER,则调用

//开始socket监听。
private void startSocketListener() {
    mSocketListener.start(mHandler);
}

mSocketListener为创建的BluetoothOppRfcommListener对象,调用其start()方法,携带handler对象。在start()内将此handler存为全局变量,用于之后向BluetoothOppService发送消息。下一步创建线程mSocketAcceptThread ,并开始运行该线程。

public synchronized boolean start(Handler callback) {
    if (mSocketAcceptThread == null) {
        mCallback = callback;
        //创建线程mSocketAcceptThread
        mSocketAcceptThread = new Thread(TAG) {
            public void run() {
                if (Constants.USE_TCP_DEBUG) { //这个是实用tcp协议,可忽略。
                } else {
                    boolean serverOK = true;
                    //可能创建失败,尝试10次
                    for (int i = 0; i < CREATE_RETRY_TIME && !mInterrupted; i++) {
                        try {
                            mBtServerSocket = mAdapter.listenUsingInsecureRfcommWithServiceRecord("OBEX Object Push", BluetoothUuid.ObexObjectPush.getUuid());
                        } catch (IOException e1) {
                            serverOK = false;
                        }
                        if (!serverOK) {
                            synchronized (this) {
                                try {
  
  //等待300ms
                                    Thread.sleep(300);
                                } catch (InterruptedException e) {
                                    mInterrupted = true;
                                }
                            }
                        } else { //创建BluetoothServerSocket成功,退出for循环。
                            break;
                        }
                    }
                    if (!serverOK) {
                        mInterrupted = true;
                    }
                    BluetoothSocket clientSocket;
                    while (!mInterrupted) {
                        try {
                            BluetoothServerSocket sSocket = mBtServerSocket;
                            if (sSocket ==null) {
                                mInterrupted = true;
                            } else {
  
  //接收客户端的连接
                                clientSocket = sSocket.accept();
                                BluetoothOppRfcommTransport transport = new BluetoothOppRfcommTransport(clientSocket);
                                Message msg = Message.obtain();
                                msg.setTarget(mCallback);
                                msg.what = MSG_INCOMING_BTOPP_CONNECTION;
                                msg.obj = transport;
                                msg.sendToTarget();
                            }
                        } catch (IOException e) {
                            try {
                                Thread.sleep(500);
                            } catch (InterruptedException ie) {}
                        }
                    }
                }
            }
        };
        mInterrupted = false;
        if(!Constants.USE_TCP_SIMPLE_SERVER) {
  
  //该值为false,没有使用tcp相关的。
            mSocketAcceptThread.start();  //mSocketAcceptThread线程开始运行
        }
    }
    return true;
}

mSocketAcceptThread中创建BluetoothServerSocket,创建BluetoothServerSocket可能会失败,这里做的保护措施是进行10次尝试。listenUsingInsecureRfcommWithServiceRecord()函数表示此rfcomm连接是不安全的,所以连接时不会进行配对。
sSocket.accept()是阻塞的,等待远程设备的连接。
当与远程设备连接成功后,发送消息MSG_INCOMING_BTOPP_CONNECTION。mCallback对应BluetoothOppService的mHandler。所以在BluetoothOppService中看是如何处理此消息到。

private Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
        case BluetoothOppRfcommListener.MSG_INCOMING_BTOPP_CONNECTION:
            ObexTransport transport = (ObexTransport)msg.obj;
            //传入连接策略:
            //2. 如果正在通过向外蓝牙分享文件,保持20秒(1秒* 20次)
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值