android ble ondescriptorwrite,bluetooth - Android BLE Gatt timeout after gatt.writeDescriptor() - St...

I am currently trying to get a BLE app on the android to work. Specifically, I am trying to pickup a BLE signal sent from an iOS device. Below is the code relevant to my problem.

public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

Log.w(tag, "State: " + newState+" status: "+ status + " (0 is success)");

if (newState == BluetoothProfile.STATE_CONNECTED) {

String intentAction = "com.example.bluetooth.le.ACTION_GATT_CONNECTED";

final Intent intent = new Intent(intentAction);

callbackActivity.sendBroadcast(intent);

Log.w(tag, "Connected to GATT server.");

// Attempts to discover services after successful connection.

Log.w(tag, "Attempting to start service discovery:" +

mConnectedGatt.discoverServices());

// Clear data to prepare for new data reading

bluetoothData.clear();

} else {

Log.w(tag, "GATT connection unsuccessful.");

restartSearch(gatt);

}

}

/**

* Called when a service has been discovered in the connected device

*/

@Override

public void onServicesDiscovered(BluetoothGatt gatt, int status) {

Log.w(tag, "Services Discovered! "+status);

if(gatt.getService(SERVICE_UUID)!=null){

Log.w(tag, "Not null!");

BluetoothGattCharacteristic characteristic=gatt.getService(SERVICE_UUID).getCharacteristic(CHARACTERISTC_UUID);

gatt.setCharacteristicNotification(characteristic, true);

BluetoothGattDescriptor descriptor = characteristic.getDescriptor(

CHARACTERISTIC_UPDATE_NOTIFICATION_DESCRIPTOR_UUID);

descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);

Log.w(tag, "Writing to descriptor: "+mConnectedGatt.writeDescriptor(descriptor));

}

else{

Log.w(tag, "Services discovered unsuccessful");

restartSearch(gatt);

}

When I activate both devices, the android device always pickups the connection. The connection is always a success, and the call to discoverServices() always results in a callback to onServiceDiscovered. The characteristic I am searching for is found, and the call mConnectedGatt.writeDescriptor(descriptor) always returns true. However, my onCharacteristicChanged callback is never called. Instead, the program waits for about 10 seconds after the writeDescriptor call, and then I get a call to onConnectionStateChanged specifying that the connection has been lost. This happens 95% of the time. 5% of the time, everything works perfectly and I get the call to onCharacteristicChanged.

Are there any reasons for this timeout that keeps happening? As a side note, I believe this is a problem within my android code as I can perfectly send and receive this BLE signal between iOS devices.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值