日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

蓝牙防丢器实现安卓的BLE接口编程

發(fā)布時(shí)間:2024/3/24 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 蓝牙防丢器实现安卓的BLE接口编程 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

藍(lán)牙防丟profile
1.TXP(txpower) Characteristic, 設(shè)備端需要通過(guò)主機(jī)控制接口HCI來(lái)獲得發(fā)射功率參數(shù),并以read屬性提供給master。

2.IAS(immediate alter service), write屬性,供master寫(xiě)告警級(jí)別。當(dāng)master寫(xiě)入新的值時(shí),設(shè)備端會(huì)收到write的回調(diào),其根據(jù)告警級(jí)別進(jìn)行相應(yīng)告警。

  • LLS(link loss service),write/read屬性,供master設(shè)置鏈路斷開(kāi)情況下默認(rèn)的告警級(jí)別。
  • RSSI通過(guò)接收端的接口來(lái)獲得,并不需要設(shè)備端提供service。

    以上Characteristic都通過(guò)GATT profile提供服務(wù),在藍(lán)牙通信協(xié)議上,每個(gè)Characteristic都會(huì)對(duì)應(yīng)一個(gè)UUID。

    android藍(lán)牙BLE接口編程
    androidBLE接口在android4.3版本以上提供。

  • 判斷當(dāng)前系統(tǒng)是否支持BLE
  • getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)

    返回真表示支持。

  • 獲得藍(lán)牙適配器類(lèi)
  • 用戶(hù)通過(guò)統(tǒng)一的藍(lán)牙適配器類(lèi)BluetoothAdapter來(lái)使用BLE API。

    先獲得藍(lán)牙管理器:

    BluetoothManagerbluetoothManager = getSystemService(Context.BLUETOOTH_SERVICE);

    再獲得藍(lán)牙適配器實(shí)例(單體對(duì)象):

    BluetoothAdaptermBluetoothAdapter = bluetoothManager.getAdapter();

  • 啟動(dòng)手機(jī)藍(lán)牙硬件功能(相當(dāng)于在設(shè)置界面開(kāi)啟藍(lán)牙功能)
  • mBluetoothAdapter.enable();

  • 開(kāi)始掃描
  • BluetoothAdapter.startLeScan(android.bluetooth.BluetoothAdapter.LeScanCallbackcallback)

    callback是當(dāng)掃描到藍(lán)牙設(shè)備時(shí)的回調(diào)接口。實(shí)現(xiàn)callback中的onLeScan接口:

    @Override

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

    其中,device代表掃描到的設(shè)備,可以獲得其MAC地址、設(shè)備名等等;rssi即信號(hào)強(qiáng)度,這是未連接時(shí)獲取RSSI的方法;scanRecord代表掃描設(shè)備得到的響應(yīng)參數(shù),ibeacon即通過(guò)該參數(shù)來(lái)獲得廣播內(nèi)容。

    假設(shè)String bluetoothAddress = device.getAddress(),獲取藍(lán)牙48位MAC地址

  • 連接GATT,獲取設(shè)備端的UUID服務(wù),并進(jìn)行數(shù)據(jù)通信交互
  • 通過(guò)MAC地址獲得代表設(shè)備端的藍(lán)牙設(shè)備類(lèi)

    BluetoothDevicedevice = mBluetoothAdapter.getRemoteDevice(bluetoothAddress);

    連接GATT

    BluetoothGatt mBluetoothGatt = device.connectGatt(android.content.Context context, booleanautoConnect, android.bluetooth.BluetoothGattCallback callback);

    Callback是連接GATT之后,所有數(shù)據(jù)交互的回調(diào)入口。分別包括:

    1)設(shè)備服務(wù)發(fā)現(xiàn)

    @Override

    publicvoid onServicesDiscovered(BluetoothGatt gatt, int status)

    mBluetoothGatt.getServices()代表設(shè)備服務(wù)集合,

    for (BluetoothGattService gattService : mBluetoothGatt.getServices())

    對(duì)于每個(gè)服務(wù)service,用getUuid()可以獲得服務(wù)的UUID,getCharacteristics()代表該服務(wù)的Characteristic集合。

    for(BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics)

    對(duì)于每個(gè)Characteristic,getUuid()獲得UUID,getPermissions()獲得屬性權(quán)限,getValue()獲得屬性值。

    在該回調(diào)中我們只提取感興趣的三個(gè)Characteristic的UUID,對(duì)于其他的如電池、設(shè)備服務(wù)等UUID可以不管。

    gattCharacteristic_char5_TXP=gattCharacteristic;

    2)連接狀態(tài)改變

    @Override

    public voidonConnectionStateChange(BluetoothGatt gatt, int status,intnewState)

    有兩種狀態(tài),BluetoothProfile.STATE_CONNECTED代表連接,BluetoothProfile.STATE_DISCONNECTED代表斷開(kāi)連接。

    3)讀回調(diào)

    @Override

    public voidonCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristiccharacteristic, intstatus)

    其對(duì)應(yīng)手機(jī)端發(fā)出讀請(qǐng)求后,當(dāng)收到設(shè)備端的數(shù)據(jù)時(shí)的回調(diào)。如

    mBluetoothGatt.readCharacteristic(gattCharacteristic_char5_TXP)

    4)設(shè)備端數(shù)據(jù)變化回調(diào)

    這里對(duì)應(yīng)設(shè)備的characteristic的屬性是notify或者indication,即相當(dāng)手機(jī)端訂閱這個(gè)characteristic的值變更服務(wù),當(dāng)設(shè)備端的characteristic發(fā)生變化時(shí),設(shè)備端會(huì)主動(dòng)發(fā)出通知給手機(jī)端。

    @Override

    public voidonCharacteristicChanged(BluetoothGatt gatt,

    BluetoothGattCharacteristiccharacteristic)

    在回調(diào)中獲得新的值characteristic.getValue()。

    5)獲取到RSSI值的回調(diào)

    RSSI在掃描時(shí)可以通過(guò)掃描回調(diào)接口獲得,但是在連接之后要不斷地使用

    mBluetoothGatt.readRemoteRssi()向底層驅(qū)動(dòng)發(fā)出讀取RSSI請(qǐng)求,當(dāng)?shù)讓荧@取到新的RSSI后會(huì)進(jìn)行以下回調(diào):

    @Override

    public voidonReadRemoteRssi(BluetoothGatt gatt, int rssi, int status)

    rssi即是新的信號(hào)強(qiáng)度值。

    連接后,由于手機(jī)和設(shè)備端的距離在發(fā)生變化,因此要不斷地讀取RSSI,實(shí)時(shí)計(jì)算兩者之間的距離才能保證防丟功能的實(shí)現(xiàn)。

    總結(jié)

    以上是生活随笔為你收集整理的蓝牙防丢器实现安卓的BLE接口编程的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。