Android 4.3:如何连接多个蓝牙低功耗设备?

时间:2015-07-05 17:30:20

标签: android bluetooth bluetooth-lowenergy connection simultaneous

任务:

我正在开发一个Android应用程序,需要同时连接到多个(相同的,可以通过其ID区分)BLE芯片设备,以便发送和接收更新。我在Google的官方网页上使用了这些教程:

http://developer.android.com/guide/topics/connectivity/bluetooth-le.html

这帮助我创建了一个DeviceScanActivity Java类,它允许我扫描并列出所有可用的BLE设备,与此类型的大多数应用程序目前的类似。我还发现最多可以同时将7个外部从站连接到同一个主设备。但是,实施这种沟通对我来说仍然不清楚。一个有用的链接是:

Android 4.3: How to connect to multiple Bluetooth Low Energy devices

但是,它没有为我提供足够的细节来理解这样的实现是如何工作的。

我一直在研究这个主题,但却无法找到任何示例实现。我知道这个问题已被问过很多次,但似乎没有任何在线可用的解决方案/演示可以让我更清楚。如果有人能指出我的资源/工作解决方案,我将非常感激,它将详细解释修改现有Java类以实现此功能所需的步骤。

DeviceScanActivity.java

import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

/**
 * Activity for scanning and displaying available Bluetooth LE devices.
 */
public class DeviceScanActivity extends ListActivity {
    private LeDeviceListAdapter mLeDeviceListAdapter;
    private BluetoothAdapter mBluetoothAdapter;
    private boolean mScanning;
    private Handler mHandler;

    private static final int REQUEST_ENABLE_BT = 1;
    // Stops scanning after 10 seconds.
    private static final long SCAN_PERIOD = 10000;

    private static final String CONFIG_FILE = "file";

    private int _countClick = 0;
    private boolean experimenterMode = false;
    private String pairedDeviceAddress = "";



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActionBar().setTitle(R.string.title_devices);
        mHandler = new Handler();

        // Use this check to determine whether BLE is supported on the device.  Then you can
        // selectively disable BLE-related features.
        if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
            Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
            finish();
        }

        // Initializes a Bluetooth adapter.  For API level 18 and above, get a reference to
        // BluetoothAdapter through BluetoothManager.
        final BluetoothManager bluetoothManager =
                (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        mBluetoothAdapter = bluetoothManager.getAdapter();

        // Checks if Bluetooth is supported on the device.
        if (mBluetoothAdapter == null) {
            Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show();
            finish();
            return;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        if (!mScanning) {
            menu.findItem(R.id.menu_stop).setVisible(false);
            menu.findItem(R.id.menu_scan).setVisible(true);
            menu.findItem(R.id.menu_refresh).setActionView(null);
        } else {
            menu.findItem(R.id.menu_stop).setVisible(true);
            menu.findItem(R.id.menu_scan).setVisible(false);
            menu.findItem(R.id.menu_refresh).setActionView(
                    R.layout.actionbar_indeterminate_progress);
        }
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_scan:
                mLeDeviceListAdapter.clear();
                scanLeDevice(true);
                break;
            case R.id.menu_stop:
                scanLeDevice(false);
                break;
            case android.R.id.home:
                //onBackPressed();
                _countClick++;
                if(_countClick>8)
                {
                    toggleExperimenterMode();
                }
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        super.onResume();

        // Ensures Bluetooth is enabled on the device.  If Bluetooth is not currently enabled,
        // fire an intent to display a dialog asking the user to grant permission to enable it.
        if (!mBluetoothAdapter.isEnabled()) {
            if (!mBluetoothAdapter.isEnabled()) {
                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
            }
        }

        // Restore preferences
        SharedPreferences settings = getSharedPreferences(CONFIG_FILE, 0);
        experimenterMode = settings.getBoolean("experimenterMode", false);
        pairedDeviceAddress = settings.getString("pairedDeviceAddress", "");

        // Initializes list view adapter.
        mLeDeviceListAdapter = new LeDeviceListAdapter();
        setListAdapter(mLeDeviceListAdapter);
        scanLeDevice(true);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // User chose not to enable Bluetooth.
        if (requestCode == REQUEST_ENABLE_BT && resultCode == Activity.RESULT_CANCELED) {
            finish();
            return;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onPause() {
        super.onPause();
        scanLeDevice(false);
        mLeDeviceListAdapter.clear();
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        final BluetoothDevice device = mLeDeviceListAdapter.getDevice(position);
        if (device == null) return;
        connectToDevice(device);
    }



    private void connectToDevice(BluetoothDevice device){
        final Intent intent = new Intent(this, DeviceControlActivity.class);
        intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_NAME, device.getName());
        intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_ADDRESS, device.getAddress());
        if (mScanning) {
            mBluetoothAdapter.stopLeScan(mLeScanCallback);
            mScanning = false;
        }

        SharedPreferences settings = getSharedPreferences(CONFIG_FILE, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString("pairedDeviceAddress", device.getAddress());
        editor.commit();

        startActivity(intent);
    }

    //---------------------------------------------------

    private void scanLeDevice(final boolean enable) {
        ProgressDialog progress;
        if (enable) {
            // Stops scanning after a pre-defined scan period.
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mScanning = false;
                    mBluetoothAdapter.stopLeScan(mLeScanCallback);
                    invalidateOptionsMenu();
                }
            }, SCAN_PERIOD);

            mScanning = true;
            mBluetoothAdapter.startLeScan(mLeScanCallback);
        } else {
            mScanning = false;
            mBluetoothAdapter.stopLeScan(mLeScanCallback);

        }
        invalidateOptionsMenu();
    }

    private void toggleExperimenterMode(){
        _countClick=0;
        experimenterMode = !experimenterMode;
        SharedPreferences settings = getSharedPreferences(CONFIG_FILE, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean("experimenterMode", experimenterMode);
        editor.commit();
        String t = experimenterMode ? "You are now in Experimenter mode":"You are now in User mode";
        Toast.makeText(this, t, Toast.LENGTH_SHORT).show();
    }

    // Adapter for holding devices found through scanning.
    private class LeDeviceListAdapter extends BaseAdapter {
        private ArrayList<BluetoothDevice> mLeDevices;
        private LayoutInflater mInflator;

        public LeDeviceListAdapter() {
            super();
            mLeDevices = new ArrayList<BluetoothDevice>();
            mInflator = DeviceScanActivity.this.getLayoutInflater();
        }

        public void addDevice(BluetoothDevice device) {
            if(!mLeDevices.contains(device)) {
                mLeDevices.add(device);
            }
            if (!experimenterMode && ! pairedDeviceAddress.isEmpty()) {
                if (device.getAddress().equals(pairedDeviceAddress)) {
                    connectToDevice(device);
                }
            }
        }

        public BluetoothDevice getDevice(int position) {
            return mLeDevices.get(position);
        }

        public void clear() {
            mLeDevices.clear();
        }

        @Override
        public int getCount() {
            return mLeDevices.size();
        }

        @Override
        public Object getItem(int i) {
            return mLeDevices.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder viewHolder;
            // General ListView optimization code.
            if (view == null) {
                view = mInflator.inflate(R.layout.listitem_device, null);
                viewHolder = new ViewHolder();
                viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address);
                viewHolder.deviceName = (TextView) view.findViewById(R.id.device_name);
                view.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) view.getTag();
            }

            BluetoothDevice device = mLeDevices.get(i);
            final String deviceName = device.getName();
            if (deviceName != null && deviceName.length() > 0)
                viewHolder.deviceName.setText(deviceName);
            else
                viewHolder.deviceName.setText(R.string.unknown_device);
            viewHolder.deviceAddress.setText(device.getAddress());

            return view;
        }
    }

    // Device scan callback.
    private BluetoothAdapter.LeScanCallback mLeScanCallback =
            new BluetoothAdapter.LeScanCallback() {

        @Override
        public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mLeDeviceListAdapter.addDevice(device);
                    mLeDeviceListAdapter.notifyDataSetChanged();
                }
            });
        }
    };

    static class ViewHolder {
        TextView deviceName;
        TextView deviceAddress;
    }
}

最新信息:

所有这些页面都包含一些指示,但它们没有提供有关如何实现多个连接的足够详细信息。

UI目标表示:

Diagram

我的想法是,我的5.1.1 Android Nexus 7设备将能够连接至多5个BLE芯片从设备并发送/接收更新。

2 个答案:

答案 0 :(得分:1)

我在Android中使用两个设备完成了此操作。 首先根据我的经验,分割代码要好得多。就我而言,我使用了一项服务来进行扫描。使用广播接收器,我已将找到的设备Object发送到主要活动。在这个活动中,只有ui的东西,我的Servicenavigation就完成了。当我的主要活动重新启动蓝牙设备对象时,我将此对象传输到单独的服务,其中只有蓝牙通信完成,而另一个广播接收器我将收到的数据发送回我的主要活动以显示值。 如果要连接多个设备,可以像使用一个设备一样使用此模式。在LeScan Callback中的扫描仪服务中,您可以检查找到的Macadress设备。我已经疯了一个字符串数组,其中包含我要连接的所有Mac地址。当我找到一个我从阵列中删除它时,当你找到所需的所有设备时,你可以停止整个服务。 此外: 我的经验是你不应该绑定你的服务。最好手动启动和停止服务。当我想要破坏我的绑定服务时,连接经常保持活跃状态​​,当再次连接时,我的蓝牙表现得很奇怪,或者我找不到我的Ble服务器设备,因为它内部仍然受限制。

答案 1 :(得分:1)

我的应用程序也遇到了同样的问题,有关android上BLE的文档很少,但是经过大量研究,我创建了一个运行良好的蓝牙库来连接多个设备:https://github.com/niedev/BluetoothCommunicator

p.s。在我对该库的测试中,稳定连接的设备的最大数量为4(即使重新连接)

编辑:主持人继续取消我的回答,因为我使用了指向代码的链接,而不是直接在此处插入代码,但我不能这样做,因为该代码将近4600行代码分为10类(以及字符数)答案有限)