BLE主机端通过点击Characterstic “read”,从机端发送消息原理及方法介绍

**

前言:

**
1.主机端通过NRF Connect App连接蓝牙设备
2.芯片为NRF52840
3.程序为Nordic官方提供程序

1.问题描述:手机(主机)连接蓝牙从机后,需要读取从机的信息,在自定义服务/标准服务下,通过操作Characterstic 下“read”按键,读取从机端的信息。
在这里插入图片描述

2.原理介绍:
主机read是在SD(softdevice)中获取数据的,而在APP层,是通过函数库sd_ble_gatts_value_set()
这个函数更新信息到SD的,read的动作并没有在APP层上报信息/发生事件,
所以在APP层找不到相关主机请求数据的事件。
在这里插入图片描述
3.应用:
1).将不需要改变的值(例如硬件/软件版本号等)可以通过在初始化Characterstic的时候将其写入SD中,具体的操作示例如下:
将APK版本号写到SD中
在初始化Characterstic时赋值即可
在这里插入图片描述

#define CONFIG_APK_REVISION  "2.9.0"
ble_add_char_params_t addCharParams;

在这里插入图片描述
2).需要读取从机实时的值给主机
通过调用APIsd_ble_gatts_value_set()将值写到SD中
在这里插入图片描述
现象:读取到写入的值为08
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Android Studio开发BLE向手发送消息的代码示例: 1. 添加权限到AndroidManifest.xml文件中: ```xml <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> ``` 2. 在布局文件中添加一个按钮和一个文本框: ```xml <Button android:id="@+id/send_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Send"/> <EditText android:id="@+id/message_edittext" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter message"/> ``` 3. 在Activity或Fragment中添加以下代码: ```java private BluetoothAdapter bluetoothAdapter; private BluetoothGatt bluetoothGatt; private BluetoothGattCharacteristic characteristic; private Button sendButton; private EditText messageEditText; private final String SERVICE_UUID = "0000fff0-0000-1000-8000-00805f9b34fb"; private final String CHARACTERISTIC_UUID = "0000fff1-0000-1000-8000-00805f9b34fb"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); sendButton = findViewById(R.id.send_btn); messageEditText = findViewById(R.id.message_edittext); sendButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String message = messageEditText.getText().toString(); if (characteristic != null && message != null) { characteristic.setValue(message.getBytes()); bluetoothGatt.writeCharacteristic(characteristic); } } }); } private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { if (newState == BluetoothProfile.STATE_CONNECTED) { bluetoothGatt.discoverServices(); } } @Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { BluetoothGattService service = gatt.getService(UUID.fromString(SERVICE_UUID)); if (service != null) { characteristic = service.getCharacteristic(UUID.fromString(CHARACTERISTIC_UUID)); if (characteristic != null) { gatt.setCharacteristicNotification(characteristic, true); } } } @Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { // Handle incoming data } }; private void connectToDevice(BluetoothDevice device) { if (bluetoothGatt != null) { bluetoothGatt.close(); } bluetoothGatt = device.connectGatt(this, false, gattCallback); } private void scanForDevices() { BluetoothLeScanner scanner = bluetoothAdapter.getBluetoothLeScanner(); if (scanner != null) { scanner.startScan(new ScanCallback() { @Override public void onScanResult(int callbackType, ScanResult result) { BluetoothDevice device = result.getDevice(); if (device.getName() != null && device.getName().equals("MyDeviceName")) { connectToDevice(device); } } }); } } @Override protected void onResume() { super.onResume(); if (bluetoothAdapter != null && !bluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } else { scanForDevices(); } } @Override protected void onPause() { super.onPause(); if (bluetoothGatt != null) { bluetoothGatt.close(); bluetoothGatt = null; } } ``` 4. 注意替换SERVICE_UUID和CHARACTERISTIC_UUID为你自己设备的UUID。 5. 运行应用程序,输入消息并单Send按钮。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值