Android項目實戰(三十四):藍牙4.0 BLE 多設備鏈接

  最近項目有個需求,手機設備鏈接多個藍牙4.0 設備 並獲取這些設備的數據。java

  查詢了不少資料終於實現,現進行總結。android

 

---------------------------------------------------------------------------------------------------------------------------------------------------------------git

    從零開始實現一個鏈接多個藍牙4.0 設備並獲取數據的 Demogithub

  注:若是不想看實現過程的,直接看最下面的demo源碼便可,或每一步後相關操做步驟的完整代碼。app

  

  1、Demo需求框架

    一、搜索設備 , 選擇多個要鏈接的設備。ide

    二、開始鏈接,顯示數據。佈局

  

  2、項目知識儲備post

    項目中須要用到的三方:gradle

    一、RecyclerView 

       列表,用於顯示掃描獲得的全部藍牙設備

    二、BaseRecyclerViewAdapterHelper

       Recyclerview 幫助框架,快速實現列表操做 

    三、eventbus

      用於消息傳遞,獲取到藍牙傳送的數據以後,刷新界面顯示數據時使用

    四、bluetooth-manager

      藍牙4.0框架

    五、permissionsdispatcher

     權限管理,適配6.0+設備

     

    添加依賴 gradle.bulld文件

    compile 'com.android.support:appcompat-v7:25.3.1'    compile 'com.blakequ.androidblemanager:bluetooth-manager-lib:2.1.5'
    compile 'com.github.hotchemi:permissionsdispatcher:2.1.3'
    compile 'de.greenrobot:eventbus:2.4.0'
    compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.18'
    compile 'com.android.support:design:25.3.1'

 

  3、項目實現,佈局文件

    一、demo中一共用到兩個activity 對應兩個佈局文件

 

     先看掃描設備界面

     包含:

      一、一個列表,顯示 全部掃描到的設備的MAC地址,點擊狀態在 ''已選擇' or '‘未選擇’ 之間改變,代表當前設備有沒有加入到須要鏈接的設備集合中

     二、掃描按鈕

     三、結束掃描按鈕

     四、完成選擇按鈕,將選擇的設備MAC地址傳回 

     

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_select_device"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.maiji.magkareble40.SelectDeviceActivity">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        >

    </android.support.v7.widget.RecyclerView>


    <Button
        android:id="@+id/btnScan"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="開始掃描"
        />

    <Button
        android:id="@+id/btnStopScan"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="中止掃描"
        />
    <Button
        android:id="@+id/btnOk"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="完成選擇設備"
        />
</LinearLayout>

     

    鏈接界面。

    包含:

    一、選擇須要鏈接的傳感器設備 按鈕

    二、開始鏈接 按鈕

    三、數據展現

    

    

 

    佈局文件代碼:

  

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.maiji.magkareble40.XBleActivity">


    <Button
        android:id="@+id/btnSelectDevice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="選擇須要鏈接的傳感器設備"
        />

    <Button
        android:id="@+id/btnStartConnect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="開始鏈接"
        />

    <TextView
        android:id="@+id/txtContentMac"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text=""/>

</LinearLayout>

 

    4、Activity實現

    一、掃描 設備 選擇設備Activity

    (1)、變量聲明

   private Button btnScan;        //開始掃描按鈕
    private Button btnStopScan;   //中止掃描按鈕
    private Button btnOk;   //選擇好了須要鏈接的mac設備

    BluetoothScanManager scanManager ;  // 設備掃描管理器


    /* 列表相關 */
    private RecyclerView recyclerView ; //列表
    private ScanDeviceAdapter adapter;  //設備掃描適配器
    private ArrayList<String> deviceMacs ; // 數據源 : 全部掃描到的設備mac地址

    private ArrayList<String> selectDeviceMacs; // 選擇的須要鏈接的設備的mac集合

 

    關鍵代碼:

    (1)、藍牙掃描的初始化設置

/**
     * 初始化藍牙相關配置
     */
    private void initBle() {
        scanManager = BleManager.getScanManager(this);

        scanManager.setScanOverListener(new ScanOverListener() {
            @Override
            public void onScanOver() {
            }
        });


        scanManager.setScanCallbackCompat(new ScanCallbackCompat() {
            @Override
            public void onBatchScanResults(List<ScanResultCompat> results) {
                super.onBatchScanResults(results);
            }

            @Override
            public void onScanFailed(final int errorCode) {
                super.onScanFailed(errorCode);

            }

            @Override
            public void onScanResult(int callbackType, ScanResultCompat result) {
                super.onScanResult(callbackType, result);
                //scan result
                // 只有當前列表中沒有該mac地址的時候 添加
                if (!deviceMacs.contains(result.getDevice().getAddress())) {
                    deviceMacs.add(result.getDevice().getAddress());
                    adapter.notifyDataSetChanged();
                }
            }
        });

    }
藍牙掃描設置初始化

    (2)、開始掃描按鈕 操做

//                scanManager.startCycleScan(); //不會當即開始,可能會延時
                scanManager.startScanNow(); //當即開始掃描

    (3)、中止掃描按鈕 操做

 // 若是正在掃描中 中止掃描
                if (scanManager.isScanning()) {
                    scanManager.stopCycleScan();
                }

    (4)、RecyclerView初始化 ,點擊事件操做

    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

        // 列表相關初始化
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new ScanDeviceAdapter(deviceMacs);

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (!selectDeviceMacs.contains(deviceMacs.get(position))){
                    //若是改item的mac不在已選中的mac集合中 說明沒有選中,添加進已選中mac集合中,狀態改成"已選擇"
                    selectDeviceMacs.add(deviceMacs.get(position));
                    ((TextView)view.findViewById(R.id.txtState)).setText("已選擇");
                }else {
                    selectDeviceMacs.remove(deviceMacs.get(position));
                    ((TextView)view.findViewById(R.id.txtState)).setText("未選擇");
                }
            }
        });

        recyclerView.setAdapter(adapter);

  

  activity所有代碼:

package com.maiji.magkareble40;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.blakequ.bluetooth_manager_lib.BleManager;
import com.blakequ.bluetooth_manager_lib.BleParamsOptions;
import com.blakequ.bluetooth_manager_lib.connect.ConnectConfig;
import com.blakequ.bluetooth_manager_lib.scan.BluetoothScanManager;
import com.blakequ.bluetooth_manager_lib.scan.ScanOverListener;
import com.blakequ.bluetooth_manager_lib.scan.bluetoothcompat.ScanCallbackCompat;
import com.blakequ.bluetooth_manager_lib.scan.bluetoothcompat.ScanFilterCompat;
import com.blakequ.bluetooth_manager_lib.scan.bluetoothcompat.ScanResultCompat;
import com.chad.library.adapter.base.BaseQuickAdapter;

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

/**
* @author xqx
* @email djlxqx@163.com
* blog:http://www.cnblogs.com/xqxacm/
* createAt 2017/9/6
* description:  掃描藍牙設備  選擇須要鏈接的傳感器
*/

public class SelectDeviceActivity extends Activity implements View.OnClickListener {

    private Button btnScan;        //開始掃描按鈕
    private Button btnStopScan;   //中止掃描按鈕
    private Button btnOk;   //選擇好了須要鏈接的mac設備

    BluetoothScanManager scanManager ;


    /* 列表相關 */
    private RecyclerView recyclerView ; //列表
    private ScanDeviceAdapter adapter;
    private ArrayList<String> deviceMacs ; // 數據源 : 全部掃描到的設備mac地址

    private ArrayList<String> selectDeviceMacs; // 選擇的須要鏈接的設備的mac集合

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_device);

        deviceMacs = new ArrayList<>();
        selectDeviceMacs = new ArrayList<>();
        initView();
        initEvent();
        initBle();


    }

    /**
     * 初始化藍牙相關配置
     */
    private void initBle() {
        scanManager = BleManager.getScanManager(this);

        scanManager.setScanOverListener(new ScanOverListener() {
            @Override
            public void onScanOver() {
            }
        });


        scanManager.setScanCallbackCompat(new ScanCallbackCompat() {
            @Override
            public void onBatchScanResults(List<ScanResultCompat> results) {
                super.onBatchScanResults(results);
            }

            @Override
            public void onScanFailed(final int errorCode) {
                super.onScanFailed(errorCode);

            }

            @Override
            public void onScanResult(int callbackType, ScanResultCompat result) {
                super.onScanResult(callbackType, result);
                //scan result
                // 只有當前列表中沒有該mac地址的時候 添加
                if (!deviceMacs.contains(result.getDevice().getAddress())) {
                    deviceMacs.add(result.getDevice().getAddress());
                    adapter.notifyDataSetChanged();
                }
            }
        });

    }

    private void initEvent() {
        btnScan.setOnClickListener(this);
        btnStopScan.setOnClickListener(this);
        btnOk.setOnClickListener(this);
    }

    private void initView() {
        btnScan = (Button) findViewById(R.id.btnScan);
        btnStopScan = (Button) findViewById(R.id.btnStopScan);
        btnOk = (Button) findViewById(R.id.btnOk);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

        // 列表相關初始化
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new ScanDeviceAdapter(deviceMacs);

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (!selectDeviceMacs.contains(deviceMacs.get(position))){
                    //若是改item的mac不在已選中的mac集合中 說明沒有選中,添加進已選中mac集合中,狀態改成"已選擇"
                    selectDeviceMacs.add(deviceMacs.get(position));
                    ((TextView)view.findViewById(R.id.txtState)).setText("已選擇");
                }else {
                    selectDeviceMacs.remove(deviceMacs.get(position));
                    ((TextView)view.findViewById(R.id.txtState)).setText("未選擇");
                }
            }
        });

        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btnScan:
                //開始 掃描
//                scanManager.startCycleScan(); //不會當即開始,可能會延時
                scanManager.startScanNow(); //當即開始掃描
                break;

            case R.id.btnStopScan:
                // 若是正在掃描中 中止掃描
                if (scanManager.isScanning()) {
                    scanManager.stopCycleScan();
                }
                break;
            case R.id.btnOk:
                Intent intent = new Intent();
                intent.putExtra("data",selectDeviceMacs);                // 設置結果,並進行傳送
                this.setResult(1, intent);
                this.finish();
                break;

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 若是正在掃描中 中止掃描
        if (scanManager.isScanning()) {
            scanManager.stopCycleScan();
        }
    }
}
SelectDeviceActivity.class

  適配器相關代碼:

package com.maiji.magkareble40;

import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;

/**
* @author xqx
* @email djlxqx@163.com
* blog:http://www.cnblogs.com/xqxacm/
* createAt 2017/9/6
* description:  掃描獲得的藍牙設備列表適配器
*/

public class ScanDeviceAdapter extends BaseQuickAdapter<String , BaseViewHolder> {

    public ScanDeviceAdapter(ArrayList<String> datas) {
        super(R.layout.item_device, datas);
    }

    @Override
    protected void convert(BaseViewHolder helper, String item) {
        helper.setText(R.id.txtMac,item);

    }
}
ScanDeviceAdapter.class

  適配器佈局代碼:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:layout_marginTop="16dp"
    android:layout_marginBottom="16dp"
    >

    <TextView
        android:id="@+id/txtMac"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:layout_centerVertical="true"
        />

    <TextView
        android:id="@+id/txtState"
        android:text="未選擇"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#fff"
        android:layout_alignParentBottom="true"
        ></View>
</RelativeLayout>
item_device.xml

 

    二、鏈接多設備,獲取數據並展現Activity 

    (1)、變量聲明

    private Button btnSelectDevice ;  //選擇須要綁定的設備
    private Button btnStartConnect ;  //開始鏈接按鈕

    private TextView txtContentMac ; //獲取到的數據解析結果顯示

    private final int REQUEST_CODE_PERMISSION = 1; // 權限請求碼  用於回調

    MultiConnectManager multiConnectManager ;  //多設備鏈接
    private BluetoothAdapter bluetoothAdapter;   //藍牙適配器


    private ArrayList<String> connectDeviceMacList ; //須要鏈接的mac設備集合
    ArrayList<BluetoothGatt> gattArrayList; //設備gatt集合

  

    二、關鍵代碼

    一、權限適配

    注意:不止藍牙權限,位置權限也須要打開

/**
     * @author xqx
     * @email djlxqx@163.com
     * blog:http://www.cnblogs.com/xqxacm/
     * createAt 2017/8/30
     * description:  權限申請相關,適配6.0+機型 ,藍牙,文件,位置 權限
     */

    private String[] allPermissionList = {Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN,
            Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE
    };


    /**
     * 遍歷出須要獲取的權限
     */
    private void requestWritePermission() {
        ArrayList<String> permissionList = new ArrayList<>();
        // 將須要獲取的權限加入到集合中  ,根據集合數量判斷 需不須要添加
        for (int i = 0; i < allPermissionList.length; i++) {
            if (PackageManager.PERMISSION_DENIED == ContextCompat.checkSelfPermission(this, allPermissionList[i])){
                permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
            }
        }

        String permissionArray[] = new String[permissionList.size()];
        for (int i = 0; i < permissionList.size(); i++) {
            permissionArray[i] = permissionList.get(i);
        }
        if (permissionList.size() > 0)
            ActivityCompat.requestPermissions(this, permissionArray, REQUEST_CODE_PERMISSION);
    }


    /**
     * 權限申請的回調
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == REQUEST_CODE_PERMISSION){
            if (permissions[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    &&grantResults[0] == PackageManager.PERMISSION_GRANTED){
                //用戶贊成使用write

            }else{
                //用戶不一樣意,自行處理便可
                Toast.makeText(XBleActivity.this,"您取消了權限申請,可能會影響軟件的使用,若有問題請退出重試",Toast.LENGTH_SHORT).show();
            }
        }
    }
權限適配

    二、藍牙開啓、鏈接等 初始化設置

/**
     * 對藍牙的初始化操做
     */
    private void initConfig() {
        multiConnectManager = BleManager.getMultiConnectManager(this);
        // 獲取藍牙適配器

        try {
            // 獲取藍牙適配器
            bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            //
            if (bluetoothAdapter == null) {
                Toast.makeText(this, "藍牙不可用", Toast.LENGTH_LONG).show();
                return;
            }

            // 藍牙沒打開的時候打開藍牙
            if (!bluetoothAdapter.isEnabled())
                bluetoothAdapter.enable();
        }catch (Exception err){};
        BleManager.setBleParamsOptions(new BleParamsOptions.Builder()
                .setBackgroundBetweenScanPeriod(5 * 60 * 1000)
                .setBackgroundScanPeriod(10000)
                .setForegroundBetweenScanPeriod(2000)
                .setForegroundScanPeriod(10000)
                .setDebugMode(BuildConfig.DEBUG)
                .setMaxConnectDeviceNum(7)            //最大能夠鏈接的藍牙設備個數
                .setReconnectBaseSpaceTime(1000)
                .setReconnectMaxTimes(Integer.MAX_VALUE)
                .setReconnectStrategy(ConnectConfig.RECONNECT_LINE_EXPONENT)
                .setReconnectedLineToExponentTimes(5)
                .setConnectTimeOutTimes(20000)
                .build());
    }
initBle

    三、開始鏈接操做

 /**
     * 鏈接須要鏈接的傳感器
     * @param
     */
    private void connentBluetooth(){

        String[] objects = connectDeviceMacList.toArray(new String[connectDeviceMacList.size()]);
        multiConnectManager.addDeviceToQueue(objects);
        multiConnectManager.addConnectStateListener(new ConnectStateListener() {
            @Override
            public void onConnectStateChanged(String address, ConnectState state) {
                switch (state){
                    case CONNECTING:
                        Log.i("connectStateX","設備:"+address+"鏈接狀態:"+"正在鏈接");
                        break;
                    case CONNECTED:
                        Log.i("connectStateX","設備:"+address+"鏈接狀態:"+"成功");
                        break;
                    case NORMAL:
                        Log.i("connectStateX","設備:"+address+"鏈接狀態:"+"失敗");
                        break;
                }
            }
        });

        /**
         * 數據回調
         */

        multiConnectManager.setBluetoothGattCallback(new BluetoothGattCallback() {
            @Override
            public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
                super.onCharacteristicChanged(gatt, characteristic);
                dealCallDatas(gatt , characteristic);
            }
        });

        multiConnectManager.setServiceUUID("0000ffe5-0000-1000-8000-00805f9a34fb");
        multiConnectManager.addBluetoothSubscribeData(
                new BluetoothSubScribeData.Builder().setCharacteristicNotify(UUID.fromString("0000ffe4-0000-1000-8000-00805f9a34fb")).build());

        //還有讀寫descriptor
        //start descriptor(注意,在使用時當回調onServicesDiscovered成功時會自動調用該方法,因此只須要在鏈接以前完成1,3步便可)
        for (int i = 0; i < gattArrayList.size(); i++) {
            multiConnectManager.startSubscribe(gattArrayList.get(i));
        }

        multiConnectManager.startConnect();

    }

    /**
     * 處理回調的數據
     * @param gatt
     * @param characteristic
     */

    float[][] floats = new float[7][30];

    private void dealCallDatas(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
        int position = connectDeviceMacList.indexOf(gatt.getDevice().getAddress());
        //第一個傳感器數據
        byte[] value = characteristic.getValue();
        if (value[0] != 0x55) {
            //開頭不是0x55的數據刪除
            return;
        }
        switch (value[1]) {
            case 0x61:
                //加速度數據
                floats[position][3] = ((((short) value[3]) << 8) | ((short) value[2] & 0xff)) / 32768.0f * 16;   //x軸
                floats[position][4] = ((((short) value[5]) << 8) | ((short) value[4] & 0xff)) / 32768.0f * 16;   //y軸
                floats[position][5] = ((((short) value[7]) << 8) | ((short) value[6] & 0xff)) / 32768.0f * 16;   //z軸
                //角速度數據
                floats[position][6] = ((((short) value[9]) << 8) | ((short) value[8] & 0xff)) / 32768.0f * 2000;  //x軸
                floats[position][7] = ((((short) value[11]) << 8) | ((short) value[10] & 0xff)) / 32768.0f * 2000;  //x軸
                floats[position][8] = ((((short) value[13]) << 8) | ((short) value[12] & 0xff)) / 32768.0f * 2000;  //x軸
                break;
            case 0x62:
                //四元素
                floats[position][13] = ((((short) value[3]) << 8) | ((short) value[2] & 0xff)) / 32768.0f; // q1
                floats[position][14] = ((((short) value[5]) << 8) | ((short) value[4] & 0xff)) / 32768.0f; // q2
                floats[position][15] = ((((short) value[7]) << 8) | ((short) value[6] & 0xff)) / 32768.0f; // q3
                floats[position][16] = ((((short) value[9]) << 8) | ((short) value[8] & 0xff)) / 32768.0f; // q4
                //電池電壓
                floats[position][21] = (float) 1.2 * 4 * (((value[11] << 8) | value[10]) + 1) / 1024;
                //充電狀態
                floats[position][22] = value[12];
                //低電壓報警
                floats[position][23] = value[14];
                break;
        }
        EventBus.getDefault().post(new RefreshDatas()); // 發送消息,更新UI 顯示數據
    }
connectBle

 

activity所有代碼:

package com.maiji.magkareble40;

import android.Manifest;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.blakequ.bluetooth_manager_lib.BleManager;
import com.blakequ.bluetooth_manager_lib.BleParamsOptions;
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.blakequ.bluetooth_manager_lib.connect.ConnectStateListener;
import com.blakequ.bluetooth_manager_lib.connect.multiple.MultiConnectManager;

import java.util.ArrayList;
import java.util.UUID;

import de.greenrobot.event.EventBus;

/**
* @author xqx
* @email djlxqx@163.com
* blog:http://www.cnblogs.com/xqxacm/
* createAt 2017/9/6
* description:  ble 4.0 多設備鏈接
*/

public class XBleActivity extends Activity implements View.OnClickListener {

    private Button btnSelectDevice ;  //選擇須要綁定的設備
    private Button btnStartConnect ;  //開始鏈接按鈕

    private TextView txtContentMac ; //獲取到的數據解析結果顯示

    private final int REQUEST_CODE_PERMISSION = 1; // 權限請求碼  用於回調

    MultiConnectManager multiConnectManager ;  //多設備鏈接
    private BluetoothAdapter bluetoothAdapter;   //藍牙適配器


    private ArrayList<String> connectDeviceMacList ; //須要鏈接的mac設備集合
    ArrayList<BluetoothGatt> gattArrayList; //設備gatt集合


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xble);

        initVariables();
        initView();
        initEvent();
        requestWritePermission();
        initConfig();  // 藍牙初始設置
        EventBus.getDefault().register(this);
    }

    private void initVariables() {
        connectDeviceMacList = new ArrayList<>();
        gattArrayList = new ArrayList<>();

    }

    private void initEvent() {
        btnSelectDevice.setOnClickListener(this);
        btnStartConnect.setOnClickListener(this);
    }

    private void initView() {
        btnSelectDevice = (Button) findViewById(R.id.btnSelectDevice);
        btnStartConnect = (Button) findViewById(R.id.btnStartConnect);
        txtContentMac = (TextView) findViewById(R.id.txtContentMac);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btnSelectDevice:
                // 掃描並選擇須要鏈接的設備
                Intent intent = new Intent();
                intent.setClass(this,SelectDeviceActivity.class);
                startActivityForResult(intent,1);
                break;
            case R.id.btnStartConnect:
                connentBluetooth();
                break;
        }
    }


    /**
     * 鏈接須要鏈接的傳感器
     * @param
     */
    private void connentBluetooth(){

        String[] objects = connectDeviceMacList.toArray(new String[connectDeviceMacList.size()]);
        multiConnectManager.addDeviceToQueue(objects);
        multiConnectManager.addConnectStateListener(new ConnectStateListener() {
            @Override
            public void onConnectStateChanged(String address, ConnectState state) {
                switch (state){
                    case CONNECTING:
                        Log.i("connectStateX","設備:"+address+"鏈接狀態:"+"正在鏈接");
                        break;
                    case CONNECTED:
                        Log.i("connectStateX","設備:"+address+"鏈接狀態:"+"成功");
                        break;
                    case NORMAL:
                        Log.i("connectStateX","設備:"+address+"鏈接狀態:"+"失敗");
                        break;
                }
            }
        });

        /**
         * 數據回調
         */

        multiConnectManager.setBluetoothGattCallback(new BluetoothGattCallback() {
            @Override
            public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
                super.onCharacteristicChanged(gatt, characteristic);
                dealCallDatas(gatt , characteristic);
            }
        });

        multiConnectManager.setServiceUUID("0000ffe5-0000-1000-8000-00805f9a34fb");
        multiConnectManager.addBluetoothSubscribeData(
                new BluetoothSubScribeData.Builder().setCharacteristicNotify(UUID.fromString("0000ffe4-0000-1000-8000-00805f9a34fb")).build());

        //還有讀寫descriptor
        //start descriptor(注意,在使用時當回調onServicesDiscovered成功時會自動調用該方法,因此只須要在鏈接以前完成1,3步便可)
        for (int i = 0; i < gattArrayList.size(); i++) {
            multiConnectManager.startSubscribe(gattArrayList.get(i));
        }

        multiConnectManager.startConnect();

    }

    /**
     * 處理回調的數據
     * @param gatt
     * @param characteristic
     */

    float[][] floats = new float[7][30];

    private void dealCallDatas(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
        int position = connectDeviceMacList.indexOf(gatt.getDevice().getAddress());
        //第一個傳感器數據
        byte[] value = characteristic.getValue();
        if (value[0] != 0x55) {
            //開頭不是0x55的數據刪除
            return;
        }
        switch (value[1]) {
            case 0x61:
                //加速度數據
                floats[position][3] = ((((short) value[3]) << 8) | ((short) value[2] & 0xff)) / 32768.0f * 16;   //x軸
                floats[position][4] = ((((short) value[5]) << 8) | ((short) value[4] & 0xff)) / 32768.0f * 16;   //y軸
                floats[position][5] = ((((short) value[7]) << 8) | ((short) value[6] & 0xff)) / 32768.0f * 16;   //z軸
                //角速度數據
                floats[position][6] = ((((short) value[9]) << 8) | ((short) value[8] & 0xff)) / 32768.0f * 2000;  //x軸
                floats[position][7] = ((((short) value[11]) << 8) | ((short) value[10] & 0xff)) / 32768.0f * 2000;  //x軸
                floats[position][8] = ((((short) value[13]) << 8) | ((short) value[12] & 0xff)) / 32768.0f * 2000;  //x軸
                break;
            case 0x62:
                //四元素
                floats[position][13] = ((((short) value[3]) << 8) | ((short) value[2] & 0xff)) / 32768.0f; // q1
                floats[position][14] = ((((short) value[5]) << 8) | ((short) value[4] & 0xff)) / 32768.0f; // q2
                floats[position][15] = ((((short) value[7]) << 8) | ((short) value[6] & 0xff)) / 32768.0f; // q3
                floats[position][16] = ((((short) value[9]) << 8) | ((short) value[8] & 0xff)) / 32768.0f; // q4
                //電池電壓
                floats[position][21] = (float) 1.2 * 4 * (((value[11] << 8) | value[10]) + 1) / 1024;
                //充電狀態
                floats[position][22] = value[12];
                //低電壓報警
                floats[position][23] = value[14];
                break;
        }
        EventBus.getDefault().post(new RefreshDatas()); // 發送消息,更新UI 顯示數據
    }

    /**
     * 對藍牙的初始化操做
     */
    private void initConfig() {
        multiConnectManager = BleManager.getMultiConnectManager(this);
        // 獲取藍牙適配器

        try {
            // 獲取藍牙適配器
            bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            //
            if (bluetoothAdapter == null) {
                Toast.makeText(this, "藍牙不可用", Toast.LENGTH_LONG).show();
                return;
            }

            // 藍牙沒打開的時候打開藍牙
            if (!bluetoothAdapter.isEnabled())
                bluetoothAdapter.enable();
        }catch (Exception err){};
        BleManager.setBleParamsOptions(new BleParamsOptions.Builder()
                .setBackgroundBetweenScanPeriod(5 * 60 * 1000)
                .setBackgroundScanPeriod(10000)
                .setForegroundBetweenScanPeriod(2000)
                .setForegroundScanPeriod(10000)
                .setDebugMode(BuildConfig.DEBUG)
                .setMaxConnectDeviceNum(7)            //最大能夠鏈接的藍牙設備個數
                .setReconnectBaseSpaceTime(1000)
                .setReconnectMaxTimes(Integer.MAX_VALUE)
                .setReconnectStrategy(ConnectConfig.RECONNECT_LINE_EXPONENT)
                .setReconnectedLineToExponentTimes(5)
                .setConnectTimeOutTimes(20000)
                .build());
    }

    /**
     * @author xqx
     * @email djlxqx@163.com
     * blog:http://www.cnblogs.com/xqxacm/
     * createAt 2017/8/30
     * description:  權限申請相關,適配6.0+機型 ,藍牙,文件,位置 權限
     */

    private String[] allPermissionList = {Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN,
            Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE
    };


    /**
     * 遍歷出須要獲取的權限
     */
    private void requestWritePermission() {
        ArrayList<String> permissionList = new ArrayList<>();
        // 將須要獲取的權限加入到集合中  ,根據集合數量判斷 需不須要添加
        for (int i = 0; i < allPermissionList.length; i++) {
            if (PackageManager.PERMISSION_DENIED == ContextCompat.checkSelfPermission(this, allPermissionList[i])){
                permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
            }
        }

        String permissionArray[] = new String[permissionList.size()];
        for (int i = 0; i < permissionList.size(); i++) {
            permissionArray[i] = permissionList.get(i);
        }
        if (permissionList.size() > 0)
            ActivityCompat.requestPermissions(this, permissionArray, REQUEST_CODE_PERMISSION);
    }


    /**
     * 權限申請的回調
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == REQUEST_CODE_PERMISSION){
            if (permissions[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    &&grantResults[0] == PackageManager.PERMISSION_GRANTED){
                //用戶贊成使用write

            }else{
                //用戶不一樣意,自行處理便可
                Toast.makeText(XBleActivity.this,"您取消了權限申請,可能會影響軟件的使用,若有問題請退出重試",Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data!=null){
            switch (requestCode){
                case 1:
                    connectDeviceMacList = data.getStringArrayListExtra("data");
                    Log.i("xqxinfo","須要鏈接的mac"+connectDeviceMacList.toString());
                    //獲取設備gatt對象
                    for (int i = 0; i < connectDeviceMacList.size(); i++) {
                        BluetoothGatt gatt = bluetoothAdapter.getRemoteDevice(connectDeviceMacList.get(i)).connectGatt(this, false, new BluetoothGattCallback() {
                        });
                        gattArrayList.add(gatt);
                        Log.i("xqxinfo","添加了"+connectDeviceMacList.get(i));
                    }
                    break;
            }
        }
    }

    public void onEventMainThread(RefreshDatas event) {
        txtContentMac.setText("");
        for (int i = 0; i < connectDeviceMacList.size(); i++) {
            txtContentMac.append("Mac:"+connectDeviceMacList.get(i)+"\n");
            txtContentMac.append("加速度:"+floats[i][3]+"-"+floats[i][4]+"-"+floats[i][5]+"\n");
            txtContentMac.append("角速度:"+floats[i][6]+"-"+floats[i][7]+"-"+floats[i][8]+"\n");
            txtContentMac.append("四元素:"+floats[i][13]+"-"+floats[i][14]+"-"+floats[i][15]+"-"+floats[i][16]+"\n");
            txtContentMac.append("電池電壓:"+floats[i][21]+"\n");
            txtContentMac.append("充電狀態:"+floats[i][22]+"\n");
            txtContentMac.append("低電壓報警:"+floats[i][23]+"\n\n");
        }
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
XBleActivity.class

 

----------------------------------------------------------------------------------------------------------------------------------------------------------

 

項目地址:

https://github.com/BestCoderXQX/MagkareBle4.0

 

項目使用說明:

一、點擊按鈕:'選擇須要鏈接的傳感器設備'、跳轉新界面
二、點擊'開始掃描'按鈕,會出現不少設備的mac地址 ,以列表的新式展示
三、對列表item操做,更改狀態'已選擇'or'未選擇'
四、點擊按鈕'完成選擇設備'按鈕,將列表中狀態爲'已選擇'的mac集合傳回上個界面

五、點擊'開始鏈接'按鈕。鏈接開始,顯示鏈接設備的數據。(注意,這裏是按個人傳感器來的。實際須要換成你所用到的設備的 數據 轉換公式!)

 

框架使用說明:

多設備鏈接切換設備的使用

1、 關閉當前全部鏈接的設備
multiConnectManager.closeAll();

2、  講設備清出隊列

        // 當前註冊隊列中的設備
        List<String> allDevice = ultiConnectManager.getAllDevice();
        Log.i("connentBluetoothX", "全部的的傳感器" +allDevice.toString());

        for (int i = 0; i < allDevice.size(); i++) {
            multiConnectManager.removeDeviceFromQueue(allDevice.get(i));
            Log.i("connentBluetoothX", "斷開的傳感器" + allDevice.get(i));
        }

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                     // 等待3秒以後鏈接
                    EventBus.getDefault().post(new ConnectBle(1));

                 }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();    


當有數據,可是該設備不在已鏈接的設備集合中時,異常處理
 if (multiConnectManager.getAllConnectingDevice().contains(gatt.getDevice().getAddress())){
            // 若是正在鏈接中的設備列表中有當前傳輸數據的傳感器
            gatt.disconnect();
            return;
        }
View Code

 

 

若有問題,歡迎右側加羣。

相關文章
相關標籤/搜索