Android 应用程序,通过 UART 通过 BLE 模块接收微 Controller 发送的数据

标签 android bluetooth-lowenergy microcontroller stm32 uart

我想创建android应用程序,它可以与蓝牙低功耗模块连接并可以接收数据。在我的系统中,微 Controller stm32f1 通过 UART 将测量数据发送到 BT LE 模块。

我的问题是如何开始?我读了很多关于 GATT 和 UART 服务的内容,但仍然不知道如何开始。请给我一些信息。

最佳答案

您需要:

  • 您的服务、特征和描述符的 UUID;

  • 权限低功耗蓝牙

该计划必须:

  1. 扫描 BLE 设备
  2. 连接设备
  3. 设置回调的 BLE

AndroidManifest.xml

<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

Activity.java

// Bluetooth's variables
    BluetoothAdapter bluetoothAdapter;
    BluetoothLeScanner bluetoothLeScanner;
    BluetoothManager bluetoothManager;
    BluetoothScanCallback bluetoothScanCallback;
    BluetoothGatt gattClient;

    BluetoothGattCharacteristic characteristicID; // To get Value

// UUID's (set yours)
final UUID SERVICE_UUID = UUID.fromString("ab0828b1-198e-4351-b779-901fa0e0371e");
final UUID CHARACTERISTIC_UUID_ID = UUID.fromString("1a220d0a-6b06-4767-8692-243153d94d85");
final UUID DESCRIPTOR_UUID_ID = UUID.fromString("ec6e1003-884b-4a1c-850f-1cfce9cf6567");

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Bluetooth
    bluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
    bluetoothAdapter = bluetoothManager.getAdapter();
    startScan();
}

 // BLUETOOTH SCAN

private void startScan(){
    Log.i(TAG,"startScan()");
    bluetoothScanCallback = new BluetoothScanCallback();
    bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
    bluetoothLeScanner.startScan(bluetoothScanCallback);
}

// BLUETOOTH CONNECTION
private void connectDevice(BluetoothDevice device) {
    if (device == null) Log.i(TAG,"Device is null");
    GattClientCallback gattClientCallback = new GattClientCallback();
    gattClient = device.connectGatt(this,false,gattClientCallback);
}

// BLE Scan Callbacks
private class BluetoothScanCallback extends ScanCallback {

    @Override
    public void onScanResult(int callbackType, ScanResult result) {
        Log.i(TAG, "onScanResult");
        if (result.getDevice().getName() != null){
            if (result.getDevice().getName().equals(YOUR_DEVICE_NAME)) {
                // When find your device, connect.
                connectDevice(result.getDevice());
                bluetoothLeScanner.stopScan(bluetoothScanCallback); // stop scan
            }
        }
    }

    @Override
    public void onBatchScanResults(List<ScanResult> results) {
        Log.i(TAG, "onBathScanResults");
    }

    @Override
    public void onScanFailed(int errorCode) {
        Log.i(TAG, "ErrorCode: " + errorCode);
    }            
}

// Bluetooth GATT Client Callback
private class GattClientCallback extends BluetoothGattCallback {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        super.onConnectionStateChange(gatt, status, newState);
        Log.i(TAG,"onConnectionStateChange");

        if (status == BluetoothGatt.GATT_FAILURE) {
            Log.i(TAG, "onConnectionStateChange GATT FAILURE");
            return;
        } else if (status != BluetoothGatt.GATT_SUCCESS) {
            Log.i(TAG, "onConnectionStateChange != GATT_SUCCESS");
            return;
        }

        if (newState == BluetoothProfile.STATE_CONNECTED) {
            Log.i(TAG, "onConnectionStateChange CONNECTED");
            gatt.discoverServices();
        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            Log.i(TAG, "onConnectionStateChange DISCONNECTED");
        }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        super.onServicesDiscovered(gatt, status);
        Log.i(TAG,"onServicesDiscovered");
        if (status != BluetoothGatt.GATT_SUCCESS) return;

        // Reference your UUIDs
        characteristicID = gatt.getService(SERVICE_UUID).getCharacteristic(CHARACTERISTIC_UUID_ID);
        gatt.setCharacteristicNotification(characteristicID,true);

        BluetoothGattDescriptor descriptor = characteristicID.getDescriptor(DESCRIPTOR_UUID_ID);
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        gatt.writeDescriptor(descriptor);
    }

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        super.onCharacteristicRead(gatt, characteristic, status);
        Log.i(TAG,"onCharacteristicRead");
    }

    @Override
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        super.onCharacteristicWrite(gatt, characteristic, status);
        Log.i(TAG,"onCharacteristicWrite");
    }

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
        super.onCharacteristicChanged(gatt, characteristic);
        Log.i(TAG,"onCharacteristicChanged");
        // Here you can read the characteristc's value
        // new String(characteristic.getValue();
    }

    @Override
    public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
        super.onDescriptorRead(gatt, descriptor, status);
        Log.i(TAG,"onDescriptorRead");
    }

    @Override
    public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
        super.onDescriptorWrite(gatt, descriptor, status);
        Log.i(TAG,"onDescriptorWrite");
    }
}

库:

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanResult;

注释:

  • UUID 必须与模块上设置的相同。
  • 在您的程序中,您必须有两个回调:ScanCallback 和 GattCallback。 Scan回调用于管理扫描结果,GattCallback可以管理数据输入/输出。
  • 此代码基本展示了如何在 Android 上使用 BLE,对我来说效果很好。
  • 您可以在此处生成 UUID:https://www.uuidgenerator.net/

关于Android 应用程序,通过 UART 通过 BLE 模块接收微 Controller 发送的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53282386/

相关文章:

android - 将以 "content//:"或 "/data/.."开头的路径转换为 ​​URI

arm - 无法删除 samd21 微 Controller 闪存的第一页

c - 嵌入式设备(微 Controller )静态链接固件中的远程可更新功能或代码

android - 如何初始化ViewBinding?

java.lang.NoSuchMethodError : No static method clearInstance()

android - 如何将 LinearLayout 转换为图像?

ios - Objective-C 禁用 iOS BLE 配对消息

android - 在 Android 中使用 BLE(iBeacons) 标签进行三角测量

Android BLE 在绑定(bind)后断开连接

microcontroller - STMicro 是否错误地解释了术语 "shadow register"?