Jasonchenlijian / FastBle

Android Bluetooth Low Energy (BLE) Fast Development Framework. It uses simple ways to filter, scan, connect, read ,write, notify, readRssi, setMTU, and multiConnection.
Apache License 2.0
5.29k stars 1.22k forks source link

No reliable write support #477

Open taimour1446 opened 4 years ago

taimour1446 commented 4 years ago

Please include reliable write support for this library as I cannot see it anywhere

kingiis commented 4 years ago

BleManager.getInstance().connect(bleDevice, new BleGattCallback() { @Override public void onConnectSuccess(BleDevice bleDevice, BluetoothGatt gatt, int status) {

//I'm connected here. Find your data service and channel according to your Bluetooth protocol String uuid = context.getInfo().cmut_uuid;//Server data String uuid_w = context.getInfo().cmut_uuid_w;//Server data String uuid_n = context.getInfo().cmut_uuid_r;//Server data

App.UUID_SERVICE = gatt.getService(UUID.fromString(uuid));
                UUID_SERVICE = App.UUID_SERVICE;

                App.UUID_WRITE = App.UUID_SERVICE.getCharacteristic(UUID.fromString(uuid_w));
                UUID_WRITE = App.UUID_WRITE;

                App.UUID_NOTIFY = App.UUID_SERVICE.getCharacteristic(UUID.fromString(uuid_n));
                UUID_NOTIFY = App.UUID_NOTIFY;

//Fire at will

taimour1446 commented 4 years ago

I want to get response data from ble when I write to ble device and get device response data on OnWriteSuccess. How this gonna help me in that situation? Can you explain further?

kingiis commented 4 years ago

public void startConn(BleDevice bleDevice) {

    BleManager.getInstance().connect(bleDevice, new BleGattCallback() {
        @Override
        public void onStartConnect() {
            Ui.tip(context, context.getString(R.string.CONN_START));

// Log.i("tag", "开始链接"); }

        @Override
        public void onConnectFail(BleDevice bleDevice, BleException exception) {
            Ui.tip(context, context.getString(R.string.CONN_FAILE));

// Log.i("tag", "链接失败"); }

        @Override
        public void onConnectSuccess(BleDevice bleDevice, BluetoothGatt gatt, int status) {

// Log.i("tag", "链接成功|" + bleDevice.getMac()); Ui.tip(context, context.getString(R.string.CONN_SUCCESS)); //存储链接成功的mac context.saveMac(bleDevice.getMac()); App.deviceMac = bleDevice.getMac(); App.deivce = bleDevice; if (!mDevice.contains(bleDevice)) mDevice.add(bleDevice); // for (BluetoothGattService service : gatt.getServices()) { // Log.i("tag", "service|" + service.getUuid()); // for (BluetoothGattCharacteristic chara : //// gatt.getService(UUID.fromString(context.getInfo().cmut_uuid)).getCharacteristics() // service.getCharacteristics() // ) { // Log.i("tag", "oo|" + chara.getUuid()); // } // } //6e400001-b5a3-f393-e0a9-e50e24dcca9e

            try {

                String uuid = context.getInfo().cmut_uuid;
                String uuid_w = context.getInfo().cmut_uuid_w;
                String uuid_n = context.getInfo().cmut_uuid_r;

// String uuid = BLE.LG_UUID_SERVICE; // String uuid_w = BLE.LG_UUID_WRITE; // String uuid_n = BLE.LG_UUID_NOTIFY;

                App.UUID_SERVICE = gatt.getService(UUID.fromString(uuid));
                UUID_SERVICE = App.UUID_SERVICE;

                App.UUID_WRITE = App.UUID_SERVICE.getCharacteristic(UUID.fromString(uuid_w));
                UUID_WRITE = App.UUID_WRITE;

                App.UUID_NOTIFY = App.UUID_SERVICE.getCharacteristic(UUID.fromString(uuid_n));
                UUID_NOTIFY = App.UUID_NOTIFY;
            } catch (Exception e) {
                Ui.tip(context, context.getString(R.string.BluetoothUnable1));
                App.GATT = null;
                mGatt = null;
                gatt.disconnect();
                gatt.close();
                return;
            }

            if (App.UUID_WRITE == null) {
                gatt.disconnect();
                gatt.close();
                return;
            }
            boolean connState = gatt.setCharacteristicNotification(App.UUID_NOTIFY, true);

// //频率ok if (connState) { onBleConnSetNotify(bleDevice, App.UUID_SERVICE.getUuid().toString(), App.UUID_NOTIFY.getUuid().toString()); App.GATT = gatt; mGatt = gatt;

// sendMsg(bleDevice, // context.getInfo().cmut_uuid, // context.getInfo().cmut_uuid_w, // ); } else {//协议错误 不能连 App.GATT = null; mGatt = null; gatt.disconnect(); gatt.close(); } }

        @Override
        public void onDisConnected(boolean isActiveDisConnected, BleDevice device, BluetoothGatt gatt, int status) {
            Ui.tip(context, context.getString(R.string.CONN_DISSCONN));
            onBleConnSetNotifyOff(device, UUID_SERVICE.getUuid().toString(), UUID_NOTIFY.getUuid().toString());
            showConn();

// Log.i("tag", "断开链接"); } });

}

void onBleConnSetNotify(BleDevice bleDevice, String serviceId, String charId) { // for(BluetoothGattDescriptor dp: characteristic.getDescriptors()){ // if (dp != null) { // if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) { // dp.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); // } else if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0) { // dp.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); // } // gatt.writeDescriptor(dp); // } // } BleManager.getInstance().notify(bleDevice, serviceId, charId, new BleNotifyCallback() { @Override public void onNotifySuccess() { //打开成功 showControl(bleDevice); }

        @Override
        public void onNotifyFailure(BleException exception) {
            //打开失败

// Log.i("tag", "fail|" + exception.toString()); }

        @Override
        public void onCharacteristicChanged(byte[] data) {
            //来的信息

// Log.i("tag", "data|" + byteArrayToHexString(data));

        }
    });
}

public String byteArrayToHexString(byte[] bytes) { StringBuilder sb = new StringBuilder(bytes.length * 2); for (byte element : bytes) { int v = element & 0xff; if (v < 16) { sb.append('0'); } sb.append(Integer.toHexString(v)); } return sb.toString().toUpperCase(Locale.US); }

void onBleConnSetNotifyOff(BleDevice bleDevice, String serviceId, String charId) {
    BleManager.getInstance().stopNotify(bleDevice, serviceId, charId);
}

public void sendPacket(byte[] data) {
    try {
        App.UUID_WRITE.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
        boolean result = App.UUID_WRITE.setValue(data);
        boolean res = App.GATT.writeCharacteristic(App.UUID_WRITE);

// Log.i("tag", "result|" + res); // Log.i("tag", "result||" + res); } catch (Exception e) {

    }
}
kingiis commented 4 years ago

蓝牙数据发送时可以根据boolean值得知发送成功失败,但是得到返回是回调函数,如果你想同步处理就要先主线程沉睡,因为返回值是异步的 When sending Bluetooth data, you can know the success or failure of sending according to the Boolean value, but the return function is the callback function. If you want to synchronize, you need to sleep the main thread first, because the return value is asynchronous

kingiis commented 4 years ago

or You are not communicating with SSP socket!