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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android BLE学习(一): Android搜索BLE设备

發布時間:2023/12/15 Android 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android BLE学习(一): Android搜索BLE设备 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?http://my.csdn.net/lidec


背景

總結一下最近ble的學習情況。自從入手ble 51822開發板后就開始不停加班,中途出于好奇,業余時間寫了一些單片機上json解析相關的東西,妄圖使用藍牙傳輸json數據,不知道是否實用,既然開始寫了,得寫出點樣子,晃晃蕩蕩,2016年的1月份就過去了。

這里本章我們主要總結一下ble搜索相關的內容,先建立直觀印象,然后剖析ble模塊與Android相關代碼,看看源碼與現象是如何對應的。最后,當我們了解流程后,就可以比較容易地理解藍牙協議中的一些內容,最終實現照我們自己的需求建立協議,開發屬于我們自己的模塊的目的。

51822中自帶了藍牙協議棧,協議棧也規定了程序的框架,感覺這樣的好處就是簡化了開發流程,我們可以按照能跑通的demo進行修改即可,方便學習。

Android BLE 搜索

BluetoothAdapter

以下是android官方對這個類的介紹

Represents the local device Bluetooth adapter. The BluetoothAdapter lets you perform fundamental Bluetooth tasks, such as initiate device discovery, query a list of bonded (paired) devices, instantiate a BluetoothDevice using a known MAC address, and create a BluetoothServerSocket to listen for connection requests from other devices, and start a scan for Bluetooth LE devices.

To get a BluetoothAdapter representing the local Bluetooth adapter, when running on JELLY_BEAN_MR1 and below, call the static getDefaultAdapter() method; when running on JELLY_BEAN_MR2 and higher, retrieve it through getSystemService(String) with BLUETOOTH_SERVICE. Fundamentally, this is your starting point for all Bluetooth actions. Once you have the local adapter, you can get a set of BluetoothDevice objects representing all paired devices with getBondedDevices(); start device discovery with startDiscovery(); or create a BluetoothServerSocket to listen for incoming connection requests with listenUsingRfcommWithServiceRecord(String, UUID); or start a scan for Bluetooth LE devices with startLeScan(LeScanCallback).

大意在講這個類代表了本地藍牙適配器,可以對藍牙進行一些基本操作,比如:?
1.發現設備?
2.獲取配對設備列表?
3.獲取設備mac地址?
4.創建監聽?
5.搜索BLE設備

我們所需要的就是其搜索BLE設備的功能。這也就是為什么我們的Android程序在運行時只能看到ble設備而看不到其他一些藍牙設備的原因。

下面看一下我們需要用到搜索ble設備的API

boolean startLeScan(BluetoothAdapter.LeScanCallback callback)

啟動搜索BLE設備

boolean startLeScan(UUID[] serviceUuids, BluetoothAdapter.LeScanCallback callback)

搜索指定serviceUUID的BLE設備

void stopLeScan(BluetoothAdapter.LeScanCallback callback)

停止收索BLE設備

其中的回調函數就是收索到設備后進行的回調。

BlueToothAdapter通過BluetoothManager的getAdapter()方法獲取實例,具體代碼如下:

final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);mBluetoothAdapter = bluetoothManager.getAdapter();
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

搜索流程

開始開啟藍牙設備檢查BLE設備是否可用BLE設備可用?開始搜索BLE設備并獲取相關信息結束yesno

開啟藍牙設備

這里我們使用一個Intent來開啟系統牙,此處我們還需要為應用添加藍牙相關權限

<uses-permission android:name="android.permission.BLUETOOTH" /><uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
  • 1
  • 2
  • 1
  • 2

開啟藍牙,返回時接收Result,用于顯示藍牙是否開啟成功。

Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableIntent, 1);
  • 1
  • 2
  • 1
  • 2

檢查BLE是否可用

private void checkBLEDevice(){// 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, " ble not supported ", Toast.LENGTH_SHORT).show();finish(); }// Initializes a Bluetooth adapter. For API level 18 and above, get a reference to // BluetoothAdapter throughBluetoothManager. 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, " ble not supported ", Toast.LENGTH_SHORT).show();finish();return;} }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

搜索

mBluetoothAdapter.stopLeScan(mBLEScanCallback);//啟動搜索BLE設備mBluetoothAdapter.startLeScan(mBLEScanCallback);//停止搜索BLE設備
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

當收索到內容時,將調用我們設置的回調函數。可以得到信號強度和BLE設備信息以及廣播內容。

new BluetoothAdapter.LeScanCallback(){@Overridepublic void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) {Log.i("MainActivity", device.getAddress());addBLEDeviceData(device, rssi);}};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

VONCHENCHEN_BLE就是我們的51822 BLE設備。

小結

至此,我們就完成了BLE設備的搜索,總結一下就是使用BluetoothAdapter類提供的方法完成對BLE設備的掃描,獲取到BLE設備的相關信息,如設備名字和Mac地址等,我們可以使用這些信息進行藍牙連接。

總結

以上是生活随笔為你收集整理的Android BLE学习(一): Android搜索BLE设备的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。