Android BLE一次连接到多个设备

时间:2015-12-12 03:16:03

标签: android android-ble bluetooth-lowenergy

我已经看到很多关于如何故意连接到多个设备的问题。但在我的情况下,我只是想连接到一个硬件设备。

我有两个应该做同样事情的硬件设备。当他们通过BLE连接到我的应用程序时,他们就会有一个可以变成纯色的LED。当我只打开一个设备时,这一切都很好用。但是,当我打开其中两个设备然后尝试连接到一个设备时。 设备的两个' LED变得坚固。虽然我似乎没有从我没有打算连接的数据中获取任何数据。

我不认为这是设备的错。因为我在iOS上没有这个问题。我认为手机可能在某处记住以前连接过的设备吗?

对不起,这是很多代码。但我觉得整个班级都很重要。非常感谢任何帮助。

package com.roberts.croberts.orange;

import android.annotation.TargetApi;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.le.ScanCallback;

import android.bluetooth.le.ScanResult;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.util.Log;
import android.bluetooth.le.BluetoothLeScanner;

import java.util.ArrayList;
import java.util.List;


@TargetApi(21)
public class BluetoothRegulator {

    private static BluetoothRegulator instance = null;

    private Context context;
    private BluetoothLeScanner mLEScanner;

    private BluetoothDevice orangeDevice;
    //scanner stuff
    private Handler mHandler;

    // Stops scanning after 3 seconds.
    private static final long SCAN_PERIOD = 3000;

    //connected stuff
    private android.bluetooth.BluetoothManager mBluetoothManager;
    private BluetoothAdapter mBluetoothAdapter;
    private BluetoothGatt mBluetoothGatt;

    public ArrayList<BluetoothDevice> devices = new ArrayList<>();

    private Handler foundHandler = new Handler();
    private Handler servicesHandler = new Handler();

    private ScanCallback mScanCallback;

    public static BluetoothRegulator sharedInstance(){
        if(instance == null) {
            instance = new BluetoothRegulator();
            Log.i("chase", "created new instance");
        }
        return instance;
    }
    // Implements callback methods for GATT events that the app cares about.  For example,
    // connection change and services discovered.
    private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            if (newState == BluetoothProfile.STATE_CONNECTED) {
                mBluetoothGatt.discoverServices();
                Log.i(TAG, "BR: onconnectionsStateChanged Connected to GATT server.");
                // Attempts to discover services after successful connection.
            } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                Log.i(TAG, "Disconnected from GATT server.");
            }
        }

        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {

            if (status == BluetoothGatt.GATT_SUCCESS) {
                // Loops through available GATT Serviceokay so ees.
                for (BluetoothGattService gattService : mBluetoothGatt.getServices()) {
                    for (BluetoothGattCharacteristic gattCharacteristic : gattService.getCharacteristics()) {
                        mBluetoothGatt.setCharacteristicNotification(gattCharacteristic, true);
                        Log.i(TAG, mBluetoothGatt == null ? "mbluetooth is null" : "should be subscribed");
                    }
                }
                Log.i("chase", "did connect and discover devices");
            } else {
                Log.w(TAG, "Not Success onServicesDiscovered received: " + status);
                connect(orangeDevice);
            }
        }
        private Object getFieldFromObject(Object obj, String name){
            try {
                Field field = obj.getClass().getDeclaredField(name);
                field.setAccessible(true);
                return field.get(obj);

            }catch(Exception e){
                Log.i("chase", "e: "+e);
                return null;
            }
        }
        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt,
                                            BluetoothGattCharacteristic characteristic) {
            Log.i("BR: chase", "received data!");
        }
    };


    public void close() {
        if (mBluetoothGatt == null) {
            return;
        }
        mBluetoothGatt.close();
        mBluetoothGatt = null;
    }

    public boolean initialize(android.bluetooth.BluetoothManager btmanager, Context ctx) {
        mBluetoothManager = btmanager;
        context = ctx;
        mBluetoothAdapter = mBluetoothManager.getAdapter();
        if (mBluetoothAdapter == null) {
            Log.e(TAG, "Unable to obtain a BluetoothAdapter.");
            return false;
        }

        if (Build.VERSION.SDK_INT >= 21) {
            mLEScanner = mBluetoothAdapter.getBluetoothLeScanner();
            setUpCallBack();
        }
        return true;
    }

    public void scan() { //we call scan when they hit the connect button...
        // Stops scanning after a pre-defined scan period.
        Log.i("chase", "start scanning");
        devices = new ArrayList<>();
        if (mHandler == null) mHandler = new Handler();
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (Build.VERSION.SDK_INT < 21) {
                    mBluetoothAdapter.stopLeScan(mLeScanCallback);
                } else {
                    mLEScanner.stopScan(mScanCallback);
                }
            }
        }, SCAN_PERIOD);

        if (Build.VERSION.SDK_INT < 21) {
            mBluetoothAdapter.startLeScan(mLeScanCallback);
        } else {
            mLEScanner.startScan(mScanCallback);
        }
    }
    private void foundDevice(BluetoothDevice device){
        final String deviceName = device.getName();

        if (deviceName != null && deviceName.length() > 5 && (deviceName.substring(0, 6).equals("orange") || deviceName.substring(0, 6).equals("smartb"))) {
            for (BluetoothDevice d : devices){
                if (d.getAddress().equals(device.getAddress())){
                    return;
                }
            }
            mHandler.removeCallbacksAndMessages(null);
            devices.add(device);
            if (devices.size() == 1) { //wait one second and then assume there aren't any more devices named "orange"
                foundHandler.postDelayed(new Runnable() {
                    public void run() {
                        doneSearching();
                    }
                }, 1000);
            }
        }
    }
    private void doneSearching(){
        if (Build.VERSION.SDK_INT < 21) {
            mBluetoothAdapter.stopLeScan(mLeScanCallback);
        } else {
            mLEScanner.stopScan(mScanCallback);
        }
        if (devices.size() == 1){
            BluetoothDevice device = devices.get(0);
            connect(device);
        }else{
            //normally this displays a list and the user can choose which device. But this works just as well for now.
            BluetoothDevice device = devices.get(0);
            connect(device);
        }
    }
    //connect method
    public boolean connect(BluetoothDevice btdevice) {
        orangeDevice = btdevice;
        if (mBluetoothAdapter == null || btdevice == null || btdevice.getAddress() == null) {
            return false;
        }
        if (Build.VERSION.SDK_INT < 21) {
            mBluetoothAdapter.stopLeScan(mLeScanCallback);
        } else {
            mLEScanner.stopScan(mScanCallback);
        }
        devices = new ArrayList<>();
        mBluetoothGatt = orangeDevice.connectGatt(context, true, mGattCallback);
        return true;
    }

    public void disconnect() {
        if (mBluetoothAdapter == null || mBluetoothGatt == null) return;
        mBluetoothGatt.disconnect();
        mBluetoothGatt = null;
    }

    // Device scan callback.
    private BluetoothAdapter.LeScanCallback mLeScanCallback =
        new BluetoothAdapter.LeScanCallback() {
            @Override
            public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
                foundDevice(device);
            }
        };

    public void setUpCallBack(){
        mScanCallback = new ScanCallback() {
            @Override
            public void onScanResult(int callbackType, ScanResult result) {
                BluetoothDevice device = result.getDevice();
                foundDevice(device);
            }
            @Override
            public void onScanFailed(int errorCode) {
                Log.e("Chase", "Scan Failed Error Code: " + errorCode);
            }
        };
    }
}

更新

我正在玩一个星系平板电脑并且无法重现这个问题。所以我认为它取决于设备。 Galaxy S3上出现问题,我正在尝试将其他一些设备用于测试。

此外,我能够接触到一些新设备,似乎如果设备之前从未连接到手机(原始设备),那么该设备不会混淆并认为#&# 39;当它不连接时连接。因此,我们会在搜索时看到它,但它从未认为我正在连接它,直到我实际连接到它。在那之后,当我不是时,它认为我试图与之交谈的时间有一半。我希望这是有道理的。这支持了手机以某种方式缓存旧设备的理论。我尝试卸载应用程序并重新安装它以查看它是否与使用原始设备具有相同的效果,但似乎该应用程序与它无关。即使我重新安装了该应用,该设备仍然会在手机被引入后连接(当它不应该连接时)。

2 个答案:

答案 0 :(得分:0)

我会检查BLE设备本身。他们是否有可能拥有相同的系统ID?我相信它是0x180A的第一个特性。如果是这样 - 主机很难区分它们,并且可能发生这种双重连接。

答案 1 :(得分:0)

- 假设您有2台设备。所以findDevice()被调用了。现在设备arraylist包含1个设备。

- 之后你正在使用调用doneSearching()&amp;检查

if device.size()==1

返回true并调用connect()

-Inside connect你再次创建一个arraylist,即

devices = new ArrayList<>();

所以现在发生的事情是你的设备ArrayList&lt;&gt;()包含0个元素。

- 现在当再次找到第二个设备时,重复上述步骤,因为无论何时调用connect方法,列表的大小都会刷新为0 所以只需删除

 devices = new ArrayList<>();

在connect()方法中