Android藍牙通信

基礎(chǔ)準備

藍牙通信概念

通常意義的藍牙通信的含義就是Client通過藍牙對接后進行數(shù)據(jù)通信,這里面需要注意的是藍牙通信是點對點的,所以是1對1關(guān)系,所以就有了Central vs. peripheral (中心設(shè)備vs外圍設(shè)備)的概念,接下來我們將以Client角度闡述

藍牙通信首先要了解幾個重要的類

BluetoothAdapter
藍牙通信的核心類,單例模式,通過BluetoothAdapter.getDefaultAdapter()獲得或者通過藍牙服務獲取
BluetoothDevice
對應可搜索到的藍牙設(shè)備,可使用這個類進行連接
BluetoothGatt
連接成功后的返回,可用來關(guān)閉連接,發(fā)現(xiàn)服務等等

具體開發(fā)流程(代碼)

1.首先需要檢查權(quán)限

<uses-featureandroid:name="android.hardware.bluetooth_le"android:required="true"/>
 
<uses-permissionandroid:name="android.permission.BLUETOOTH"/>

<uses-permissionandroid:name="android.permission.BLUETOOTH_ADMIN"/>

2.檢查ble支持

f(!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
            Toast.makeText(this,R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
            finish();
        }

3.獲取藍牙代理

BluetoothAdapter   bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
或者
BluetoothManager bm = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter bluetoothAdapter = bm.getAdapter();

4.檢查藍牙是否開啟

if(!mBluetoothAdapter.isEnabled()) {

            if (!mBluetoothAdapter.isEnabled()){

                Intent enableBtIntent = newIntent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

               startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);

            }

        }

5.搜索藍牙設(shè)備

mHandler.postDelayed(newRunnable() {

                @Override
                public void run() {

                    mScanning = false;

                   mBluetoothAdapter.stopLeScan(mLeScanCallback);
                  
                }

            }, SCAN_PERIOD); 

            mScanning = true;

           mBluetoothAdapter.startLeScan(mLeScanCallback);

這里需要注意的是因為藍牙搜索比較費電,所以我們過一個時間間隔就關(guān)閉它,
startLeScan(UUID[], BluetoothAdapter.LeScanCallback)有個重載可通過uuid搜索指定的設(shè)備

privateBluetoothAdapter.LeScanCallback mLeScanCallback =

            newBluetoothAdapter.LeScanCallback() {
 
        @Override

        public void onLeScan(finalBluetoothDevice device, int rssi, byte[] scanRecord) {

            runOnUiThread(new Runnable() {

                @Override

                public void run() {

                    mLeDeviceListAdapter.addDevice(device);

                   mLeDeviceListAdapter.notifyDataSetChanged();

                }

            });

        }

};

一旦搜索到設(shè)備就加到顯示列表
6.獲取到需要連接的device后連接

BluetoothGatt mBluetoothGatt = device.connectGatt(this, false,BluetoothGattCallback);

這個GattCallback有9個回調(diào),主要提供狀態(tài)變換后的數(shù)據(jù)通信,BluetoothGatt 用來可用來寫數(shù)據(jù)

Paste_Image.png

可以看源碼大致了解函數(shù)功能
7.連接后,就可以通訊了
BLE的三部分Service,Characteristic,Descriptor,所以大致講下這幾個概念
Service
Characteristic的集合。比如心率可以分成好幾個Characteristic來秒速他
Characteristic
一個數(shù)據(jù)類型,包含一個value和0-n個對value的Descriptor
Descriptor
對Characteristic的描述,比如返回,單位
這三者的查找都是通過uuid,然后通過獲取相應的類進行操作
寫數(shù)據(jù)
首先需要一個Characteristic對象,然后往里面放數(shù)據(jù),這里我們放入byte[]

Characteristic characteristic=new Characteristic();
characteristic.setValue(byte[] mByte);

bluetoothGatt.writeCharacteristic(characteristic); 

這時候就會收到GattCallback里的onCharacteristicWrite回調(diào)
讀數(shù)據(jù)

bluetoothGatt.readCharacteristic(characteristic); 

調(diào)用此方法后會收到GattCallback的onCharacteristicRead回調(diào),或者數(shù)據(jù)正常傳過來的情況下此方法也會被回調(diào),通過Characteristic.getValue方法即可收到相應數(shù)值

Summarize

流程大概就如上所示,有幾個點需要注意
比如connect需要先discoverServices
還有一些數(shù)據(jù)轉(zhuǎn)換的問題,16進制,byte[],十進制,字符串...
本文參考woshasanguo藍牙開發(fā)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • BLE 與經(jīng)典藍牙的區(qū)別 BLE 的 Kotlin 下實踐 BluetoothGattCallback 不回調(diào)異常...
    chauI閱讀 11,591評論 1 7
  • 前言: 本文主要描述Android BLE的一些基礎(chǔ)知識及相關(guān)操作流程,不牽扯具體的業(yè)務實現(xiàn),其中提供了針對廣播包...
    幻影宇寰閱讀 5,589評論 6 19
  • 近來工作做了很多Android手機對接藍牙設(shè)備的工作,記錄一下。 場景 需要對接藍牙設(shè)備,發(fā)送或者獲取數(shù)據(jù),比如藍...
    自然like閱讀 5,063評論 0 4
  • 初識低功耗藍牙 Android 4.3(API Level 18)開始引入Bluetooth Low Energy...
    JBD閱讀 113,561評論 46 342
  • ?我的前半生?看到后面越是叫人欲罷不能,心機婊表現(xiàn)的是越來越明顯了,老公也忍無可忍了。而我們的子君,卻成了一個香餑...
    19畫生閱讀 285評論 0 2

友情鏈接更多精彩內(nèi)容