如何通过蓝牙低功耗(BLE)连接发送的数据? [英] How to send data over a Bluetooth Low Energy (BLE) link?

查看:522
本文介绍了如何通过蓝牙低功耗(BLE)连接发送的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够发现,连接到蓝牙。

I am able to discover, connect to bluetooth.

来源$ C ​​$ C ---

Source Code---

通过蓝牙远程设备连接:

//Get the device by its serial number
 bdDevice = mBluetoothAdapter.getRemoteDevice(blackBox);

 //for ble connection
 bdDevice.connectGatt(getApplicationContext(), true, mGattCallback);

盖特回调状态:

 private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
    //Connection established
    if (status == BluetoothGatt.GATT_SUCCESS
        && newState == BluetoothProfile.STATE_CONNECTED) {

        //Discover services
        gatt.discoverServices();

    } else if (status == BluetoothGatt.GATT_SUCCESS
        && newState == BluetoothProfile.STATE_DISCONNECTED) {

        //Handle a disconnect event

    }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {

    //Now we can start reading/writing characteristics

    }
};

现在我想将命令发送到远程BLE设备,但不知道该怎么做。

Now I want to send commands to Remote BLE device but don't know how to do that.

在该命令被发送到BLE设备,该设备BLE将由广播响应 数据,我的应用程序能够接收。

Once the command is sent to the BLE device, the BLE device will respond by broadcasting data which my application can receive.

推荐答案

您需要打破这个过程分成几个步骤,当您连接到一个BLE设备和发现服务:

You need to break this process into a few steps, when you connect to a BLE device and discover Services:

  1. 显示在 onServicesDiscovered 可用 gattServices 回调

要检查是否可以写一个特性或不
检查<一href="https://developer.android.com/reference/android/bluetooth/BluetoothGattCharacteristic.html#PROPERTY_WRITE">BluetoothGattCharacteristic性能 - 我并没有意识到需要让对BLE硬件PROPERTY_WRITE和浪费了大量的时间。

To check whether you can write a characteristic or not
check for BluetoothGattCharacteristic PROPERTIES -I didn't realize that need to enable the PROPERTY_WRITE on the BLE hardware and that wasted a lot of time.

当你写一个特点,它的硬件上执行任何操作来明确指示操作(在我的情况我是点亮LED)

When you write a characteristic, does the hardware perform any action to explicitly indicate the operation (in my case i was lighting an led)

假设 mWriteCharacteristic 是<一个href="https://developer.android.com/reference/android/bluetooth/BluetoothGattCharacteristic.html">BluetoothGattCharacteristic 部分地方检查的属性应该是这样的:

Suppose mWriteCharacteristic is a BluetoothGattCharacteristic The part where to check the PROPERTY should be like:

if (((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) |
     (charaProp & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE)) > 0) {
 // writing characteristic functions
 mWriteCharacteristic = characteristic;
  }

和,写下你的特点:

// "str" is the string or character you want to write
byte[] strBytes = str.getBytes();
byte[] bytes = activity.mWriteCharacteristic.getValue();  
YourActivity.this.mWriteCharacteristic.setValue(bytes);
YourActivity.this.writeCharacteristic(YourActivity.this.mWriteCharacteristic);  

这些都是的code中的有用的部分,你需要pcisely实现$ P $。

Those are the useful parts of the code that you need to implement precisely.

的GitHub项目与只是一个基本的演示的实现。

Refer this github project for an implementation with just a basic demo.

这篇关于如何通过蓝牙低功耗(BLE)连接发送的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆