在物聯網蓬勃發展的當下,Android 低功耗裝置憑藉其便攜性與低能耗優勢,在眾多領域得到廣泛應用。其中,利用 Android 裝置採集藍牙感測器資料並進行直觀展示,成為許多智慧應用場景的關鍵需求。無論是健康監測、環境感知,還是工業控制,精準、穩妥的資料採集與展示都至關重要。本文將深入剖析 Android 低功耗裝置實現藍牙感測器資料採集與展示的開發過程,為開發者提供全面且實用的指導。
一、開發前的準備工作
1. 硬體選型
Android 低功耗裝置:選擇具備藍牙低功耗(BLE)功能的 Android 裝置,如部分智慧手錶、行動式資料採集終端等。有助於支援裝置系統版本支援所需的 BLE API,一般 Android 4.3 及以上版本對 BLE 有較好支援。
藍牙感測器:根據應用場景挑選合適的藍牙感測器,如心率感測器、溫度感測器、加速度感測器等。瞭解感測器的工作模式、資料傳輸格式以及通訊協議,有助於支援與 Android 裝置相容。
2. 軟體環境搭建
開發工具:安裝 Android Studio,這是官方推薦的 Android 應用開發整合環境,提供豐富的開發工具和除錯功能。
依賴庫:在專案中引入必要的依賴庫,如 Android BLE 相關庫,用於簡化 BLE 裝置的掃描、連線和資料讀取操作。在
build.gradle檔案中新增如下依賴:
groovydependencies { implementation 'androidx.core:core-ktx:1.12.0' implementation 'androidx.appcompat:appcompat:1.7.0' // 新增 BLE 相關庫,具體版本可根據實際情況調整 implementation 'no.nordicsemi.android:ble:2.2.3'}二、藍牙感測器資料採集實現
1. 掃描藍牙裝置
在 Android 應用中,需要先掃描周圍的藍牙裝置,找到目標感測器裝置。使用 BluetoothLeScanner 類來實現裝置掃描功能:
javaimport android.bluetooth.BluetoothAdapter;import android.bluetooth.BluetoothDevice;import android.bluetooth.le.BluetoothLeScanner;import android.bluetooth.le.ScanCallback;import android.bluetooth.le.ScanResult;import android.content.Context;public class BleScanner { private BluetoothLeScanner bluetoothLeScanner; private ScanCallback scanCallback; public BleScanner(Context context) { BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (bluetoothAdapter != null && bluetoothAdapter.isEnabled()) { bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner(); } } public void startScan() { scanCallback = new ScanCallback() { @Override public void onScanResult(int callbackType, ScanResult result) { super.onScanResult(callbackType, result); BluetoothDevice device = result.getDevice(); // 在這裡處理掃描到的裝置,判斷是否為目標感測器裝置 if (isTargetDevice(device)) { // 找到目標裝置,可進行連線等操作 //... } } }; if (bluetoothLeScanner != null) { bluetoothLeScanner.startScan(scanCallback); } } public void stopScan() { if (bluetoothLeScanner != null && scanCallback != null) { bluetoothLeScanner.stopScan(scanCallback); } } private boolean isTargetDevice(BluetoothDevice device) { // 根據裝置名稱、MAC 地址等特徵判斷是否為目標裝置 // 示例:假設目標裝置名稱為 "MySensor" return "MySensor".equals(device.getName()); }}2. 連線藍牙裝置
掃描到目標感測器裝置後,需要建立與裝置的連線。使用 BluetoothGatt 類來實現連線和資料通訊:
javaimport android.bluetooth.BluetoothDevice;import android.bluetooth.BluetoothGatt;import android.bluetooth.BluetoothGattCallback;import android.bluetooth.BluetoothGattCharacteristic;import android.bluetooth.BluetoothProfile;public class BleConnector { private BluetoothGatt bluetoothGatt; public void connect(BluetoothDevice device) { bluetoothGatt = device.connectGatt(null, false, new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { super.onConnectionStateChange(gatt, status, newState); if (newState == BluetoothProfile.STATE_CONNECTED) { // 裝置連線成功,可進行服務發現等操作 gatt.discoverServices(); } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { // 裝置斷開連線 //... } } @Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { super.onServicesDiscovered(gatt, status); if (status == BluetoothGatt.GATT_SUCCESS) { // 服務發現成功,可獲取感測器資料 // 假設已知感測器資料所在的特徵 UUID String characteristicUuid = "00002a37-0000-1000-8000-00805f9b34fb"; BluetoothGattCharacteristic characteristic = gatt.getService(/* 服務 UUID */).getCharacteristic(java.util.UUID.fromString(characteristicUuid)); if (characteristic != null) { // 啟用通知,以便接收感測器資料 gatt.setCharacteristicNotification(characteristic, true); //... } } } @Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { super.onCharacteristicChanged(gatt, characteristic); // 在這裡處理接收到的感測器資料 byte[] data = characteristic.getValue(); // 解析資料,根據感測器資料格式進行相應處理 //... } }); } public void disconnect() { if (bluetoothGatt != null) { bluetoothGatt.disconnect(); bluetoothGatt.close(); bluetoothGatt = null; } }}3. 資料解析與處理
不同的藍牙感測器傳輸的資料格式可能不同,需要根據感測器的資料手冊對接收到的位元組資料進行解析。例如,對於一個傳輸 16 位整數溫度值的感測器,可以按照以下方式解析:
javapublic float parseTemperatureData(byte[] data) { if (data != null && data.length >= 2) { int intValue = ((data[0] & 0xFF) << 8) | (data[1] & 0xFF); return intValue / 100.0f; // 假設資料以 0.01℃ 為單位傳輸 } return 0;}三、資料展示實現
1. 選擇合適的展示方式
根據應用場景和使用者需求,選擇合適的資料展示方式,如折線圖、柱狀圖、儀表盤等。可以使用第三方圖表庫,如 MPAndroidChart,它提供了豐富的圖表型別和自定義選項。
2. 在介面上展示資料
以折線圖為例,展示藍牙感測器採集到的溫度資料:
javaimport com.github.mikephil.charting.charts.LineChart;import com.github.mikephil.charting.data.Entry;import com.github.mikephil.charting.data.LineData;import com.github.mikephil.charting.data.LineDataSet;import java.util.ArrayList;import java.util.List;public class DataDisplayActivity extends AppCompatActivity { private LineChart lineChart; private List<Entry> temperatureEntries = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_data_display); lineChart = findViewById(R.id.line_chart); // 模擬新增一些初始資料 for (int i = 0; i < 10; i++) { temperatureEntries.add(new Entry(i, 20 + i)); } updateChart(); } // 當接收到新的感測器資料時呼叫此方法更新圖表 public void updateTemperatureData(float temperature) { // 移除最舊的資料點(如果需要限制資料點數量) if (temperatureEntries.size() >= 20) { temperatureEntries.remove(0); } // 新增新的資料點 temperatureEntries.add(new Entry(temperatureEntries.size(), temperature)); updateChart(); } private void updateChart() { LineDataSet dataSet = new LineDataSet(temperatureEntries, "Temperature"); dataSet.setColor(Color.BLUE); dataSet.setCircleColor(Color.RED); LineData lineData = new LineData(dataSet); lineChart.setData(lineData); lineChart.invalidate(); // 重新整理圖表 }}四、最佳化與測試
1. 功耗最佳化
合理設定掃描引數:減少掃描時間和頻率,避免長時間持續掃描消耗過多電量。
及時斷開連線:當不需要採集資料時,及時斷開與藍牙感測器的連線,降低功耗。
使用低功耗模式:如果感測器支援低功耗模式,在合適的時候切換到該模式。
2. 功能測試
連線測試:測試 Android 裝置能否成功掃描、連線和斷開藍牙感測器。
資料採集測試:驗證採集到的資料是否準確、完整,資料解析是否正確。
資料展示測試:檢查資料在介面上的展示是否清晰、直觀,圖表是否能即時更新。
線上諮詢
電話諮詢
微信諮詢
回到頂部