安卓应用 : Connecting to GATT server using UUIDs

标签 android bluetooth bluetooth-lowenergy android-bluetooth gatt

我正在尝试从我的安卓应用程序连接到 Bleno Periphecal GATT 服务器。

GATT 服务器具有具有唯一 UUID 的自定义服务和特征。

我怎样才能精确连接到这个服务器并发送一些文本?

最小SDK是21,target SDK是24,所以旧的BluetoothLE Scanning方法已经弃用了,现在我需要使用BluetoothLEScanner。

最佳答案

要连接 Ble,只需使用此方法传递您的 BT 设备的 mac 地址。

 private boolean connectGatt(final String address) {
    if (mBluetoothAdapter == null || address == null) {
        Log.w(TAG, "BluetoothAdapter not initialized or unspecified address.");
        return false;
    }

    if (mBluetoothGatt != null) {
        Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection.");
        if (mBluetoothGatt.connect()) {
            return true;
        } else {
            return false;
        }
    }

    final BluetoothDevice device = mBluetoothAdapter
            .getRemoteDevice(address);
    if (device == null) {
        Log.w(TAG, "Device not found.  Unable to connect.");
        return false;
    }

    mBluetoothGatt = device.connectGatt(mContext, false, mGattCallback);
    Log.d(TAG, "Trying to create a new connection.");
    return mBluetoothGatt.connect();
}

你应该注册一个回调来知道连接是否成功。

 private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

        if (newState == BluetoothProfile.STATE_CONNECTED) {
            //bluetooth is connected so discover services
            mBluetoothGatt.discoverServices();

        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            //Bluetooth is disconnected
        }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
          // services are discoverd 
        }

        
    }

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt,
                                     BluetoothGattCharacteristic characteristic,
                                     int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            
        }
    }

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt,
                                        BluetoothGattCharacteristic characteristic) {

    }

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

一旦发现服务,您就可以写入或读取服务。

使用此方法写入服务

 private boolean writeRXCharacteristic(byte[] value) {
    BluetoothGattService RxService = mBluetoothGatt.getService(/*Place service UUID*/);
    if (RxService == null) {
        //Service not supported
        return false;
    }
    BluetoothGattCharacteristic RxChar = RxService.getCharacteristic(/*RX_CHAR_UUID*/);
    if (RxChar == null) {
        // service not supported
        return false;
    }
     RxChar.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
        RxChar.setValue(arr);
        return mBluetoothGatt.writeCharacteristic(RxChar);
}

关于安卓应用 : Connecting to GATT server using UUIDs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40646567/

相关文章:

java - Libgdx 变量类型的内存大小

android - 找不到处理 https ://Intent 的 Activity

android - 在 ListView 中的每一行周围放置一个边框

Android 与 RFCOMM 设备配对无法连接

ios - 从 BLE 设备获取通知

android - 如果传入的数据包太多,则不会调用 onCharacteristicChanged() 回调

android - 禁用选项卡之间的滑动

java - 接收来自特征的通知

android - 以无线方式将 Android 连接到 Arduino 的最快方法?

iOS 将 WIFI 设置传递给蓝牙(BLE)设备?