我英語不好。敬請諒解。我想知道藍牙設(shè)置何時完成。我想在藍牙連接和設(shè)置完成后將數(shù)據(jù)發(fā)送到我的設(shè)備。我如何知道流打開何時完成?當(dāng)我嘗試所有 BLE 設(shè)置時,我只是睡了幾秒鐘,但我想將其更改為順序代碼。private class GattClientCallback extends BluetoothGattCallback {@Override public void onServicesDiscovered( BluetoothGatt _gatt, int _status ) { super.onServicesDiscovered( _gatt, _status ); // check if the discovery failed if( _status != BluetoothGatt.GATT_SUCCESS ) { Log.e( TAG, "Device service discovery failed, status: " + _status ); return; } // find discovered characteristics List<BluetoothGattCharacteristic> matching_characteristics= BluetoothUtils.findBLECharacteristics(_gatt); if( matching_characteristics.isEmpty() ) { Log.e( TAG, "Unable to find characteristics" ); return; } // log for successful discovery Log.d( TAG, "Services discovery is successful" ); // find command characteristics from the GATT server cmd_characteristic= BluetoothUtils.findCommandCharacteristic( ble_gatt_ ); //setCharacteristicNotification ble_gatt_.setCharacteristicNotification(cmd_characteristic, true); BluetoothGattDescriptor descriptor = cmd_characteristic.getDescriptor(CHARACTERISTIC_UPDATE_NOTIFICATION_DESCRIPTOR_UUID); descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); ble_gatt_.writeDescriptor(descriptor); //descriptor write operation successfully started? // stream open complete. // update the connection status message msg_handler(SET_TV_STATUS,"connection complete"); }}
1 回答

楊__羊羊
TA貢獻1943條經(jīng)驗 獲得超7個贊
您可以實現(xiàn) onConnectionStateChange。這會讓您知道連接是否成功。
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status,int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
// you can send data here
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
}
}
添加回答
舉報
0/150
提交
取消