android ble status,bluetooth lowenergy - Android Ble status code 14 in onCharacteristicwrite - Stack...

I am developing an app using BLE where i have to send some commands to peripheral device to get response. Write characteristics code is written below:

public void writeCustomCharacteristic(String value) {

this.value = "";

if (mBluetoothAdapter == null || mBluetoothGatt == null) {

Log.w(TAG, "BluetoothAdapter not initialized");

return;

}

/*check if the service is available on the device*/

BluetoothGattService mCustomService = mBluetoothGatt.getService(SERVICE_UUID);

if (mCustomService == null) {

Log.w(TAG, "Custom BLE Service not found");

return;

}

/*get the read characteristic from the service*/

BluetoothGattCharacteristic mWriteCharacteristic = mCustomService.getCharacteristic(CHARACTERSTICS_UUID);

mWriteCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);

mWriteCharacteristic.setValue(value.getBytes());

if (!mBluetoothGatt.writeCharacteristic(mWriteCharacteristic)) {

Log.w(TAG, "Failed to write characteristic");

}

}

And after write characteristics i got response in following method here it returns status code 14, which is not mentioned in official document as well:

public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {

System.out.println("BluetoothLeService.onCharacteristicWrite");

System.out.println(BluetoothGatt.GATT_SUCCESS + " :status: " + status);

}

好的,以下是nrf52832的一个从机允许两个主机蓝牙连接的配置程序: ```c #include <stdbool.h> #include <stdint.h> #include "nrf.h" #include "nrf_delay.h" #include "boards.h" #include "bsp.h" #include "nrf_sdh.h" #include "nrf_sdh_ble.h" #include "ble.h" #include "ble_conn_state.h" #define DEVICE_NAME "Dual_Bluetooth_Connection" // 设备名称 #define APP_BLE_CONN_CFG_TAG 1 // BLE连接配置标签 #define APP_BLE_OBSERVER_PRIO 3 // BLE观察者优先级 #define APP_BLE_CONN_CFG_TAG 1 // BLE连接配置标签 #define APP_FEATURE_NOT_SUPPORTED BLE_GATT_STATUS_ATTERR_APP_BEGIN + 2 // 不支持的特性 #define BLE_GAP_CONN_SEC_MODE_MULTI_PERIPH_NO_MITM \ { \ .sm = 1, \ .lv = 1, \ .kdist_periph.enc = 1, \ .kdist_periph.id = 1, \ } static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID; // 连接句柄 // BLE观察者事件处理函数 static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context) { switch (p_ble_evt->header.evt_id) { case BLE_GAP_EVT_DISCONNECTED: // 断开连接事件 m_conn_handle = BLE_CONN_HANDLE_INVALID; break; default: // No implementation needed. break; } } // 初始化BLE协议栈 static void ble_stack_init(void) { ret_code_t err_code; // 初始化SoftDevice err_code = nrf_sdh_enable_request(); APP_ERROR_CHECK(err_code); // 等待SoftDevice启动 while (nrf_sdh_is_enabled() == false) { // Wait for SoftDevice to be enabled } // 初始化BLE协议栈 err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, BLE_CONN_CFG_TAG_DEFAULT); APP_ERROR_CHECK(err_code); // 配置BLE观察者 err_code = sd_ble_gap_device_name_set(&BLE_GAP_CONN_SEC_MODE_MULTI_PERIPH_NO_MITM, (const uint8_t *)DEVICE_NAME, strlen(DEVICE_NAME)); APP_ERROR_CHECK(err_code); err_code = sd_ble_gap_appearance_set(BLE_APPEARANCE_GENERIC_TAG); APP_ERROR_CHECK(err_code); err_code = sd_ble_gap_ppcp_set(NULL); APP_ERROR_CHECK(err_code); err_code = sd_ble_gap_tx_power_set(4); APP_ERROR_CHECK(err_code); ble_gap_conn_params_t gap_conn_params = { .min_conn_interval = MSEC_TO_UNITS(7.5, UNIT_1_25_MS), .max_conn_interval = MSEC_TO_UNITS(30, UNIT_1_25_MS), .slave_latency = 0, .conn_sup_timeout = MSEC_TO_UNITS(4000, UNIT_10_MS) }; err_code = sd_ble_gap_ppcp_set(&gap_conn_params); APP_ERROR_CHECK(err_code); // 注册BLE事件处理函数 err_code = sd_ble_gap_adv_data_set(NULL, 0); APP_ERROR_CHECK(err_code); ble_uuid_t adv_uuids[] = {{BLE_UUID_DEVICE_INFORMATION_SERVICE, BLE_UUID_TYPE_BLE}}; ble_advdata_t advdata = { .name_type = BLE_ADVDATA_FULL_NAME, .include_appearance = false, .flags = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE, .uuids_complete.uuid_cnt = sizeof(adv_uuids) / sizeof(adv_uuids[0]), .uuids_complete.p_uuids = adv_uuids, }; err_code = ble_advdata_encode(&advdata, NULL, &advdata.adv_data.len); APP_ERROR_CHECK(err_code); err_code = sd_ble_gap_adv_data_set(&advdata.adv_data, advdata.scan_rsp_data); APP_ERROR_CHECK(err_code); ble_gap_conn_sec_mode_t sec_mode; BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode); err_code = sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *)DEVICE_NAME, strlen(DEVICE_NAME)); APP_ERROR_CHECK(err_code); err_code = sd_ble_gap_appearance_set(BLE_APPEARANCE_GENERIC_TAG); APP_ERROR_CHECK(err_code); err_code = sd_ble_gap_adv_start(NULL, APP_BLE_CONN_CFG_TAG); APP_ERROR_CHECK(err_code); } // 初始化连接状态模块 static void conn_state_init(void) { ret_code_t err_code = ble_conn_state_init(); APP_ERROR_CHECK(err_code); } int main(void) { // 初始化 bsp_board_init(BSP_INIT_LEDS); ble_stack_init(); conn_state_init(); // 循环 while (true) { // 检查连接状态 if (m_conn_handle != BLE_CONN_HANDLE_INVALID) { // 如果有连接,闪烁LED bsp_board_led_invert(LED_1); nrf_delay_ms(500); } else { // 如果没有连接,LED关闭 bsp_board_led_off(LED_1); nrf_delay_ms(500); } } } ``` 需要注意的是,这只是一个简单的示例程序,如果需要在实际项目中使用,还需要根据具体需求进行修改和优化。同时,由于具体的硬件和软件平台可能不同,还需要根据实际情况进行适当的调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值