小编abh*_*ICT的帖子

在Android中将数据写入蓝牙LE特性

虽然已经提出了类似的问题,但它略有不同.我知道如何将数据传递给连接的BLE设备,但我认为我做错了什么我需要帮助.下面的代码包含我的类中扩展BroadcastReceiver的所有方法.

  1. 我扫描并连接到'PEN_ADDRESS`指定的设备.
  2. 在`onServicesDiscovered`方法中,我寻找一个'UUID`包含`abcd`的服务.
  3. 然后我循环遍历这些服务的特性,并在其"UUID"中查找具有特定字符串的三个特征.
  4. 第三个特性是一个可写特性,我试图通过调用方法`writeCharac(mGatt,writeChar1,123)来写数据;`上面传递的数据`123`只是一个伪数据.

我在尝试写入此特性时调试了我的代码,但是在writeCharac方法中放置了断点,我发现status值为false,表明写入不成功.我在这里错过了什么吗?请帮忙!

public class BackgroundReceiverFire extends BroadcastReceiver {
Context context;
private BluetoothAdapter mBluetoothAdapter;
private BluetoothGatt mGatt;
private BluetoothLeService mBluetoothLeService;
private boolean mScanning;
private final String TAG = "READING: ";
private BluetoothDevice mDevice;
private Handler mHandler;
private static final int REQUEST_ENABLE_BT = 1;
private final String PEN_ADDRESS = "FB:23:AF:42:5C:56";
// Stops scanning after 10 seconds.
private static final long SCAN_PERIOD = 10000;

    public void onReceive(Context context, Intent intent) {
        this.context = context;
        Toast.makeText(context, …
Run Code Online (Sandbox Code Playgroud)

android bluetooth bluetooth-lowenergy

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

android ×1

bluetooth ×1

bluetooth-lowenergy ×1