筆者首先是編寫一個簡單的藍牙低功耗藍牙低功耗(Bluetooth Low Energy / BLE)掃描(Scan)設備的程式,測試是否能夠順利掃描藍牙低功耗的設備?開始編寫程式,也是碰到很多問題,主要是未能完全理解藍牙低功耗的連接步驟,所以編寫簡單的程式,對於剛剛開始是件好事,一步一步再加入其它功能來實現最終連接 Aneng AN9002 藍牙萬用電錶。
實現藍牙低功耗(BLE)掃描程式 |
- 操作系統:Windows 7 64-bit 版本
- 開發環境:Android Studio 4.0.1 版本
- 測試手機:Samsung Galaxy M33 5G
- 測試手機系統版本:Android 12(Snow Cone / 2021)
- 原程式:C:\Development\Development_Android\Android_Project\DIY-Android-013-03 BLE_Connect Bugworkshop 20230121
- 程式:C:\Development\Development_Android\Android_Project\ DIY-Android-013-03 BLE_Connect Bugworkshop 20230121
藍牙低功耗(BLE)掃描編程步驟:
- 開啟藍牙啟用權
- 在 Layout 配置檔(activity_ble_01)裡面放一個 TextView 和Button。
- 在 BLE01SetupActivity 內設定 BluetoothManager 和 TextView
- 掃描 BLE 設備並顯示在 TextView。
開發步驟如下:
首先是設置 BluetoothAdapter,獲取 BluetoothAdapter,然後開始掃描,監聽連接回檔,獲取藍牙驅動 Device Address,將藍牙驅動 Device Address 顯示,完成藍牙設備掃描。
AndroidManifest.xml:
<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> |
activity_ble_01.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".BLE01SetupActivity"> <Button android:id="@+id/btn_scan" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/text_hello" android:layout_alignParentStart="true" android:layout_marginStart="10dp" android:layout_marginTop="10dp" android:text="Scan" /> <TextView android:id="@+id/text_hello" android:layout_width="399dp" android:layout_height="49dp" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_marginStart="12dp" android:layout_marginTop="13dp" android:text="Hello" android:textSize="24sp" /> </RelativeLayout> |
設定 Scan Button 和 TextView → 設定 BluetoothManager → 設定 BluetoothAdapter → 確定 Bluetooth Enable
Scan→BluetoothAdapter 設定 Callback
Scan Callback→Display BLE Address
BLE01SetupActivity.java:
package bugworkshop.bleconnection; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothManager; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import androidx.appcompat.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; public class BLE01SetupActivity extends AppCompatActivity { private static final int REQUEST_ENABLE_BT = 10; private BluetoothAdapter mBluetoothAdapter; private Handler mHandler; private boolean mScanning; private static final long SCAN_PERIOD = 10000; private TextView mtxt_hello; private Button mbtn_scan; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ble_01); mtxt_hello = (TextView) findViewById(R.id.text_hello); mbtn_scan = (Button) findViewById(R.id.btn_scan); mbtn_scan.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { scaneLDevice(true); } }); mHandler = new Handler(); final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); mBluetoothAdapter = bluetoothManager.getAdapter(); if (!isBluetoothEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); return; } } // Bluetooth private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() { @Override public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) { runOnUiThread(new Runnable() { @Override public void run() { mtxt_hello.setText("Address: " + device.getAddress()); } }); } }; private boolean isBluetoothEnabled() { return mBluetoothAdapter != null && mBluetoothAdapter.isEnabled(); } private void scanLeDevice(final boolean enable) { if (enable) { // Stops scanning after a pre-defined scan period. mHandler.postDelayed(new Runnable() { @Override public void run() { mScanning = false; mBluetoothAdapter.stopLeScan(mLeScanCallback); } }, SCAN_PERIOD); mScanning = true; mBluetoothAdapter.startLeScan(mLeScanCallback); } else { mScanning = false; mBluetoothAdapter.stopLeScan(mLeScanCallback); } } } |
沒有留言:
張貼留言