蓝牙自动连接实现

实现的主要功能(蓝牙配对成功如何与远程设备一直连接)

1.当蓝牙配对成功连接时,断开远程端设备会自动连接
2.当设备长时间锁屏会导致CachedBluetoothDevice自动清空,如果蓝牙断开就不会自动连接的处理

实现步骤

监控蓝牙断开连接状态时发生哪些改变媒体音频与通话音频(a2dp与hfp)
1.在com.android.bluetooth.btservice.AdapterProperties这个类中蓝牙连接状态

void sendConnectionStateChange(BluetoothDevice device, int profile, int state, int prevState) {
        if (!validateProfileConnectionState(state) ||
                !validateProfileConnectionState(prevState)) {
            // Previously, an invalid state was broadcast anyway,
            // with the invalid state converted to -1 in the intent.
            // Better to log an error and not send an intent with
            // invalid contents or set mAdapterConnectionState to -1.
            errorLog("Error in sendConnectionStateChange: "
                    + "prevState " + prevState + " state " + state);
            return;
        }

        synchronized (mObject) {
            updateProfileConnectionState(profile, state, prevState);

            if (updateCountersAndCheckForConnectionStateChange(state, prevState)) {
                setConnectionState(state);
                //留意BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED
                Intent intent = new Intent(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
                intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
                intent.putExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE,
                        convertToAdapterState(state));
                intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_CONNECTION_STATE,
                        convertToAdapterState(prevState));
                intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
                mService.sendBroadcastAsUser(intent, UserHandle.ALL,
                        mService.BLUETOOTH_PERM);
                Log.d(TAG, "CONNECTION_STATE_CHANGE: " + device + ": "
                        + prevState + " -> " + state);
            }
        }
    }

2.在com.android.bluetooth.a2dpsink.A2dpSinkStateMachine这个类中蓝牙连接状态

/** Handles A2DP connection state change intent broadcasts. */
    private class IntentBroadcastHandler extends Handler {

        private void onConnectionStateChanged(BluetoothDevice device, int prevState, int state) {
            Intent intent = new Intent(BluetoothA2dpSink.ACTION_CONNECTION_STATE_CHANGED);
            intent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, prevState);
            intent.putExtra(BluetoothProfile.EXTRA_STATE, state);
            intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
//FIXME            intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
            mContext.sendBroadcast(intent, ProfileService.BLUETOOTH_PERM);
//留意android.bluetooth.a2dp.sink... 及保存参数
            intent = new Intent("android.bluetooth.a2dp.sink.profile.action.CONNECTION_STATE_CHANGED");
            intent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, prevState);
            intent.putExtra(BluetoothProfile.EXTRA_STATE, state);
            intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
            mContext.sendBroadcast(intent, ProfileService.BLUETOOTH_PERM);

            log("Connection state " + device + ": " + prevState + "->" + state);
            mService.notifyProfileConnectionStateChanged(device, BluetoothProfile.A2DP_SINK,
                    state, prevState);
        }

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_CONNECTION_STATE_CHANGED:
                    onConnectionStateChanged((BluetoothDevice) msg.obj, msg.arg1, msg.arg2);
                    break;
            }
        }
    }

3.在com.android.bluetooth.hfpclient.HeadsetClientStateMachine这个类中蓝牙连接状态

// This method does not check for error condition (newState == prevState)
    private void broadcastConnectionState(BluetoothDevice device, int newState, int prevState) {
        Log.d(TAG, "Connection state " + device + ": " + prevState + "->" + newState);
        /*
         * Notifying the connection state change of the profile before sending
         * the intent for connection state change, as it was causing a race
         * condition, with the UI not being updated with the correct connection
         * state.
         */
        mService.notifyProfileConnectionStateChanged(device, BluetoothProfile.HEADSET_CLIENT,
                newState, prevState);
//留意
        Intent intent = new Intent(BluetoothHeadsetClient.ACTION_CONNECTION_STATE_CHANGED);
        intent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, prevState);
        intent.putExtra(BluetoothProfile.EXTRA_STATE, newState);
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
...
        mService.sendBroadcast(intent, ProfileService.BLUETOOTH_PERM);
    }

广播监听com.android.settings.bluetooth.DockEventReceiver

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent == null)
            return;
     ...

        } else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) {
            int btState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
            Log.d(TAG,"szjjyh ACTION_STATE_CHANGED btState = "+btState);
//蓝牙关闭时停止服务
            if (btState == BluetoothAdapter.STATE_OFF){
                Intent intent2 = new Intent(context, BluetoothConnectService.class);
                context.stopService(intent2);
            }
            if (btState != BluetoothAdapter.STATE_TURNING_ON) {
                Intent i = new Intent(intent);
                i.setClass(context, DockService.class);
                beginStartingService(context, i);
            }
//这下面就是上面留意的几个action及相应的参数
        }else if (BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED.equals(intent.getAction())) {
            int newState = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, 0);
            int oldState = intent.getIntExtra(BluetoothAdapter.EXTRA_PREVIOUS_CONNECTION_STATE, 0);
            changeDeviceStatus(device,newState,oldState,intent,context);
        }else if ("android.bluetooth.a2dp.sink.profile.action.CONNECTION_STATE_CHANGED".equals(intent.getAction())) {
            int newState = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, 0);
            int oldState = intent.getIntExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, 0);
            changeDeviceStatus(device,newState,oldState,intent,context);
        }else if (BluetoothHeadsetClient.ACTION_CONNECTION_STATE_CHANGED.equals(intent.getAction())) {
            int newState = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, 0);
            int oldState = intent.getIntExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, 0);
            changeDeviceStatus(device,newState,oldState,intent,context);
        }
    }

    public static void changeDeviceStatus(BluetoothDevice device,int newState, int oldState ,Intent intent,Context context) {
        if (!Utils.isAutoConnectBluetooth()) {
            return;
        }
        Log.d(TAG,"szjjyh changeDeviceStatus newState = "+newState+" oldState = "+oldState+
                "device = "+device.getAddress());
        synchronized (sStartingServiceSync) {
            if (BluetoothAdapter.getDefaultAdapter().isEnabled()) {
                if (oldState==BluetoothProfile.STATE_CONNECTING&&newState == BluetoothProfile.STATE_CONNECTED) {
//留意saveConnectDevice
                    LocalBluetoothPreferences.saveConnectDevice(context,device.getAddress());
                    Intent intent2 = new Intent(context, BluetoothConnectService.class);
                    context.stopService(intent2);
                }else if ((oldState==BluetoothProfile.STATE_CONNECTED||oldState==BluetoothProfile.STATE_CONNECTING)
                        &&newState == BluetoothProfile.STATE_DISCONNECTED){
                    if (LocalBluetoothPreferences.getConnectDevice(context,"").equals(device.getAddress())) {
                        Intent i = new Intent(intent);
                        i.setClass(context, BluetoothConnectService.class);
                        beginStartingService(context, i);
                    }
                }else if (oldState==BluetoothProfile.STATE_DISCONNECTING&&newState == BluetoothProfile.STATE_DISCONNECTED){
                    Intent intent2 = new Intent(context, BluetoothConnectService.class);
                    context.stopService(intent2);
                }else if (oldState==BluetoothProfile.STATE_CONNECTED&&newState == BluetoothProfile.STATE_DISCONNECTING){
                    LocalBluetoothPreferences.saveConnectDevice(context,"");
                }
            }
        }
    }

com.android.settings.bluetooth.LocalBluetoothPreferences存储值

    static void saveConnectDevice(Context context, String addr) {
        SharedPreferences.Editor editor = getSharedPreferences(context).edit();
        editor.putString(KEY_CONNECT_DEVICE , addr);
        editor.apply();
    }

com.android.settings.bluetooth.DeviceProfilesSettings点击断开连接时取消保存

public final class DeviceProfilesSettings extends DialogFragment implements
        CachedBluetoothDevice.Callback, DialogInterface.OnClickListener, OnClickListener {
...
      Button cancle = (Button) mRootView.findViewById(R.id.cancle);
            cancle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                     mCachedDevice.unpair();
//留意
                        LocalBluetoothPreferences.saveConnectDevice(getContext(), "");
                        com.android.settings.bluetooth.Utils.updateSearchIndex(getContext(),
                        BluetoothSettings.class.getName(), mCachedDevice.getName(),
                        getString(R.string.bluetooth_settings),
                        R.drawable.ic_settings_bluetooth, false);
                        dismiss();
                }
        });
...
    @Override
    public void onClick(DialogInterface dialog, int which) {
        switch (which) {
            case DialogInterface.BUTTON_POSITIVE:
                EditText deviceName = (EditText) mRootView.findViewById(R.id.name);
                mCachedDevice.setName(deviceName.getText().toString());
                break;
            case DialogInterface.BUTTON_NEUTRAL:
                mCachedDevice.unpair();
//留意
                LocalBluetoothPreferences.saveConnectDevice(getContext(), "");
                com.android.settings.bluetooth.Utils.updateSearchIndex(getContext(),
                        BluetoothSettings.class.getName(), mCachedDevice.getName(),
                        getString(R.string.bluetooth_settings),
                        R.drawable.ic_settings_bluetooth, false);
                break;
        }
    }

}

创建服务com.android.settings.bluetooth.BluetoothConnectService

public class BluetoothConnectService extends Service {

    private static final String TAG = "BluetoothConnectService";
    private Timer timer;
    private LocalBluetoothAdapter mLocalAdapter;
    private CachedBluetoothDeviceManager mDeviceManager;
    private LocalBluetoothProfileManager mProfileManager;

    @Override
    public void onCreate() {
        super.onCreate();
//        isOpenTimer(true);
        LocalBluetoothManager manager = Utils.getLocalBtManager(this);
        if (manager == null) {
            Log.e(TAG, "szjjyh Can't get LocalBluetoothManager: exiting");
            return;
        }

        mLocalAdapter = manager.getBluetoothAdapter();
        mDeviceManager = manager.getCachedDeviceManager();
        mProfileManager =manager.getProfileManager();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "szjjyh onDestroy");
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "szjjyh onStartCommand");
        isOpenTimer(true);
        return START_STICKY;
    }

    private void isOpenTimer(boolean isOpenWindow) {
        if (isOpenWindow) {
            if (timer == null) {
                timer = new Timer();
                timer.scheduleAtFixedRate(new RefreshTask(), 0, 1000 * 60 * 1);
            }
        } else {
            if (timer != null) {
                timer.cancel();
                timer = null;
            }
        }
    }

    private class RefreshTask extends TimerTask {
        @Override
        public void run() {
            autoConnectDevice();
        }

    }

    private void autoConnectDevice() {
        Set<BluetoothDevice> bondedDevices = mLocalAdapter.getBondedDevices();
        if (bondedDevices == null) {
            stopSelf();
            return;
        }
        Log.d(TAG, "szjjyh RefreshTask bondedDevices ="+bondedDevices.size());
        for (BluetoothDevice device : bondedDevices) {
            CachedBluetoothDevice cachedDevice = mDeviceManager.findDevice(device);
            if (LocalBluetoothPreferences.getConnectDevice(this, "").equals(device.getAddress())) {
                if (cachedDevice == null) {
                    cachedDevice = mDeviceManager.addDevice(mLocalAdapter, mProfileManager, device);
                }
                int bondState = cachedDevice.getBondState();
                if (bondState == BluetoothDevice.BOND_BONDED&&!cachedDevice.isConnected()) {
                    cachedDevice.connect(false);
                } else  {
                    stopSelf();
                }
            }
        }
    }
}

蓝牙自动连接完毕经测试长时间灭屏重启远距离断开等场景再次检测到之前连接设备时会自动连接

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,591评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,448评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,823评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,204评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,228评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,190评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,078评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,923评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,334评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,550评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,727评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,428评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,022评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,672评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,826评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,734评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,619评论 2 354

推荐阅读更多精彩内容

  • 最近项目使用蓝牙,之前并没有接触,还是发现了很多坑,查阅了很多资料,说的迷迷糊糊,今天特查看官方文档。 说下遇到的...
    King9527阅读 1,794评论 0 1
  • Guide to BluetoothSecurity原文 本出版物可免费从以下网址获得:https://doi.o...
    公子小水阅读 7,977评论 0 6
  • 蓝牙 注:本文翻译自https://developer.android.com/guide/topics/conn...
    RxCode阅读 8,670评论 11 99
  • Android平台支持蓝牙网络协议栈,实现蓝牙设备之间数据的无线传输。本文档描述了怎样利用android平台提供的...
    Camming阅读 3,308评论 0 3
  • Android 平台包含蓝牙网络堆栈支持,凭借此项支持,设备能以无线方式与其他蓝牙设备交换数据。应用框架提供了通过...
    虎三呀阅读 767评论 0 1