package com.blakequ.bluetooth_manager_lib.connect.multiple; import android.annotation.TargetApi; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGattCallback; import android.bluetooth.BluetoothManager; import android.bluetooth.BluetoothProfile; import android.content.Context; import com.blakequ.bluetooth_manager_lib.BleManager; import com.blakequ.bluetooth_manager_lib.connect.BluetoothSubScribeData; import com.blakequ.bluetooth_manager_lib.connect.ConnectConfig; import com.blakequ.bluetooth_manager_lib.connect.ConnectState; import com.blankj.utilcode.util.LogUtils; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; /** * Copyright (C) BlakeQu All Rights Reserved *

* Licensed under the blakequ.com License, Version 1.0 (the "License"); * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *

* author : quhao
* date : 2016/8/19 9:49
* last modify author :
* version : 1.0
* description:manager multiple bluetooth device connect *

* 1.must set uuid for subscribe bluetooth device data, {@link #setServiceUUID(String)}, {@link #addBluetoothSubscribeData(BluetoothSubScribeData)}
* 2.register callback of bluetooth notify by {@link #setBluetoothGattCallback(BluetoothGattCallback)}
* 3.add device to connect queue, {@link #addDeviceToQueue(String)} or {@link #addDeviceToQueue(String[])}, {@link #removeDeviceFromQueue(String)}
* 4.start auto connect one by one, {@link #startConnect()}
* 5.close all connect, {@link #close(String)}, {@link #closeAll()}
*

*/ @TargetApi(18) public final class MultiConnectManager extends ConnectRequestQueue { private static final String TAG = "MultiConnectManager"; private static MultiConnectManager INSTANCE; private BluetoothManager bluetoothManager; private static String serviceUUID; private BluetoothGattCallback mBluetoothGattCallback; private final Queue subscribeQueue; private static Object obj = new Object(); private MultiConnectManager(Context context){ super(context); bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE); subscribeQueue = new ConcurrentLinkedQueue(); BleManager.getBleParamsOptions(); } public static MultiConnectManager getInstance(Context context){ //双重锁 if (INSTANCE == null){ synchronized (obj){ if (INSTANCE == null){ INSTANCE = new MultiConnectManager(context); } } } return INSTANCE; } public List getConnectedDevice() { List devices = bluetoothManager.getConnectedDevices(BluetoothProfile.GATT); if (!isEmpty(devices)){ List newDevices = new ArrayList(); for (BluetoothDevice device: devices){ if (getDeviceState(device.getAddress()) == ConnectState.CONNECTED) { newDevices.add(device); }else { LogUtils.i("Not exist connected device in queue "+device.getAddress()); } } return newDevices; } return Collections.EMPTY_LIST; } /** * register callback of bluetooth notify * @param callback */ public void setBluetoothGattCallback(BluetoothGattCallback callback){ this.mBluetoothGattCallback = callback; } /** * add subscribe data while read or write characteristic(or descriptor) after discover service. * if each device using different config, you should invoke {@link #cleanSubscribeData()} to clean queue before using {@link #startSubscribe(BluetoothGatt)} * @param data * @see #cleanSubscribeData() * @see #startSubscribe(BluetoothGatt) * @see #setServiceUUID(String) */ public void addBluetoothSubscribeData(BluetoothSubScribeData data){ subscribeQueue.add(data); } /** * clean subscribe list * @see #addBluetoothSubscribeData(BluetoothSubScribeData) */ public void cleanSubscribeData(){ subscribeQueue.clear(); } /** * set bluetooth service uuid, can not be null * @see #addBluetoothSubscribeData(BluetoothSubScribeData) * @param serviceUUID */ public void setServiceUUID(String serviceUUID){ this.serviceUUID = serviceUUID; } @Override protected BluetoothGattCallback getBluetoothGattCallback() { return mBluetoothGattCallback; } @Override protected String getServiceUUID() { return serviceUUID; } @Override protected Queue getSubscribeDataQueue() { return subscribeQueue; } @Deprecated public void setMaxConnectDeviceNum(int number){ ConnectConfig.maxConnectDeviceNum = number; } }